美文网首页
crypto加密

crypto加密

作者: 小米和豆豆 | 来源:发表于2021-04-01 16:54 被阅读0次

需要自己下载crypto-js.js引入

<script src="js/crypto-js.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  //解析url参数
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return "";
    }
    var d = GetQueryString("d");
    var c = GetQueryString("c");
////////////////
    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,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return encrypted.toString(); //返回的是base64格式的密文
    }

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

    function getAES(data) { //加密
        var key = 'AAAAAAAAAAAAAAAAAAAAAAAAAABCDEFg'; //密钥
        var iv = '1234567812345678';
        var encrypted = getAesString(data, key, iv); //密文
        return encrypted;
    }

    function getDAes(data) { //解密
        var key = 'AAAAAAAAAAAAAAAAAAAAAAAAAABCDEFg'; //密钥
        var iv = '1234567812345678';
        var decryptedStr = getDAesString(data, key, iv);
        return decryptedStr;
    }
    console.log(getAES('啊啊啊'))
    console.log(getDAes('ZP1mdKc2K0cR5R/0raTksQ=='))
</script>

相关文章

网友评论

      本文标题:crypto加密

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