美文网首页
vue、js、Angularjs AES加密解密

vue、js、Angularjs AES加密解密

作者: 小小少年小阿清 | 来源:发表于2020-10-28 16:29 被阅读0次

需要两个js文件。aes.js和mode-ecb.js
以angular为例,现在www目录下的index.html文件中引入这两个js文件

<!-- 加密相关 -->
  <script src="build/lib/cryptojslib/rollups/aes.js"></script>
  <script src="build/lib/cryptojslib/components/mode-ecb.js"></script>

然后在需要的地方加上加密解密逻辑。

  function encryptDecryptMethod(){
   //加密密钥
    const key = CryptoJS.enc.Hex.parse("288d7bab3d6ac01ad9dc6a897754f3d3");
   // 需要加密的内容
    const dataContent="哈哈哈哈"
    const srcs = CryptoJS.enc.Utf8.parse(dataContent);
    const encrypted = CryptoJS.AES.encrypt(srcs, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
    const resData = encrypted.toString();
    console.log('加密后:',resData)

    const decrypt = CryptoJS.AES.decrypt(resData, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
    const result = CryptoJS.enc.Utf8.stringify(decrypt).toString();
      console.log('解密后:',result)
    };


vue则是需要下载相关依赖,然后需要在页面上引入。

相关文章

网友评论

      本文标题:vue、js、Angularjs AES加密解密

      本文链接:https://www.haomeiwen.com/subject/xchcvktx.html