C# 和 JS 关于 CryptoJS 的加密、解密

1.下载个CryptoJS 我的是CryptoJS-v3.1.2 版本的

C#的

 var a = "a[a]/e:";
            var b = "k3RA2bS97JlV5XPEueiqUA==";
            var c = EncryptByAES(a, "1234567891234567");
            var d =  DecryptByAES(c, "1234567891234567");

  

 // <summary>  
        /// AES解密  
        /// </summary>  
        /// <param name="input">密文字节数组</param>  
        /// <param name="key">密钥(16位)</param>  
        /// <returns>返回解密后的字符串</returns>  
        public static string DecryptByAES(string input, string key)
        {
            byte[] inputBytes = Convert.FromBase64String(input);
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 16));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.ECB;
                aesAlg.Key = keyBytes;
                aesAlg.IV = keyBytes;
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                        {
                            return srEncrypt.ReadToEnd();
                        }
                    }
                }
            }
        }


        /// <summary>  
        /// AES加密算法  
        /// </summary>  
        /// <param name="input">明文字符串</param>  
        /// <param name="key">密钥(16位)</param>  
        /// <returns>字符串</returns>  
        public static string EncryptByAES(string input, string key)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 16));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.ECB;
                aesAlg.Key = keyBytes;
                aesAlg.IV = keyBytes;
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        byte[] bytes = msEncrypt.ToArray();
                      var ddc = Convert.ToBase64String(bytes);
         
                        byte[] inputBytes = Convert.FromBase64String(ddc);
                        return ddc;
                    }
                }
            }
        }
        /// <summary>
        /// 将指定的16进制字符串转换为byte数组
        /// </summary>
        /// <param name="s">16进制字符串(如:“7F 2C 4A”或“7F2C4A”都可以)</param>
        /// <returns>16进制字符串对应的byte数组</returns>
        public static byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        /// <summary>
        /// 将一个byte数组转换成一个格式化的16进制字符串
        /// </summary>
        /// <param name="data">byte数组</param>
        /// <returns>格式化的16进制字符串</returns>
        public static string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
            {
                //16进制数字
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
                //16进制数字之间以空格隔开
                //sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            return sb.ToString().ToUpper();
        }

  

 

 

JS的

<script src="~/Content/select/js/jquery-1.11.3.min.js"></script>
<script src="~/Content/CryptoJS-v3.1.2/rollups/aes.js"></script>
<script src="~/Content/CryptoJS-v3.1.2/components/mode-ecb-min.js"></script>
<script src="~/Content/CryptoJS-v3.1.2/components/pad-zeropadding.js"></script>

  

 var a = "k3RA2bS97JlV5XPEueiqUA==";
    var b = 'a[a]/e:';
    var c = Encrypt2(b).toString();
    var d = Encrypt3(b);
    var e = Decrypt2(a);
    var f = Decrypt3(a);

  

function Encrypt2(text) {
        var key = CryptoJS.enc.Utf8.parse('1234567891234567');
        var iv = CryptoJS.enc.Latin1.parse('1234567891234567');
        var b = CryptoJS.AES.encrypt(a, key, {
            iv: iv,
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return b
    }

    function Encrypt3(text) {
        var key = CryptoJS.enc.Utf8.parse("1234567891234567");
        var InDkn4 = CryptoJS.enc.Utf8.parse(text);
        var b = CryptoJS.AES.encrypt(InDkn4, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return b["toString"]()
    }

    function Decrypt2(text) {
        var key = CryptoJS["enc"]["Utf8"]["parse"]("1234567891234567");
        var b = CryptoJS["AES"]["decrypt"](text, s3, {
            mode: CryptoJS["mode"]["ECB"],
            padding: CryptoJS["pad"]["Pkcs7"]
        });
       // var t = CryptoJS["enc"]["Utf8"]["stringify"](b);
        return CryptoJS["enc"]["Utf8"]["stringify"](b)["toString"]()
    }

    function Decrypt3(text) {
        var key = CryptoJS.enc.Utf8.parse("1234567891234567");
        var kIlnQ10 = CryptoJS.AES.decrypt(text, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        var t = CryptoJS.enc.Utf8.stringify(kIlnQ10);
        return t
    }

  

posted @ 2020-06-12 17:25  荧屏  阅读(4357)  评论(2编辑  收藏  举报