当前位置: 首页 > 工具软件 > Crypt-LE > 使用案例 >

AES 加密,C#后台,javascript前台,crypt-js

吉和同
2023-12-01

javascript前台代码

<script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/components/core-min.js"></script>
<script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/components/enc-utf16-min.js"></script>
<script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/components/sha1-min.js"></script>
<script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/components/md5-min.js"></script>
<script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script>
    function ajaxdecrypt() {
        var keystr = CryptoJS.enc.Utf16LE.parse($("#projectSign").val());
        var keymd5 = CryptoJS.MD5(keystr);
        var key = keymd5.toString(CryptoJS.enc.Hex);

        $.post("DecryptAes", { cipher: $("#txt").val(), key: key, iv: key }, function (result) {
            $("#txt").val(result);
        });
    }

    function encrypt() {
        var plantstr = $("#userSign").val();
        var plant = CryptoJS.enc.Utf16LE.parse(plantstr);

        var keystr = CryptoJS.enc.Utf16LE.parse($("#projectSign").val());
        var keymd5 = CryptoJS.MD5(keystr).toString(CryptoJS.enc.Hex);
        var key = CryptoJS.enc.Hex.parse(keymd5);

        //var encrypted = CryptoJS.AES.encrypt(plant, key, { iv: key, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });
        var encrypted = CryptoJS.AES.encrypt(plant, key, { iv: key });

        $("#txt").val(encrypted);
    }
    function decrypt() {
        var cipherstr = $("#txt").val();

        var keystr = CryptoJS.enc.Utf16LE.parse($("#projectSign").val());
        var keycode = CryptoJS.MD5(keystr).toString(CryptoJS.enc.Hex);
        var key = CryptoJS.enc.Hex.parse(keycode);

        var plantstr = CryptoJS.AES.decrypt(cipherstr, key, { iv: key });
        var plant = CryptoJS.enc.Utf16LE.stringify(plantstr);

        $("#passwordSign").val(plant);
    }

</script>

C#后台

public ActionResult DecryptAes(string cipher,string key,string iv)
        {
            AesManaged aes = new AesManaged();

            aes.Key = HexStringToBytes(key);
            aes.IV = HexStringToBytes(iv);
            //aes.Mode = CipherMode.CBC;
            //aes.Padding = PaddingMode.Zeros;
            ICryptoTransform de =  aes.CreateDecryptor();

            byte[] cipherBytes = Convert.FromBase64String(cipher);

            byte[] plantBytes =  de.TransformFinalBlock(cipherBytes,0,cipherBytes.Length);

            return Content(Encoding.Unicode.GetString(plantBytes));
        }

转载于:https://www.cnblogs.com/bushuosx/p/3931698.html

 类似资料: