首先在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());
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2553893.html