[Utility.sln]根据MSDN上的DESCryptoServiceProvider 类 尝试写的对称密钥加密原代码[可应用于安全级别不高的数据连接和密码保护]
自己份源程序.^^
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO;
namespace Utility
{
/// <summary>
/// The DSAPI wrapper
/// 安全加密算法用于日后扩展
/// To be released as part of the Microsoft Configuration Building Block
/// </summary>
public class DataProtector
{
//private SymmetricAlgorithm mCSP;
/// <summary>
/// 创建一个对称加密算法的基类
/// </summary>
private System.Security.Cryptography.SymmetricAlgorithm mCSP;
/// <summary>
/// keyOne
/// </summary>
private string _KeyGen ="qA3cLz8Y9Xw=";
/// <summary>
/// keyTwo
/// </summary>
private string _IVGen ="uHnaoO/MEDw=";
/// <summary>
/// 生成要用于此算法的随机初始化向量,返回string
/// </summary>
/// <returns></returns>
public string GenerateKeyGen()
{
mCSP = SetEnc();
mCSP.GenerateKey();
return Convert.ToBase64String(mCSP.Key);
}
/// <summary>
/// 生成用于此算法的随机 Key,返回string
/// </summary>
/// <returns></returns>
public string GenerateIVGen()
{
mCSP.GenerateIV();
return Convert.ToBase64String(mCSP.IV);
}
/// <summary>
/// 加密算法选择,本程序默认选择DESCryptoServiceProvider进行加密
/// </summary>
/// <returns></returns>
private SymmetricAlgorithm SetEnc()
{
return new DESCryptoServiceProvider();
// return new DESCryptoServiceProvider();
// return new TripleDESCryptoServiceProvider();
}
/// <summary>
/// 加密(通过本类的_KeyGen,_IVGen进行对称密钥)
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key=Convert.FromBase64String(this._KeyGen);
mCSP.IV=Convert.FromBase64String(this._IVGen);
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
//ct=mCSP.CreateEncryptor();
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());
}
/// <summary>
/// 解密
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key=Convert.FromBase64String(this._KeyGen);
mCSP.IV=Convert.FromBase64String(this._IVGen);
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());
}
}
}
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO;
namespace Utility
{
/// <summary>
/// The DSAPI wrapper
/// 安全加密算法用于日后扩展
/// To be released as part of the Microsoft Configuration Building Block
/// </summary>
public class DataProtector
{
//private SymmetricAlgorithm mCSP;
/// <summary>
/// 创建一个对称加密算法的基类
/// </summary>
private System.Security.Cryptography.SymmetricAlgorithm mCSP;
/// <summary>
/// keyOne
/// </summary>
private string _KeyGen ="qA3cLz8Y9Xw=";
/// <summary>
/// keyTwo
/// </summary>
private string _IVGen ="uHnaoO/MEDw=";
/// <summary>
/// 生成要用于此算法的随机初始化向量,返回string
/// </summary>
/// <returns></returns>
public string GenerateKeyGen()
{
mCSP = SetEnc();
mCSP.GenerateKey();
return Convert.ToBase64String(mCSP.Key);
}
/// <summary>
/// 生成用于此算法的随机 Key,返回string
/// </summary>
/// <returns></returns>
public string GenerateIVGen()
{
mCSP.GenerateIV();
return Convert.ToBase64String(mCSP.IV);
}
/// <summary>
/// 加密算法选择,本程序默认选择DESCryptoServiceProvider进行加密
/// </summary>
/// <returns></returns>
private SymmetricAlgorithm SetEnc()
{
return new DESCryptoServiceProvider();
// return new DESCryptoServiceProvider();
// return new TripleDESCryptoServiceProvider();
}
/// <summary>
/// 加密(通过本类的_KeyGen,_IVGen进行对称密钥)
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key=Convert.FromBase64String(this._KeyGen);
mCSP.IV=Convert.FromBase64String(this._IVGen);
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
//ct=mCSP.CreateEncryptor();
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());
}
/// <summary>
/// 解密
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key=Convert.FromBase64String(this._KeyGen);
mCSP.IV=Convert.FromBase64String(this._IVGen);
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());
}
}
}
本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/04/03/131246.html