前端 crypto-js AES 加解密

AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。严格地说,AES和Rijndael加密法并不完全一样(虽然在实际应用中二者可以互换),因为Rijndael加密法可以支持更大范围的区块和密钥长度:AES的区块长度固定为128比特,密钥长度则可以是128,192或256比特;而Rijndael使用的密钥和区块长度可以是32位的整数倍,以128位为下限,256比特为上限。包括AES-ECBAES-CBCAES-CTRAES-OFBAES-CFB

JavaScript加密库Crypto-JShttps://github.com/brix/crypto-js

CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加密算法。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>

    <script src="./crypto-js/src/core.js"></script>
    <script src="./crypto-js/src/evpkdf.js"></script>
    <script src="./crypto-js/src/cipher-core.js"></script>
    <script src="./crypto-js/src/md5.js"></script>
    <script src="./crypto-js/src/aes.js"></script>
    <script src="./crypto-js/src/tripledes.js"></script>
    <script src="./crypto-js/src/enc-base64.js"></script>
    <script src="./crypto-js/src/enc-utf16.js"></script>
    <script src="./crypto-js/src/format-hex.js"></script>
    <script src="./crypto-js/src/hmac.js"></script>
    <script src="./crypto-js/src/lib-typedarrays.js"></script>
    <script src="./crypto-js/src/mode-cfb.js"></script>
    <script src="./crypto-js/src/mode-ctr.js"></script>
    <script src="./crypto-js/src/mode-ctr-gladman.js"></script>
    <script src="./crypto-js/src/mode-ecb.js"></script>
    <script src="./crypto-js/src/mode-ofb.js"></script>
    <script src="./crypto-js/src/pad-ansix923.js"></script>
    <script src="./crypto-js/src/pad-iso10126.js"></script>
    <script src="./crypto-js/src/pad-iso97971.js"></script>
    <script src="./crypto-js/src/pad-nopadding.js"></script>
    <script src="./crypto-js/src/pad-zeropadding.js"></script>
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<button id="md5">MD5</button>
<button id="aes">AES</button>

<script>

$(function() {
    $("#md5").click(function(){
        console.log("测试");
        //MD5
        var md5Str = CryptoJS.MD5("key").toString(CryptoJS.enc.Hex).toUpperCase();
        console.log("MD5后得到的字符串:%s", md5Str)
        return false;
    });

    $("#aes").click(function() {
        var encrypted = getAES('AppBlog.CN')
        console.log("AES加密得到的字符串:%s", encrypted)
        var decrypted = getDeAes(encrypted)
        console.log("AES解密得到的字符串:%s", decrypted)

        /********************/
        var key = '12345678';
        console.log('密钥:', key);
        key = CryptoJS.enc.Hex.parse(key)
        var src = "AppBlog.CN";
        console.log('原字符串:', src);
        var enc = CryptoJS.AES.encrypt(src ,key, {
            mode: CryptoJS.mode.ECB,  
            padding: CryptoJS.pad.Pkcs7
        })

        var encode = enc.ciphertext.toString()
        console.log("加密:", encode);

        var decode = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(encode), key, {
            //iv:iv,
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        console.log('解密:', CryptoJS.enc.Utf8.stringify(decode));

        return false;
    });

    function getAesString(data, key, iv) {  //加密
        var key = CryptoJS.enc.Utf8.parse(key);
        var iv  = CryptoJS.enc.Utf8.parse(iv);
        var encrypted = CryptoJS.AES.encrypt(data, key,
            {
                iv: iv,  //CBC模式需要指定偏移量
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
        console.log(encrypted.ciphertext.toString())
        return encrypted.ciphertext.toString()
        //return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);  //返回Base64格式的密文
    }

    function getDeAesString(encrypted, key, iv) {  //解密
        var key = CryptoJS.enc.Utf8.parse(key);
        var iv  = CryptoJS.enc.Utf8.parse(iv);
        var decrypted = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(encrypted), key,
        //var decrypted = CryptoJS.AES.decrypt(CryptoJS.enc.Base64.parse(encrypted), key,
            {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
        return decrypted.toString(CryptoJS.enc.Utf8);
    }

    function getAES(data) {  //加密
        var key = '12345678';  //密钥(数位为8的整数倍)
        var iv  = '1234567812345678';
        var encrypted = getAesString(data, key, iv);  //密文
        var encryptedStr = CryptoJS.enc.Utf8.parse(encrypted);
        return encrypted;
    }

    function getDeAes(data) {  //解密
        var key = '12345678';  //密钥(数位为8的整数倍)
        var iv  = '1234567812345678';
        var decryptedStr = getDeAesString(data, key, iv);
        return decryptedStr;
    }
});
</script>
</body>
</html>

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/12/frontend-crypto-js-aes-encryption-and-decryption/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
前端 crypto-js AES 加解密
AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来……
<<上一篇
下一篇>>
文章目录
关闭
目 录