C#的3DES加密解密算法实例代码
c#类如下:
using system;
using system.collections.generic;
using system.text;
using system.security.cryptography;
using system.io;
namespace consoleapplication1
{
/// <summary>
/// 加解密类
/// </summary>
public class encrypthelper
{
//构造一个对称算法
private symmetricalgorithm mcsp = new tripledescryptoserviceprovider();
#region 加密解密函数
/// <summary>
/// 字符串的加密
/// </summary>
/// <param name="value">要加密的字符串</param>
/// <param name="skey">密钥,必须32位</param>
/// <param name="siv">向量,必须是12个字符</param>
/// <returns>加密后的字符串</returns>
public string encryptstring(string value, string skey,string siv)
{
try
{
icryptotransform ct;
memorystream ms;
cryptostream cs;
byte[] byt;
mcsp.key = convert.frombase64string(skey);
mcsp.iv = convert.frombase64string(siv);
//指定加密的运算模式
mcsp.mode = system.security.cryptography.ciphermode.ecb;
//获取或设置加密算法的填充模式
mcsp.padding = system.security.cryptography.paddingmode.pkcs7;
ct = mcsp.createencryptor(mcsp.key, mcsp.iv);//创建加密对象
byt = encoding.utf8.getbytes(value);
ms = new memorystream();
cs = new cryptostream(ms, ct, cryptostreammode.write);
cs.write(byt, 0, byt.length);
cs.flushfinalblock();
cs.close();
return convert.tobase64string(ms.toarray());
}
catch (exception ex)
{
//messagebox.show(ex.message, "出现异常", messageboxbuttons.ok, messageboxicon.warning);
return ("error in encrypting " + ex.message);
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="value">加密后的字符串</param>
/// <param name="skey">密钥,必须32位</param>
/// <param name="siv">向量,必须是12个字符</param>
/// <returns>解密后的字符串</returns>
public string decryptstring(string value, string skey, string siv)
{
try
{
icryptotransform ct;//加密转换运算
memorystream ms;//内存流
cryptostream cs;//数据流连接到数据加密转换的流
byte[] byt;
//将3des的密钥转换成byte
mcsp.key = convert.frombase64string(skey);
//将3des的向量转换成byte
mcsp.iv = convert.frombase64string(siv);
mcsp.mode = system.security.cryptography.ciphermode.ecb;
mcsp.padding = system.security.cryptography.paddingmode.pkcs7;
ct = mcsp.createdecryptor(mcsp.key, mcsp.iv);//创建对称解密对象
byt = convert.frombase64string(value);
ms = new memorystream();
cs = new cryptostream(ms, ct, cryptostreammode.write);
cs.write(byt, 0, byt.length);
cs.flushfinalblock();
cs.close();
return encoding.utf8.getstring(ms.toarray());
}
catch (exception ex)
{
//messagebox.show(ex.message, "出现异常", messageboxbuttons.ok, messageboxicon.warning);
return ("error in decrypting " + ex.message);
}
}
#endregion
}
}
调用方法如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
encrypthelper helper = new encrypthelper();
//加密
string oldvalue = "13800138000";
//加密后结果
//密钥,必须32位
string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm5";
//向量,必须是12个字符
string siv = "andyliu1234=";
//print
string newvalue = helper.encryptstring(oldvalue,skey,siv);
console.writeline("加密后:"+ newvalue);
//解密
string desvalue = helper.decryptstring(newvalue,skey,siv);
//
console.writeline("解密后:"+ desvalue);
console.readline();
}
}
}