关于aes:CryptoJs-使用-AES-CBC-加解密数据

111次阅读

共计 2470 个字符,预计需要花费 7 分钟才能阅读完成。

AES CBC 模式

废话不多说,间接上代码,加密步骤为:

  1. 约定 key, 生成本次加密须要的 iv
  2. 数据体 cbc 加密 encData
  3. 数据体 base64 编码 encDataBase64ed
  4. 计算数据体的 hmac
  5. {iv, mac, encDataBase64ed} stringify = payload
  6. payload base64 编码传输。payloadBase64ed
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AesCBC 加解密 </title>
    <style>
        * {box-sizing: border-box;}
        input, textarea {
            width: 100%;
            padding: 10px 20px;
            border-radius: 10px;
        }
    </style>
</head>
<body>
<div style="padding: 0 20px">
    <div>
        <h4>Key</h4>
        <input type="text" id="key" placeholder="请输出约定的 Key">
    </div>
    <div>
        <h4> 密文 </h4>
        <textarea name="encData" id="encData" rows="10" oninput="encDataDec()"></textarea>
    </div>
    <div>
        <h4> 明文 </h4>
        <textarea name="decData" id="decData" rows="10" oninput="decDataEnc()"></textarea>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
<script>
    function encDataDec() {let key = document.getElementById("key").value
        if (key.trim() === "") {alert("请输出约定的 Key")
            return;
        }
        let encDataDom = document.getElementById("encData")
        let decDataDom = document.getElementById("decData")
        if (encDataDom.value === "") {
            decDataDom.value = ""
            return
        }
        try {decDataDom.value = AesCBCDecrypt(encDataDom.value, key)
        } catch (err) {decDataDom.value = err.toString()
        }
    }

    function decDataEnc() {let key = document.getElementById("key").value
        if (key.trim() === "") {alert("请输出约定的 Key")
            return;
        }
        let encDataDom = document.getElementById("encData")
        let decDataDom = document.getElementById("decData")
        if (decDataDom.value === "") {
            encDataDom.value = ""
            return
        }
        try {encDataDom.value = AesCBCEncrypt(decDataDom.value, key)
        } catch (err) {encDataDom.value = err.toString()
        }
    }

    function AesCBCEncrypt(bizData, key) {let iv = "te21".repeat(4)//16 位随机 iv

        let waKey = CryptoJS.enc.Utf8.parse(key)
        let waIv = CryptoJS.enc.Utf8.parse(iv)

        let bizDataEnc = CryptoJS.AES.encrypt(bizData, waKey, {
            iv: waIv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })

        let bizDataEncBase64 = CryptoJS.enc.Base64.stringify(bizDataEnc.ciphertext)
        let ivBase64 = CryptoJS.enc.Base64.stringify(waIv)

        let mac = CryptoJS.HmacSHA256(ivBase64 + bizDataEncBase64, waKey).toString()
        let payloadJson = {
            iv: ivBase64,
            mac: mac,
            value: bizDataEncBase64
        }
        return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(JSON.stringify(payloadJson)))
    }

    function AesCBCDecrypt(payloadEnc, key) {let payloadJson = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(payloadEnc)))
        let waKey = CryptoJS.enc.Utf8.parse(key)
        let waIv = CryptoJS.enc.Base64.parse(payloadJson.iv)
        let bizData = CryptoJS.AES.decrypt(payloadJson.value, waKey, {
            iv: waIv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(bizData)
    }
</script>
</body>
</html>

正文完
 0