首先在nfig | nfig 文件下增加如下代碼
<?xml version=
<configuration>
<appSettings>
<add key=
<add key=
</appSettings>
</configuration>
IV:加密算法的初始向量
Key:加密算法的密鑰
接著新建類CryptoHelper
首先要從配置文件中得到IV 和Key
public class CryptoHelper
{
//private readonly string IV =
private readonly string IV = string
//private readonly string Key =
private readonly string Key = string
/// <summary>
///構造函數
/// </summary>
public CryptoHelper()
{
IV = ConfigurationManager
Key = ConfigurationManager
}
}
注意添加System
在獲得了IV 和Key 之後
在這裡
獲取TripleDESCryptoServiceProvider 的方法如下
/// <summary>
/// 獲取加密服務類
/// </summary>
/// <returns></returns>
private TripleDESCryptoServiceProvider GetCryptoProvider()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider()
provider
provider
return provider;
}
TripleDESCryptoServiceProvider 兩個有用的方法
CreateEncryptor:創建對稱加密器對象ICryptoTransform
CreateDecryptor:創建對稱解密器對象ICryptoTransform
加密器對象和解密器對象可以被CryptoStream對象使用
cryptoStream 的構造函數如下
public CryptoStream(Stream stream
完整的加密字符串代碼如下
/// <summary>
/// 獲取加密後的字符串
/// </summary>
/// <param name=
/// <returns></returns>
public string GetEncryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this
// 創建內存流來保存加密後的流
MemoryStream mStream = new MemoryStream()
// 創建加密轉換流
CryptoStream cStream = new CryptoStream(mStream
provider
// 使用UTF
byte[] toEncrypt = new UTF
// 將字節寫到轉換流裡面去
cStream
cStream
// 在調用轉換流的FlushFinalBlock方法後
byte[] ret = mStream
// Close the streams
cStream
mStream
//將加密後的字節進行
return Convert
}
解密方法也類似
/// <summary>
/// 獲取解密後的值
/// </summary>
/// <param name=
/// <returns></returns>
public string GetDecryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this
byte[] inputEquivalent = Convert
// 創建內存流保存解密後的數據
MemoryStream msDecrypt = new MemoryStream()
// 創建轉換流
CryptoStream csDecrypt = new CryptoStream(msDecrypt
provider
CryptoStreamMode
csDecrypt
csDecrypt
csDecrypt
//獲取字符串
return new UTF
}
完整的CryptoHelper代碼如下
using System;
using System
using System
using System
using System
using System
using System
namespace WindowsFormsApplication
{
public class CryptoHelper
{
//private readonly string IV =
private readonly string IV = string
//private readonly string Key =
private readonly string Key = string
public CryptoHelper()
{
IV = ConfigurationManager
Key = ConfigurationManager
}
/// <summary>
/// 獲取加密後的字符串
/// </summary>
/// <param name=
/// <returns></returns>
public string GetEncryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this
// 創建內存流來保存加密後的流
MemoryStream mStream = new MemoryStream()
// 創建加密轉換流
CryptoStream cStream = new CryptoStream(mStream
provider
// 使用UTF
byte[] toEncrypt = new UTF
// 將字節寫到轉換流裡面去
cStream
cStream
// 在調用轉換流的FlushFinalBlock方法後
byte[] ret = mStream
// Close the streams
cStream
mStream
//將加密後的字節進行
return Convert
}
/// <summary>
/// 獲取加密服務類
/// </summary>
/// <returns></returns>
private TripleDESCryptoServiceProvider GetCryptoProvider()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider()
provider
provider
return provider;
}
/// <summary>
/// 獲取解密後的值
/// </summary>
/// <param name=
/// <returns></returns>
public string GetDecryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this
byte[] inputEquivalent = Convert
// 創建內存流保存解密後的數據
MemoryStream msDecrypt = new MemoryStream()
// 創建轉換流
CryptoStream csDecrypt = new CryptoStream(msDecrypt
provider
CryptoStreamMode
csDecrypt
csDecrypt
csDecrypt
//獲取字符串
return new UTF
}
}
}
使用例子
From:http://tw.wingwit.com/Article/program/net/201311/12266.html