Code
public class TRIP3DES
{
//密钥
private string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
//矢量,矢量可以为空
private string sIV = "qcDY6X+aPLw=";
//构造一个对称算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
public TRIP3DES(string _sKey,string _sIV)
{
sKey=_sKey;
sIV=_sIV;
}
#region public string EncryptString(string Value)
///
/// 加密字符串
///
/// 输入的字符串
/// 加密后的字符串
public string EncryptString(string Value)
{
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());
//return Convert.ToChar(ms.ToArray()).ToString();
string strResult="";
byte[] b=ms.ToArray();
for(int i=0;i<b.Length;i++)
{
strResult += b[i].ToString("x").PadLeft(2,'0');
}
return strResult;
}
#endregion
#region public string DecryptString(string Value)
///
/// 解密字符串
///
/// 加过密的字符串
/// 解密后的字符串
public string DecryptString(string Value)
{
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.CreateDecryptor(mCSP.Key, mCSP.IV);
//byt = Convert.FromBase64String(Value);
byt=new byte[Value.Length/2];
int bi=0;
for (int i=0;i<Value.Length;i+=2)
{
byt[bi]= (byte)Int32.Parse(Value.Substring(i,2),System.Globalization.NumberStyles.HexNumber);
bi++;
}
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());
}
#endregion
}
Codepublic class TRIP3DES
{
//密钥
private string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
//矢量,矢量可以为空
private string sIV = "qcDY6X+aPLw=";
//构造一个对称算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
public TRIP3DES(string _sKey,string _sIV)
{
sKey=_sKey;
sIV=_sIV;
}
#region public string EncryptString(string Value)
///
/// 加密字符串
///
/// 输入的字符串
/// 加密后的字符串
public string EncryptString(string Value)
{
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());
//return Convert.ToChar(ms.ToArray()).ToString();
string strResult="";
byte[] b=ms.ToArray();
for(int i=0;i<b.Length;i++)
{
strResult += b[i].ToString("x").PadLeft(2,'0');
}
return strResult;
}
#endregion
#region public string DecryptString(string Value)
///
/// 解密字符串
///
/// 加过密的字符串
/// 解密后的字符串
public string DecryptString(string Value)
{
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.CreateDecryptor(mCSP.Key, mCSP.IV);
//byt = Convert.FromBase64String(Value);
byt=new byte[Value.Length/2];
int bi=0;
for (int i=0;i<Value.Length;i+=2)
{
byt[bi]= (byte)Int32.Parse(Value.Substring(i,2),System.Globalization.NumberStyles.HexNumber);
bi++;
}
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());
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
public partial class Default3 : System.Web.UI.Page
{
//密钥
private const string sKey = "5tpMC2dovLDHzt/pRr/TOGSJPXANLG51";
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write(EncryptString("aabbcc"));
//Response.Write("<BR>");
//Response.Write(DecryptString(EncryptString("aabbcc")));
//string transDesKey = "L/Lcp55e4H8OMelF1nw9yMhhpFvymI9b";
//Encrypt3DES test = new Encrypt3DES(transDesKey);
//Response.Write(EncryptString("aabbcc"));
//test.EncryptString("aabbcc");
}
/// <summary>
/// 加密
/// </summary>
/// <param name="encryptValue"></param>
/// <returns></returns>
private string EncryptString(string encryptValue)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = Convert.FromBase64String(sKey);
des.Mode = CipherMode.ECB;
byte[] valBytes = Encoding.Unicode.GetBytes(encryptValue);
ICryptoTransform transform = des.CreateEncryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
cs.Write(valBytes, 0, valBytes.Length);
cs.FlushFinalBlock();
byte[] returnBytes = ms.ToArray();
cs.Close();
return Convert.ToBase64String(returnBytes);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="encryptedValue"></param>
/// <returns></returns>
private string DecryptString(string encryptedValue)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = Convert.FromBase64String(sKey);
des.Mode = CipherMode.ECB;
byte[] valBytes = Convert.FromBase64String(encryptedValue);
ICryptoTransform transform = des.CreateDecryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
cs.Write(valBytes, 0, valBytes.Length);
cs.FlushFinalBlock();
byte[] returnBytes = ms.ToArray();
cs.Close();
return Encoding.Unicode.GetString(returnBytes);
}
//private string ByteToHex(byte[] byteArray)
//{
// string outString = "";
// foreach (Byte b in byteArray)
// outString += b.ToString("X2");
// return outString;
//}
//private byte[] HexToByte(string hexString)
//{
// byte[] returnBytes = new byte[hexString.Length / 2];
// for (int i = 0; i < returnBytes.Length; i++)
// returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
// return returnBytes;
//}
}
另一种: