您的位置:首页 > 编程语言 > C#

C# des 加密 解密

2010-05-17 11:11 281 查看
代码

public class EncryptDecrypt
{
public static string EncryptString(string mes, string key)
{
byte[] inputBytes = ASCIIEncoding.UTF8.GetBytes(mes);

MemoryStream outputStream = new MemoryStream();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(outputStream, desencrypt, CryptoStreamMode.Write);
cryptostream.Write(inputBytes, 0, inputBytes.Length);
cryptostream.FlushFinalBlock();
cryptostream.Close();

string outputString = Convert.ToBase64String(outputStream.ToArray());
return outputString;
}

public static string DecryptString(string mes, string key)
{
byte[] inputBytes = Convert.FromBase64String(mes);

MemoryStream outputStream = new MemoryStream();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
ICryptoTransform desencrypt = des.CreateDecryptor();
CryptoStream cryptostream = new CryptoStream(outputStream, desencrypt, CryptoStreamMode.Write);
cryptostream.Write(inputBytes, 0, inputBytes.Length);
cryptostream.FlushFinalBlock();
cryptostream.Close();

string outputString = ASCIIEncoding.UTF8.GetString(outputStream.ToArray());
return outputString;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: