說完了對稱加密中的AES,這一篇再來介紹下DES。
加解密原理什么的就不介紹了,大家可以自行百度(主要我也不太明白,也不需要太明白),大致說一下兩者的區(qū)別吧!
首先肯定是加密算法的不同,嗯,這一句完全是廢話,不同的加密方式算法肯定不會一樣。
AES比DES的運算速度更快。
AES比DES破解難度高,相對比較安全。
代碼實現(xiàn)的話其實AES和DES的寫法是基本差不多的,但是這里有一個參數(shù)問題還是需要提一下。在上一篇AES中我們有介紹過密鑰長度只能是16、24、32;在DES中的話,密鑰(Key)的長度是8(由于密鑰的長度問題,所以這里如果使用暴力破解的話,還是有很高的概率可以還原數(shù)據(jù)),有向量(IV)的話,長度一般也是8;然后就是在AES中我們采用的是Rijndael,在這里我們換成DESCryptoServiceProvider;兩個加密的實現(xiàn)代碼主要就是這里的不同,其他基本一模一樣,所以稍微改一下即可。
實現(xiàn)功能:
開發(fā)環(huán)境:
開發(fā)工具:Visual Studio 2013
.NET Framework版本:4.5
實現(xiàn)代碼:
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;
}
實現(xiàn)效果:
該文章在 2023/2/27 10:24:45 編輯過