C# DES 加密解密
2014-08-20 15:53
232 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; namespace MTR.Library.CommonHelper { public class DESEncryptHelper { /// <summary> /// Encrypt string /// </summary> /// <param name="value">The string which to be encrypted.</param> /// <returns>The value after being encrypted.</returns> public static string Encrypt(string value) { return Encrypt(value, System.Configuration.ConfigurationManager.AppSettings["pwd"]); } /// <summary> /// Encrypt string /// </summary> /// <param name="value">The string which to be encrypted.</param> /// <param name="password">The password.</param> /// <returns>The value after being encrypted.</returns> public static string Encrypt(string value, string password) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(password.Substring(0, 8)); byte[] rgbIV = Encoding.UTF8.GetBytes(password.Substring(0, 8)); byte[] inputByteArray = Encoding.UTF8.GetBytes(value); using (DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider()) { using (MemoryStream mStream = new MemoryStream()) { CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } } } catch (Exception ex) { throw new Exception("Error when encrypting data", ex); } } /// <summary> /// Decrypt string /// </summary> /// <param name="value">The string which to be decrypted.</param> /// <returns>The value after being decrypted.</returns> public static string Decrypt(string value) { return Decrypt(value, System.Configuration.ConfigurationManager.AppSettings["pwd"]); } /// <summary> /// Decrypt string /// </summary> /// <param name="value">The string which to be decrypted.</param> /// <param name="password">The password.</param> /// <returns>The value after being decrypted.</returns> public static string Decrypt(string value, string password) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(password.Substring(0, 8)); byte[] rgbIV = Encoding.UTF8.GetBytes(password.Substring(0, 8)); byte[] inputByteArray = Convert.FromBase64String(value); using (DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider()) { using (MemoryStream mStream = new MemoryStream()) { CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } } } catch (Exception ex) { throw new Exception("Error when decrypting data", ex); } } } }
View Code
相关文章推荐
- C#中使用DES和AES加密解密
- 黄聪:C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- 【转】用 Java 解密 C# 加密的数据(DES)
- Asp.Net C#中除了MD5加密之外还经常用到了AES加密、解密与DES加密、解密,本次先介绍DES的加密及解密。注意的是DES的密匙是8位的。
- C# 的 DES 加密解密
- C#DES对较大文件和文件字节数组对象加密解密
- C#字符串的加密与解密(DES和TripleDES算法的加密解密过程)
- C#中使用DES和AES加密解密
- C#中使用DES和AES加密解密
- C# DES 加密/解密,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- [网络收集]C# 各种加密,解密算法【MD5,DES,线性,卷帘】
- [读书笔记]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- Java加密和C#解密=>DES方法
- c# des 加密解密
- [转]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- C#中使用DES和AES加密解密
- DES的两个简单的加密、解密函数(C#中使用)
- .Net(c#)加密解密之Aes和Des
- C# 與JAVA 共用的DES 加密解密方法
- C#中使用DES和AES加密解密