5 代码实例:调用常用加密算法的实例
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class EncryptClass
{
private string mKey = string.Empty;
private ServiceProviderEnum mAlgorithm;
private SymmetricAlgorithm mCryptoService;
private void SetLegalIV()
{
//对称算法的初始化向量
//对于DES, TripleDES, RC2向量的长度为8字节
//对于Rijndael算法,向量长度为16字节
//可以人工输入,也可以随机生成,方法是:des.GenerateIV();
//在这儿为了简单起见,我们使用固定的值,
//也可以按照调整key的办法来调整向量。
switch(mAlgorithm)
{
case ServiceProviderEnum.Rijndael:
mCryptoService.IV = new byte[] {0xf, 0x6f, 0x13, 0x2e, 0x35, 0xc2, 0xcd, 0xf9, 0x5, 0x46, 0x9c, 0xea, 0xa8, 0x4b, 0x73,0xcc};
break;
default:
mCryptoService.IV = new byte[] {0xf, 0x6f, 0x13, 0x2e, 0x35, 0xc2, 0xcd, 0xf9};
break;
}
}
public enum ServiceProviderEnum: int
{
// .Net Framework1.1中支持的对称加密算法
Rijndael,
RC2,
DES,
TripleDES
}
//加密使用的Key
public string Key
{
get
{
return mKey;
}
set
{
mKey = value;
}
}
// 设定缺省的加密算法为Rijndael(AES)
public EncryptClass()
{
mCryptoService = new RijndaelManaged();
mCryptoService.Mode = CipherMode.CBC;
mAlgorithm = ServiceProviderEnum.Rijndael;
}
// 利用ServiceProviderEnum来选择一种加密算法
public EncryptClass(ServiceProviderEnum serviceProvider)
{
switch(serviceProvider)
{
case ServiceProviderEnum.Rijndael:
mCryptoService = new RijndaelManaged();
mAlgorithm = ServiceProviderEnum.Rijndael;
break;
case ServiceProviderEnum.RC2:
mCryptoService = new RC2CryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.RC2;
break;
case ServiceProviderEnum.DES:
mCryptoService = new DESCryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.DES;
break;
case ServiceProviderEnum.TripleDES:
mCryptoService = new TripleDESCryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.TripleDES;
break;
}
mCryptoService.Mode = CipherMode.CBC;
}
//利用算法名来选择加密算法
public EncryptClass(string serviceProviderName)
{
try
{
switch(serviceProviderName.ToLower())
{
case "rijndael":
serviceProviderName = "Rijndael";
mAlgorithm = ServiceProviderEnum.Rijndael;
break;
case "rc2":
serviceProviderName = "RC2";
mAlgorithm = ServiceProviderEnum.RC2;
break;
case "des":
serviceProviderName = "DES";
mAlgorithm = ServiceProviderEnum.DES;
break;
case "tripledes":
serviceProviderName = "TripleDES";
mAlgorithm = ServiceProviderEnum.TripleDES;
break;
}
mCryptoService = (SymmetricAlgorithm)CryptoConfig.CreateFromName(serviceProviderName);
mCryptoService.Mode = CipherMode.CBC;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public byte[] GetLegalKey()
{
//对称算法的机密密钥的长度如下:
//对于DES算法,密钥长度为8字节
//对于TripleDES算法,密钥长度为16或者24字节
//对于Rijndael算法,密钥长度为16,24或者32字节
//可以人工输入,也可以随机生成,方法是:des.GenerateKey();
//对于不符合要求的key,就需要调整其内容
if (mCryptoService.LegalKeySizes.Length > 0)
{
// 用位来表示key的大小
int keySize = mKey.Length * 8;
int minSize = mCryptoService.LegalKeySizes[0].MinSize;
int maxSize = mCryptoService.LegalKeySizes[0].MaxSize;
int skipSize = mCryptoService.LegalKeySizes[0].SkipSize;
if (keySize > maxSize)
{
// 去除多余的部分
mKey = mKey.Substring(0, maxSize / 8);
}
else if (keySize < maxSize)
{
// 设定有效大小
int validSize = (keySize <= minSize)? minSize : (keySize - keySize % skipSize) + skipSize;
if (keySize < validSize)
{
// 使用*来填补不足的部分
mKey = mKey.PadRight(validSize / 8, '*');
}
}
}
PasswordDeriveBytes key = new PasswordDeriveBytes(mKey, ASCIIEncoding.ASCII.GetBytes(string.Empty));
return key.GetBytes(mKey.Length);
}
public string Encrypt(string plainText)
{
byte[] cryptoByte = null;
try
{
byte[] plainByte = System.Text.UTF8Encoding.UTF8.GetBytes(plainText);
byte[] keyByte = GetLegalKey();
// 设定key和向量
mCryptoService.Key = keyByte;
SetLegalIV();
// 加密对象
ICryptoTransform cryptoTransform = mCryptoService.CreateEncryptor();
// 内存流对象
MemoryStream ms = new MemoryStream();
// 初始化加密流
CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write);
// 将加密后的数据写入加密流
cs.Write(plainByte, 0, plainByte.Length);
cs.FlushFinalBlock();
cs.Close();
// 得到加密后的数据
cryptoByte = ms.ToArray();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
// 将数据转换成base64字符串
return Convert.ToBase64String(cryptoByte, 0, cryptoByte.GetLength(0));
}