CryptoJS与C#AES加解密互转

CryptoJS下载地址:

https://code.google.com/archive/p/crypto-js/downloads

http://download.csdn.net/detail/wz122889488/9851085

页面js引用:

    <script type="text/javascript" src="/content/plugin/CryptoJSv3.1.2/components/core-min.js"></script>
    <script type="text/javascript" src="/content/plugin/CryptoJSv3.1.2/rollups/aes.js"></script>

 

JS端AES加密解密:

复制代码
 1     com.str = {
 2         _KEY: "12345678900000001234567890000000",//32位
 3         _IV: "1234567890000000",//16位
 4         /**************************************************************
 5         *字符串加密
 6         *   str:需要加密的字符串
 7         ****************************************************************/
 8         Encrypt: function (str) {
 9             var key = CryptoJS.enc.Utf8.parse(this._KEY); 
10             var iv = CryptoJS.enc.Utf8.parse(this._IV);
11 
12             var encrypted = '';
13 
14             var srcs = CryptoJS.enc.Utf8.parse(str);
15             encrypted = CryptoJS.AES.encrypt(srcs, key, {
16                 iv: iv,
17                 mode: CryptoJS.mode.CBC,
18                 padding: CryptoJS.pad.Pkcs7
19             });
20 
21             return encrypted.ciphertext.toString();
22         },
23 
24         /**************************************************************
25         *字符串解密
26         *   str:需要解密的字符串
27         ****************************************************************/
28         Decrypt: function (str) {
29             var key = CryptoJS.enc.Utf8.parse(this._KEY);
30             var iv = CryptoJS.enc.Utf8.parse(this._IV);
31             var encryptedHexStr = CryptoJS.enc.Hex.parse(str);
32             var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
33             var decrypt = CryptoJS.AES.decrypt(srcs, key, {
34                 iv: iv,
35                 mode: CryptoJS.mode.CBC,
36                 padding: CryptoJS.pad.Pkcs7
37             });
38             var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
39             return decryptedStr.toString();
40         }
41     }
复制代码

C# AES加密解密:

复制代码
const string AES_IV = "1234567890000000";//16位    

/// <summary> /// AES加密算法 /// </summary> /// <param name="input">明文字符串</param> /// <param name="key">密钥(32位)</param> /// <returns>字符串</returns> public static string EncryptByAES(string input, string key) { byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32)); using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.Key = keyBytes; aesAlg.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16)); 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(); return ByteArrayToHexString(bytes); } } } } /// <summary> /// AES解密 /// </summary> /// <param name="input">密文字节数组</param> /// <param name="key">密钥(32位)</param> /// <returns>返回解密后的字符串</returns> public static string DecryptByAES(string input, string key) { byte[] inputBytes = HexStringToByteArray(input); byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32)); using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.Key = keyBytes; aesAlg.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16)); 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> /// 将指定的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(); }
复制代码

 

用法:

com.str.Encrypt("2017-05")//结果:68f4a7903a9fe6085d2301ac68cc039c
com.str.Decrypt("68f4a7903a9fe6085d2301ac68cc039c")//结果:2017-05
//加密
string str1 = Encrypt.EncryptByAES("2017-05", "12345678900000001234567890000000");
//解密
string str2 = Encrypt.DecryptByAES("68f4a7903a9fe6085d2301ac68cc039c", "12345678900000001234567890000000");

 注:前后台的加密Key必须一致。

posted @ 2019-09-11 16:36  心冰之海  阅读(1595)  评论(1编辑  收藏  举报