您好,登录后才能下订单哦!
本篇内容介绍了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。