string key = "hdyxsjtd";//密匙8个字符
//加密
public string JiaMi(string sourceString)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(sourceString);
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
crypStream.Write(inputByteArray, 0, inputByteArray.Length);
crypStream.FlushFinalBlock();
return Convert.ToBase64String(memStream.ToArray());
}
//解密的函数
public string JieMi(string encryptString)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(encryptString);
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
crypStream.Write(inputByteArray, 0, inputByteArray.Length);
crypStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memStream.ToArray());
}