加解密
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();
}
DES加密/解密的应用
最新推荐文章于 2023-06-16 18:16:39 发布