Asp.net,C# 加密解密字符串 |
首先在web.config | app.config 文件下增加如下代码: <?xml version="1.0"?> <configuration> <appSettings> <add key="IV" value="SuFjcEmp/TE="/> <add key="Key" value="KIPSToILGp6fl+3gXJvMsN4IajizYBBT"/> </appSettings> </configuration> IV:加密算法的初始向量 。 Key:加密算法的密钥 。 接着新建类CryptoHelper,作为加密协助类 。 首先要从配置文件中得到IV 和Key.所以 根本代码如下: public class CryptoHelper { //private readonly string IV = "SuFjcEmp/TE="; private readonly string IV = string.Empty; //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT"; private readonly string Key = string.Empty; /// <summary> /// 构造函数 /// </summary> public CryptoHelper() { IV = ConfigurationManager.AppSettings["IV"]; Key = ConfigurationManager.AppSettings["Key"]; } } 留神增加System.Configuration.dll程序集 引用 。 在 获得了IV 和Key 之后,需求猎取提供加密服务的Service 类 。 在这里, 使用的是System.Security.Cryptography; 命名空间下的TripleDESCryptoServiceProvider类 。 猎取TripleDESCryptoServiceProvider 的 步骤如下: /// <summary> /// 猎取加密服务类 /// </summary> /// <returns></returns> private TripleDESCryptoServiceProvider GetCryptoProvider() { TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); provider.IV = Convert.FromBase64String(IV); provider.Key = Convert.FromBase64String(Key); return provider; } TripleDESCryptoServiceProvider 两个有用的 步骤 CreateEncryptor: 缔造对称加密器对象ICryptoTransform. CreateDecryptor: 缔造对称解密器对象ICryptoTransform 加密器对象和解密器对象 可以被CryptoStream对象 使用 。来对流进行加密和解密 。 cryptoStream 的 构造函数如下: public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode); 使用transform 对象对stream 进行转换 。 完全的加密字符串代码如下: /// <summary> /// 猎取加密后的字符串 /// </summary> /// <param name="inputValue">输入值 。</param> /// <returns></returns> public string GetEncryptedValue(string inputValue) { TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); // 缔造内存流来 保留加密后的流 MemoryStream mStream = new MemoryStream(); // 缔造加密转换流 CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(), CryptoStreamMode.Write); // 使用UTF8编码猎取输入字符串的字节 。 byte[] toEncrypt = new UTF8Encoding() 。GetBytes(inputValue); // 将字节写到转换流里面去 。 cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // 在调用转换流的FlushFinalBlock 步骤后,内部就会进行转换了,此时mStream便是加密后的流了 。 byte[] ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); //将加密后的字节进行64编码 。 return Convert.ToBase64String(ret); } 解密 步骤也 类似: /// <summary> /// 猎取解密后的值 /// </summary> /// <param name="inputValue"> 通过加密后的字符串 。</param> /// <returns></returns> public string GetDecryptedValue(string inputValue) { TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); byte[] inputEquivalent = Convert.FromBase64String(inputValue); // 缔造内存流 保留解密后的数据 MemoryStream msDecrypt = new MemoryStream(); // 缔造转换流 。 CryptoStream csDecrypt = new CryptoStream(msDecrypt, provider.CreateDecryptor(), CryptoStreamMode.Write); csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length); csDecrypt.FlushFinalBlock(); csDecrypt.Close(); //猎取字符串 。 return new UTF8Encoding() 。GetString(msDecrypt.ToArray()); } 完全的CryptoHelper代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; using System.Configuration; namespace WindowsFormsApplication1 { public class CryptoHelper { //private readonly string IV = "SuFjcEmp/TE="; private readonly string IV = string.Empty; //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT"; private readonly string Key = string.Empty; public CryptoHelper() { IV = ConfigurationManager.AppSettings["IV"]; Key = ConfigurationManager.AppSettings["Key"]; } /// <summary> /// 猎取加密后的字符串 /// </summary> /// <param name="inputValue">输入值 。</param> /// <returns></returns> public string GetEncryptedValue(string inputValue) { TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); // 缔造内存流来 保留加密后的流 MemoryStream mStream = new MemoryStream(); // 缔造加密转换流 CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(), CryptoStreamMode.Write); // 使用UTF8编码猎取输入字符串的字节 。 byte[] toEncrypt = new UTF8Encoding() 。GetBytes(inputValue); // 将字节写到转换流里面去 。 cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // 在调用转换流的FlushFinalBlock 步骤后,内部就会进行转换了,此时mStream便是加密后的流了 。 byte[] ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); //将加密后的字节进行64编码 。 return Convert.ToBase64String(ret); } /// <summary> /// 猎取加密服务类 /// </summary> /// <returns></returns> private TripleDESCryptoServiceProvider GetCryptoProvider() { TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); provider.IV = Convert.FromBase64String(IV); provider.Key = Convert.FromBase64String(Key); return provider; } /// <summary> /// 猎取解密后的值 /// </summary> /// <param name="inputValue"> 通过加密后的字符串 。</param> /// <returns></returns> public string GetDecryptedValue(string inputValue) { TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); byte[] inputEquivalent = Convert.FromBase64String(inputValue); // 缔造内存流 保留解密后的数据 MemoryStream msDecrypt = new MemoryStream(); // 缔造转换流 。 CryptoStream csDecrypt = new CryptoStream(msDecrypt, provider.CreateDecryptor(), CryptoStreamMode.Write); csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length); csDecrypt.FlushFinalBlock(); csDecrypt.Close(); //猎取字符串 。 return new UTF8Encoding() 。GetString(msDecrypt.ToArray()); } } } 使用例子:
|