DES加密/解密的应用

时间:2021-04-16 20:42:06
加解密
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();
}