一个基于.NET的加密/解密算法
使用:
C#类:
sr:http://dotnet.aspx.cc/ShowDetail.aspx?id=1AF1179E-43E5-4D4F-61AE-84922B5CBA74
SymmCrypto de = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
Response.Write(x.Decrypting(de.Encrypting("ok","yyy"),"yyy"));
Response.Write(x.Decrypting(de.Encrypting("ok","yyy"),"yyy"));
C#类:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace eMeng
{
/// <summary>
/// SymmCrypto 的摘要说明。
/// SymmCrypto类实现.NET框架下的加密和解密服务。
/// 原作者: Frank Fang : fangfrank@hotmail.com
/// </summary>
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
private SymmetricAlgorithm mobjCryptoService;
/// <remarks>
/// 使用.Net SymmetricAlgorithm 类的构造器.
/// </remarks>
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
/// <remarks>
/// 使用自定义SymmetricAlgorithm类的构造器.
/// </remarks>
public SymmCrypto(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
/// <remarks>
/// Depending on the legal key size limitations of
/// a specific CryptoService provider and length of
/// the private key provided, padding the secret key
/// with space character to meet the legal size of the algorithm.
/// </remarks>
private byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
// key sizes are in bits
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
// convert the secret key to byte array
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
public string Encrypting(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
// create a MemoryStream so that the process can be done without I/O files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
// get the output and trim the '
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace eMeng
{
/// <summary>
/// SymmCrypto 的摘要说明。
/// SymmCrypto类实现.NET框架下的加密和解密服务。
/// 原作者: Frank Fang : fangfrank@hotmail.com
/// </summary>
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
private SymmetricAlgorithm mobjCryptoService;
/// <remarks>
/// 使用.Net SymmetricAlgorithm 类的构造器.
/// </remarks>
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
/// <remarks>
/// 使用自定义SymmetricAlgorithm类的构造器.
/// </remarks>
public SymmCrypto(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
/// <remarks>
/// Depending on the legal key size limitations of
/// a specific CryptoService provider and length of
/// the private key provided, padding the secret key
/// with space character to meet the legal size of the algorithm.
/// </remarks>
private byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
// key sizes are in bits
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
// convert the secret key to byte array
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
public string Encrypting(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
// create a MemoryStream so that the process can be done without I/O files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
// get the output and trim the '
sr:http://dotnet.aspx.cc/ShowDetail.aspx?id=1AF1179E-43E5-4D4F-61AE-84922B5CBA74
posted on 2004-08-05 17:27 HelloSnoopy 阅读(428) 评论(0) 编辑 收藏 举报