说完了对称加密中的AES,这一篇再来介绍下DES。
加解密原理什么的就不介绍了,大家可以自行百度(主要我也不太明白,也不需要太明白),大致说一下两者的区别吧!
首先肯定是加密算法的不同,嗯,这一句完全是废话,不同的加密方式算法肯定不会一样。
AES比DES的运算速度更快。
AES比DES破解难度高,相对比较安全。
代码实现的话其实AES和DES的写法是基本差不多的,但是这里有一个参数问题还是需要提一下。在上一篇AES中我们有介绍过密钥长度只能是16、24、32;在DES中的话,密钥(Key)的长度是8(由于密钥的长度问题,所以这里如果使用暴力破解的话,还是有很高的概率可以还原数据),有向量(IV)的话,长度一般也是8;然后就是在AES中我们采用的是Rijndael,在这里我们换成DESCryptoServiceProvider;两个加密的实现代码主要就是这里的不同,其他基本一模一样,所以稍微改一下即可。
实现功能:
开发环境:
开发工具:Visual Studio 2013
.NET Framework版本:4.5
实现代码:
public class DesUtil
{
public static byte[] Encrypt(DesModel desModel)
{
byte[] key8 = new byte[8];
byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length));
Array.Copy(byteKey, key8, key8.Length);
byte[] iv8 = new byte[8];
byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length));
Array.Copy(byteIv, iv8, iv8.Length);
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
desProvider.Mode = desModel.Mode;
desProvider.Padding = desModel.Padding;
desProvider.Key = key8;
desProvider.IV = iv8;
byte[] result = null;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream EncryptStream = new CryptoStream(ms, desProvider.createEncryptor(), CryptoStreamMode.Write))
{
EncryptStream.Write(desModel.Data, 0, desModel.Data.Length);
EncryptStream.FlushFinalBlock();
result = ms.ToArray();
}
}
}
catch { }
return result;
}
public static byte[] Decrypt(DesModel desModel)
{
byte[] key8 = new byte[8];
byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length));
Array.Copy(byteKey, key8, key8.Length);
byte[] iv8 = new byte[8];
byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length));
Array.Copy(byteIv, iv8, iv8.Length);
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
desProvider.Mode = desModel.Mode;
desProvider.Padding = desModel.Padding;
desProvider.Key = key8;
desProvider.IV = iv8;
byte[] result = null;
try
{
using (MemoryStream ms = new MemoryStream(desModel.Data))
{
using (CryptoStream DecryptStream = new CryptoStream(ms, desProvider.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;
}
public static string Encrypt(string data, string key, string iv = "")
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] result = Encrypt(new DesModel
{
Data = bytes,
Key = key,
IV = iv,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
});
if (result == null)
{
return "";
}
return Convert.ToBase64String(result);
}
public static string Decrypt(string data, string key, string iv = "")
{
byte[] bytes = Convert.fromBase64String(data);
byte[] result = Decrypt(new DesModel
{
Data = bytes,
Key = key,
IV = iv,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
});
if (result == null)
{
return "";
}
return Encoding.UTF8.GetString(result);
}
public class DesModel
{
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_Des_Encrypt_Click(object sender, EventArgs e)
{
string result = DesUtil.Encrypt(textBox1.Text, "12345678900987654321");
textBox2.Text = result;
}
private void btn_Des_Decrypt_Click(object sender, EventArgs e)
{
string result = DesUtil.Decrypt(textBox2.Text, "12345678900987654321");
textBox1.Text = result;
}
实现效果:
该文章在 2023/2/27 10:24:45 编辑过