DES的原始思想可以参照二战德国的恩尼格玛机,其基本思想大致相同。传统的密码加密都是由古代的循环移位思想而来,恩尼格玛机在这个基础之上进行了扩散模糊。现代DES在二进制级别做着同样的事:替代模糊,增加分析的难度。
加解密
using System.Security.Cryptography;
private static byte[] DESKey = new byte[] {11,23,93,102,72,41,18,12};
private static byte[] DESIV = new byte[] {75,158,46,97,78,57,17,36};
加密函数:
public static string Encode(string Encode_String)
{
DESCryptoserviceProvider objDES = new DESCryptoserviceProvider();
MemoryStream objMemoryStream = new MemoryStream();
Cryptostream objCryptostream = new Cryptostream
(objMemoryStream,objDES.CreateEncryptor(DESKey,DESIV),CryptostreamMode.Write);
StreamWriter objStreamWriter = new StreamWriter(objCryptostream);
objStreamWriter.Write(Encode_String);
objStreamWriter.Flush();
objCryptostream.FlushFinalBlock();
objMemoryStream.Flush();
return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)
objMemoryStream.Length);
}
//解密函数
public static string Decode(string Encode_String)
{
DESCryptoserviceProvider objDES = new DESCryptoserviceProvider();
byte[] Input = Convert.FromBase64String(Encode_String);
MemoryStream objMemoryStream = new MemoryStream(Input);
Cryptostream objCryptostream = new Cryptostream
(objMemoryStream,objDES.CreateDecryptor(DESKey,DESIV),CryptostreamMode.Read);
StreamReader objStreamReader = new StreamReader(objCryptostream);
return objStreamReader.ReadToEnd();
}