C#加密解密字符串方法
首先在web.config | app.config 文件下增加如下代码:
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;///
///构造函数
///
public CryptoHelper()
{
IV = ConfigurationManager.AppSettings["IV"];
Key = ConfigurationManager.AppSettings["Key"];
}
}
注意添加System.Configuration.dll程序集引用。
在获得了IV 和Key 之后,需要获取提供加密服务的Service 类。
在这里,使用的是System.Security.Cryptography; 命名空间下的TripleDESCryptoServiceProvider类。
获取TripleDESCryptoServiceProvider 的方法如下:
///
/// 获取加密服务类
///
///
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 进行转换。
完整的加密字符串代码如下:
///
/// 获取加密后的字符串
///
/// 输入值.
///
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);
}
解密方法也类似:
///
/// 获取解密后的值
///
/// 经过加密后的字符串.
///
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"];
}///
/// 获取加密后的字符串
///
/// 输入值.
///
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);
}///
/// 获取加密服务类
///
///
private TripleDESCryptoServiceProvider GetCryptoProvider()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();provider.IV = Convert.FromBase64String(IV);
provider.Key = Convert.FromBase64String(Key);return provider;
}
///
/// 获取解密后的值
///
/// 经过加密后的字符串.
///
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());
}
}
}
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔