美文网首页
Js AES(ECB模式)加密和解密

Js AES(ECB模式)加密和解密

作者: Pino | 来源:发表于2022-01-10 13:52 被阅读0次

    全部拷贝到html文件 即可直接运行

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
    </body>
    
    </html>
    <script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.0/aes.js"></script>
    <script>
        /**
         * 加密
         * @param encryptString 要加密的字符串
         * @param key 秘钥
         * @returns {string} 加密后的字符串
         */
        function aesEncrypt(encryptString, key) {
            var key = CryptoJS.enc.Utf8.parse(key);
            var srcs = CryptoJS.enc.Utf8.parse(encryptString);
            var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
            return encrypted.toString();
        }
    
        /**
         * 解密
         * @param decryptString 要解密的字符串
         * @param key 秘钥
         * @returns {string} 解密后的字符串
         */
        function aesDecrypt(decryptString, key) {
            var key = CryptoJS.enc.Utf8.parse(key);
            var decrypt = CryptoJS.AES.decrypt(decryptString, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
            return CryptoJS.enc.Utf8.stringify(decrypt).toString();
        }
    
        var key = "9966"; // 至少四位数字
        var pazzword = "杰克马的财富密码";
        // aes 加密
        pazzword = aesEncrypt(pazzword, key);
        console.log('加密后:' + pazzword);
        // 解密
        console.log("解密后:" + aesDecrypt(pazzword, key));
    </script>
    

    相关文章

      网友评论

          本文标题:Js AES(ECB模式)加密和解密

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