- namespace winform
- {
- class useCBC
- {
- ///输入“input”字符串(密文)、“bykey”字节型密钥、“IV”字节型初始向量
- ///输出BASE64编码的明文字符串
- ///解密函数
- public static string DecryptString(string input, byte[] byKey, byte[] IV)
- {
- if (input.Equals(string.Empty))
- {
- return input;
- }
- byte[] inputByteArray = new Byte[input.Length];
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- inputByteArray = Convert.FromBase64String(input);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- Encoding encoding = new UTF8Encoding();
- return encoding.GetString(ms.ToArray());
- }
- ///输入“input”字符串(明文)、“bykey”字节型密钥、“IV”字节型初始向量
- ///输出BASE64编码的密文字符串
- ///加密函数
- public static string EncryptString(string input, byte[] byKey, byte[] IV)
- {
- if (input.Equals(string.Empty))
- {
- return input;
- }
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return Convert.ToBase64String(ms.ToArray());
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2107201513175.html
来源: http://www.codesnippet.cn/detail/2107201513175.html