CryptoJS與C#AES加解密互轉

來源:互聯網
上載者:User

標籤:密碼編譯演算法   param   c#   provider   空格   writer   public   return   end   

CryptoJS:

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

頁面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.Pkcs719             });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.Pkcs737             });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")//結果:68f4a7903a9fe6085d2301ac68cc039ccom.str.Decrypt("68f4a7903a9fe6085d2301ac68cc039c")//結果:2017-05
//加密string str1 = Encrypt.EncryptByAES("2017-05", "12345678900000001234567890000000");//解密string str2 = Encrypt.DecryptByAES("68f4a7903a9fe6085d2301ac68cc039c", "12345678900000001234567890000000");

 

CryptoJS與C#AES加解密互轉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.