public class XMLFileEncryptDecrypt
{
public string Encrypt(string pToEncrypt, string str)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(str);
des.IV = ASCIIEncoding.ASCII.GetBytes(str);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string strRet = Convert.ToBase64String(ms.ToArray());
ms.Close();
return strRet;
}
public string Decrypt(string pToDecrypt, string str)
{
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(str);
des.IV = ASCIIEncoding.ASCII.GetBytes(str);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string strRet = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strRet;
}
}
C#: 文件加密解密
最新推荐文章于 2024-07-18 08:56:36 发布