TrippleDESCSPEncrypt 加密解密试试看
2015-06-23 09:19
309 查看
public class TrippleDESCSPEncrypt { //12个字符 private static string customIV = "4vHKRj3yfzU="; //32个字符 private static string customKey = "xhVs6DRXLfUGxw+AhtfQdpQGoa+8SA9d"; /// <summary> /// 加密字符串 /// </summary> /// <param name="password"></param> /// <returns></returns> public string EncryptPassword(string password) { string encryptPassword = string.Empty; SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider(); algorithm.Key = Convert.FromBase64String(customKey); algorithm.IV = Convert.FromBase64String(customIV);//在ECB模式下,IV不起作用 algorithm.Mode = CipherMode.ECB; algorithm.Padding = PaddingMode.PKCS7; ICryptoTransform transform = algorithm.CreateEncryptor(); byte[] data = (new System.Text.ASCIIEncoding()).GetBytes(password); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); encryptPassword = Convert.ToBase64String(memoryStream.ToArray()); memoryStream.Close(); cryptoStream.Close(); return encryptPassword; } /// <summary> /// 解密字符串 /// </summary> /// <param name="password"></param> /// <returns></returns> public string DecryptPassword(string password) { string decryptPassword = string.Empty; SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider(); algorithm.Key = Convert.FromBase64String(customKey); algorithm.IV = Convert.FromBase64String(customIV);//在ECB模式下不起作用 algorithm.Mode = CipherMode.ECB; algorithm.Padding = PaddingMode.PKCS7; ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV); //ICryptoTransform transform = algorithm.CreateDecryptor();//在ECB模式下也可以使用此句。 byte[] buffer = Convert.FromBase64String(password); MemoryStream memoryStream = new MemoryStream(buffer); CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream, System.Text.Encoding.ASCII); decryptPassword = reader.ReadToEnd(); reader.Close(); cryptoStream.Close(); memoryStream.Close(); return decryptPassword; }
相关文章推荐
- ecshop退款订单原理分析
- 你的灯还亮着吗?阅读笔记之三
- 浅析人脸检测之Haar分类器方法
- Effective C++ 条款11
- openlayer vectorlayer不显示
- 第二阶段第三天站立会议总结
- Java Service Wrapper2-构建自己的Wrapper应用
- 计算机视觉目标检测的框架与过程
- .NET逻辑分层架构总结
- 编译原理文法知识通俗趣味理解
- 构建iOS交叉编译环境
- 利用编译器Bug的可抵赖后门
- Exchange 日常管理七之:客户端访问服务器高可用部署
- OpenCV之摄像头捕捉图像
- POJ 3311 Hie with the Pie
- 基于Android的拍照上传程序开发
- 致创业者:APP已死 服务永生
- Redis数据库的应用场景介绍
- C++引用的用法
- Exploring Angular 1.3: Binding to Directive Controllers