symmcryptode=newSymmCrypto(SymmCrypto.SymmProvEnum.DES);
Response.Write(x.Decrypting(de.Encrypting("ok","yyy"),"yyy"));
类的实现c#编码
usingSystem;
usingSystem.Security.Cryptography;
usingSystem.IO;
usingSystem.Text;
namespaceeMeng
{
///<summary>
///SymmCrypto的摘要说明。
///SymmCrypto类实现.NET框架下的加密和解密服务。
///原作者:FrankFang:[email protected]
///</summary>
publicclassSymmCrypto
{
publicenumSymmProvEnum:int
{
DES,RC2,Rijndael
}
privateSymmetricAlgorithmmobjCryptoService;
///<remarks>
///使用.NetSymmetricAlgorithm类的构造器.
///</remarks>
publicSymmCrypto(SymmProvEnumNetSelected)
{
switch(NetSelected)
{
caseSymmProvEnum.DES:
mobjCryptoService=newDESCryptoServiceProvider();
break;
caseSymmProvEnum.RC2:
mobjCryptoService=newRC2CryptoServiceProvider();
break;
caseSymmProvEnum.Rijndael:
mobjCryptoService=newRijndaelManaged();
break;
}
}
///<remarks>
///使用自定义SymmetricAlgorithm类的构造器.
///</remarks>
publicSymmCrypto(SymmetricAlgorithmServiceProvider)
{
mobjCryptoService=ServiceProvider;
}
///<remarks>
///Dependingonthelegalkeysizelimitationsof
///aspecificCryptoServiceproviderandlengthof
///theprivatekeyprovided,paddingthesecretkey
///withspacecharactertomeetthelegalsizeofthealgorithm.
///</remarks>
privatebyte[]GetLegalKey(stringKey)
{
stringsTemp;
if(mobjCryptoService.LegalKeySizes.Length>0)
{
intlessSize=0,moreSize=mobjCryptoService.LegalKeySizes[0].MinSize;
//keysizesareinbits
while(Key.Length*8>moreSize)
{
lessSize=moreSize;
moreSize+=mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp=Key.PadRight(moreSize/8,'');
}
else
sTemp=Key;
//convertthesecretkeytobytearray
returnASCIIEncoding.ASCII.GetBytes(sTemp);
}
publicstringEncrypting(stringSource,stringKey)
{
byte[]bytIn=System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
//createaMemoryStreamsothattheprocesscanbedonewithoutI/Ofiles
System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
byte[]bytKey=GetLegalKey(Key);
//settheprivatekey
mobjCryptoService.Key=bytKey;
mobjCryptoService.IV=bytKey;
//createanEncryptorfromtheProviderServiceinstance
ICryptoTransformencrypto=mobjCryptoService.CreateEncryptor();
//createCryptoStreamthattransformsastreamusingtheencryption
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Write);
//writeoutencryptedcontentintoMemoryStream
cs.Write(bytIn,0,bytIn.Length);
cs.FlushFinalBlock();
//gettheoutputandtrimthe'\0'bytes
byte[]bytOut=ms.GetBuffer();
inti=0;
for(i=0;i<bytOut.Length;i++)
if(bytOut[i]==0)
break;
//convertintoBase64sothattheresultcanbeusedinxml
returnSystem.Convert.ToBase64String(bytOut,0,i);
}
publicstringDecrypting(stringSource,stringKey)
{
//convertfromBase64tobinary
byte[]bytIn=System.Convert.FromBase64String(Source);
//createaMemoryStreamwiththeinput
System.IO.MemoryStreamms=newSystem.IO.MemoryStream(bytIn,0,bytIn.Length);
byte[]bytKey=GetLegalKey(Key);
//settheprivatekey
mobjCryptoService.Key=bytKey;
mobjCryptoService.IV=bytKey;
//createaDecryptorfromtheProviderServiceinstance
ICryptoTransformencrypto=mobjCryptoService.CreateDecryptor();
//createCryptoStreamthattransformsastreamusingthedecryption
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Read);
//readouttheresultfromtheCryptoStream
System.IO.StreamReadersr=newSystem.IO.StreamReader(cs);
returnsr.ReadToEnd();
}
}
}