crypto加密

2021-04-01  本文已影响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>
上一篇下一篇

猜你喜欢

热点阅读