您好,登錄后才能下訂單哦!
本篇內容介紹了“C#加解密之AES算法如何實現”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
AES是塊加密,稍微介紹一些AES的參數以及約束:
Key(密鑰):AES的密鑰長度必須為128Bit、192Bit、256Bit(一般又都稱為AES128,AES192,AES256),我們一般使用的字母數字等,占位都是1Byte,根據換算公式 1 Byte=8 Bit,所以我們的密鑰長度就只能是16、24、32;
IV(向量):“加密”初始塊,一般為128Bit,即長度為16,與塊的長度一樣
Mode(加密模式):可分為CBC(密碼塊鏈模式)、ECB(電子密碼本模式)、OFB(輸出反饋模式)、 CFB(密碼反饋模式)、CTS(密碼文本竊用模式)、CTR(計數器模式)
Padding(填充模式):NoPadding(不填充)、PKCS7(每個字節填充該字節序列的長度)、Zeros(填充0)、ANSIX923(最后一個字節填充字節序列的長度,其余字節均填充數字零)、ISO10126(最后一個字節填充字節序列的長度,其余字節填充隨機數據)、PKCS5(和PKCS7一樣)
使用AES加密方式加解密文本數據
開發工具:Visual Studio 2013
.NET Framework版本:4.5
public class AesUtil { /// <summary> /// AES加密 /// </summary> /// <param name="aesModel"></param> /// <returns></returns> public static byte[] Encrypt(AesModel aesModel) { //使用32位密鑰 byte[] key32 = new byte[32]; //如果我們的密鑰不是32為,則自動補全到32位 byte[] byteKey = Encoding.UTF8.GetBytes(aesModel.Key.PadRight(key32.Length)); //復制密鑰 Array.Copy(byteKey, key32, key32.Length); //使用16位向量 byte[] iv16 = new byte[16]; //如果我們的向量不是16為,則自動補全到16位 byte[] byteIv = Encoding.UTF8.GetBytes(aesModel.IV.PadRight(iv16.Length)); //復制向量 Array.Copy(byteIv, iv16, iv16.Length); // 創建加密對象,Rijndael 算法 //Rijndael RijndaelAes = Rijndael.Create(); RijndaelManaged RijndaelAes = new RijndaelManaged(); RijndaelAes.Mode = aesModel.Mode; RijndaelAes.Padding = aesModel.Padding; RijndaelAes.Key = key32; RijndaelAes.IV = iv16; byte[] result = null; try { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream EncryptStream = new CryptoStream(ms, RijndaelAes.CreateEncryptor(), CryptoStreamMode.Write)) { EncryptStream.Write(aesModel.Data, 0, aesModel.Data.Length); EncryptStream.FlushFinalBlock(); result = ms.ToArray(); } } } catch { } return result; } /// <summary> /// AES解密 /// </summary> /// <param name="aesModel"></param> /// <returns></returns> public static byte[] Decrypt(AesModel aesModel) { //使用32位密鑰 byte[] key32 = new byte[32]; //如果我們的密鑰不是32為,則自動補全到32位 byte[] byteKey = Encoding.UTF8.GetBytes(aesModel.Key.PadRight(key32.Length)); //復制密鑰 Array.Copy(byteKey, key32, key32.Length); //使用16位向量 byte[] iv16 = new byte[16]; //如果我們的向量不是16為,則自動補全到16位 byte[] byteIv = Encoding.UTF8.GetBytes(aesModel.IV.PadRight(iv16.Length)); //復制向量 Array.Copy(byteIv, iv16, iv16.Length); // 創建解密對象,Rijndael 算法 //Rijndael RijndaelAes = Rijndael.Create(); RijndaelManaged RijndaelAes = new RijndaelManaged(); RijndaelAes.Mode = aesModel.Mode; RijndaelAes.Padding = aesModel.Padding; RijndaelAes.Key = key32; RijndaelAes.IV = iv16; byte[] result = null; try { using (MemoryStream ms = new MemoryStream(aesModel.Data)) { using (CryptoStream DecryptStream = new CryptoStream(ms, RijndaelAes.CreateDecryptor(), CryptoStreamMode.Read)) { using (MemoryStream msResult = new MemoryStream()) { byte[] temp = new byte[1024*1024]; int len = 0; while ((len = DecryptStream.Read(temp, 0, temp.Length)) > 0) { msResult.Write(temp, 0, len); } result = msResult.ToArray(); } } } } catch { } return result; } /// <summary> /// AES加密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Encrypt(string data, string key, string iv="") { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] result = Encrypt(new AesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Convert.ToBase64String(result); } /// <summary> /// AES解密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Decrypt(string data, string key, string iv = "") { byte[] bytes = Convert.FromBase64String(data); byte[] result = Decrypt(new AesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Encoding.UTF8.GetString(result); } public class AesModel { /// <summary> /// 需要加密/解密的數據 /// </summary> public byte[] Data { get; set; } /// <summary> /// 密鑰 /// </summary> public string Key { get; set; } /// <summary> /// 向量 /// </summary> public string IV { get; set; } /// <summary> /// 加密模式 /// </summary> public CipherMode Mode { get; set; } /// <summary> /// 填充模式 /// </summary> public PaddingMode Padding { get; set; } } }
private void btn_Aes_Encrypt_Click(object sender, EventArgs e) { string result= AesUtil.Encrypt(textBox1.Text, "12345678900987654321"); textBox2.Text = result; } private void btn_Aes_Decrypt_Click(object sender, EventArgs e) { string result = AesUtil.Decrypt(textBox2.Text, "12345678900987654321"); textBox1.Text = result; }
“C#加解密之AES算法如何實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。