当前位置:   article > 正文

cryptojs加密解密_removing dependencies.crypto-js in favor of devdep

removing dependencies.crypto-js in favor of devdependencies.crypto-js

AES-128-ECB模式

1.html+js+aes+128+ecb

直接上代码

 

<script src="js/crypto-js.min.js"></script>
  1. /**
  2. * 解密
  3. * @param data 要解密的字符串
  4. * @param keys 秘钥
  5. * @returns {string} 解密后的字符串
  6. */
  7. aesDecrypt(data, keys) {
  8. let key = CryptoJS.enc.Utf8.parse(keys);//要和加密的密钥一样
  9. let decrypt = CryptoJS.AES.decrypt(data, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  10. return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  11. },
  12. /**
  13. * 加密
  14. * @param data 要加密的字符串
  15. * @param keys 秘钥
  16. * @returns {string} 加密后的字符串
  17. */
  18. aesEncrypt(data, keys) {
  19. let key = CryptoJS.enc.Utf8.parse(keys);
  20. let srcs = CryptoJS.enc.Utf8.parse(data);
  21. const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  22. return encrypted.toString();
  23. },

 js调用

  1. //加密
  2. const epwd= aesEncrypt("abcd.1234567890","E6EAB77A-4513-4EB3-A56D-E8C435CE);
  3. console.log(epwd)
  4. //解密
  5. const dpwd= aesDecrypt(epwd,"E6EAB77A-4513-4EB3-A56D-E8C435CE),
  6. console.log(dpwd)

2.先全局安装:npm install crypto-js --save-dev  (PC、端移动端、小程序)

import CryptoJS from 'crypto-js';

 

  1. var app = new Vue({
  2. el: '#app',
  3. data() {
  4. return {}
  5. },
  6. methods: {
  7. /**
  8. * 解密
  9. * @param data 要解密的字符串
  10. * @param keys 秘钥
  11. * @returns {string} 解密后的字符串
  12. */
  13. aesDecrypt(data, keys) {
  14. let key = CryptoJS.enc.Utf8.parse(keys);//要和加密的密钥一样
  15. let decrypt = CryptoJS.AES.decrypt(data, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  16. return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  17. },
  18. /**
  19. * 加密
  20. * @param data 要加密的字符串
  21. * @param keys 秘钥
  22. * @returns {string} 加密后的字符串
  23. */
  24. aesEncrypt(data, keys) {
  25. let key = CryptoJS.enc.Utf8.parse(keys);
  26. let srcs = CryptoJS.enc.Utf8.parse(data);
  27. const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  28. return encrypted.toString();
  29. },
  30. }
  1. //加密
  2. const epwd= this.aesEncrypt("abcd.1234567890","E6EAB77A-4513-4EB3-A56D-E8C435CE);
  3. console.log(epwd)
  4. //解密
  5. const dpwd= this.aesDecrypt(epwd,"E6EAB77A-4513-4EB3-A56D-E8C435CE),
  6. console.log(dpwd)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/873088
推荐阅读
相关标签
  

闽ICP备14008679号