关于javascript:从script标签引入AESjs进行加密

39次阅读

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

<!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>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/aes-js/3.1.2/index.js"></script>
<script>

    const aeskeyLocal = '' // 本人写
    const aesUtils = aesjs.utils;
    const key = aesUtils.utf8.toBytes(aeskeyLocal);
    var AES = new aesjs.ModeOfOperation.ecb(key)

    function encrypt (word) {var pad = aesUtils.utf8.toBytes(word)
        var result = AES.encrypt(aesjs.padding.pkcs7.pad(pad))
        return aesUtils.hex.fromBytes(result)
    }

    function decrypt(text) {var str = AES.decrypt(aesUtils.hex.toBytes(text))
        var res = aesjs.padding.pkcs7.strip(str)
        return aesUtils.utf8.fromBytes(res)
    }

    // 加密
    const word = '' // 本人写
    console.log(encrypt(word))
    // 解密
    const text = '' // 本人写
    console.log(decrypt(text))

</script>

正文完
 0