从这一篇开始呢,写一下常用的一些加解密方式。一般我们来说呢,对于加密,我们分为可逆和不可逆。可逆加密又可分为对称加密(AES、DES等)和非对称加密(RSA),还有就是一些编码加密等(BASE64);不可逆的呢,大部分又都称为摘要算法(MD5、SHA)。
其实上面扯这些也是白扯,对于一般用户来讲,我从明文能变成看不懂的密文就是加密了,管他叫什么,为什么要写这些,因为我发现很多人喜欢较真,拿MD5来说吧,专业点来讲,他确实是摘要算法而不是加密算法,但很多人就是喜欢称为MD5加密,反正我觉得没啥大问题,因为的确可以理解成一种不可逆的加密,大家既然说顺口了就按顺口的来吧,反正都懂就行。
这一篇主要来写一下对称加密算法中的AES加密,什么是对称加密?简单理解来说,我只有这一把钥匙,它既可以开锁也可以关锁,其他钥匙肯定不行。
AES是块加密,稍微介绍一些AES的参数以及约束:
理解了上面所描述的那些参数之后(其实不理解有没关系,只是做一下基础了解,我们大部分都只是做应用层,能用就行了);接下来我们使用代码来实现,更直观的感受下。
实现功能:
使用AES加密方式加解密文本数据
开发环境:
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码:
- public class AesUtil
- {
- ///
- /// AES加密
- ///
- ///
- ///
- 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;
- }
-
- ///
- /// AES解密
- ///
- ///
- ///
- 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;
- }
-
- ///
- /// AES加密字符串
- ///
- ///
- ///
- ///
- ///
- 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);
- }
-
- ///
- /// AES解密字符串
- ///
- ///
- ///
- ///
- ///
- 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
- {
- ///
- /// 需要加密/解密的数据
- ///
- public byte[] Data { get; set; }
-
- ///
- /// 密钥
- ///
- public string Key { get; set; }
-
- ///
- /// 向量
- ///
- public string IV { get; set; }
-
- ///
- /// 加密模式
- ///
- public CipherMode Mode { get; set; }
-
- ///
- /// 填充模式
- ///
- 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;
- }
实现效果:
由简入繁,拿来即用
更多精彩,请搜索公 Z 号:Csharp 小记