一个可逆的DES和TripleDES方式加密类(downmoon原创)
usingSystem;
usingSystemSecurity;
usingSystemSecurityCryptography;
usingSystemIO;
usingSystemText;
namespaceEncryptDownmoon
{
/**////<summary>
///EncryptSqlConn的摘要说明
///</summary>
publicclassEncryptSqlConn
{
publicEncryptSqlConn()
{
if(mCSP==null)
{
mCSP=SetEnc();
}
}
/**////<summary>
///设置加密方式为DES为TripleDES
///</summary>
///<returns></returns>
publicEncryptSqlConn(intEnMethod)
{
if(EnMethod==)
{
lngEnMethod=;
}
else
{
lngEnMethod=;
}
if(mCSP==null)
{
mCSP=SetEnc();
}
}
方法#region方法
privateSymmetricAlgorithmmCSP;
privateintm_lngEnMethod=;
/**////<summary>
///加密方式为DES为TripleDES
///</summary>
publicintlngEnMethod
{
get
{
returnm_lngEnMethod;
}
set
{
m_lngEnMethod=value;
}
}
privateSymmetricAlgorithmSetEnc()
{
if(lngEnMethod==)
{
returnnewDESCryptoServiceProvider();
}
else
returnnewTripleDESCryptoServiceProvider();
}
//测试键值
privatestringgenKeyValue
{
get
{
mCSPGenerateKey();
returnConvertToBaseString(mCSPKey);
}
}
//测试IV值
privatestringgenIVValue
{
get
{
mCSPGenerateIV();
returnConvertToBaseString(mCSPIV);
}
}
/**////<summary>
///加密字串
///</summary>
///<paramname=Value></param>
///<returns></returns>
publicstringEncryptString(stringValue)
{
ICryptoTransformct;
MemoryStreamms;
CryptoStreamcs;
byte[]byt;
ct=mCSPCreateEncryptor(mCSPKeymCSPIV);
//ct=mCSPCreateEncryptor(genKeyValuegenIVValue);
byt=EncodingUTFGetBytes(Value);
ms=newMemoryStream();
cs=newCryptoStream(msctCryptoStreamModeWrite);
csWrite(bytbytLength);
csFlushFinalBlock();
csClose();
returnConvertToBaseString(msToArray());
}
/**////<summary>
///解密字串
///</summary>
///<paramname=Value></param>
///<returns></returns>
publicstringDecryptString(stringValue)
{
ICryptoTransformct;
MemoryStreamms;
CryptoStreamcs;
byte[]byt;
ct=mCSPCreateDecryptor(mCSPKeymCSPIV);
//ct=mCSPCreateDecryptor(genKeyValuegenIVValue);
byt=ConvertFromBaseString(Value);
ms=newMemoryStream();
cs=newCryptoStream(msctCryptoStreamModeWrite);
csWrite(bytbytLength);
csFlushFinalBlock();
csClose();
returnEncodingUTFGetString(msToArray());
}
#endregion
}
}