usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Security.Cryptography;
usingSystem.IO;
namespaceWindowsFormsApplication1
{
#regionTripleDES算法
publicclassClassTripleDES
{
publicClassTripleDES()
{
}
//加密,使用密码产生加密算法的公钥,并使用TripleDES对密码进行加密。
publicstaticstringEncrypt(stringpass)
{
try
{
byte[]bt=(newSystem.Text.UnicodeEncoding()).GetBytes(pass);
PasswordDeriveBytespdb=newPasswordDeriveBytes(pass,null);
byte[]key=pdb.GetBytes(24);
byte[]iv=pdb.GetBytes(8);
MemoryStreamms=newMemoryStream();
TripleDESCryptoServiceProvidertdesc=newTripleDESCryptoServiceProvider();
CryptoStreamcs=newCryptoStream(ms,tdesc.CreateEncryptor(key,iv),CryptoStreamMode.Write);
cs.Write(bt,0,bt.Length);
cs.FlushFinalBlock();
returnConvert.ToBase64String(ms.ToArray());
}
catch(Exceptionex)
{
throwex;
}
}
//解密,使用密码产生加密算法的公钥,并使用TripleDES对加密数据进行解密。
publicstaticstringDecrypt(stringstr,stringpass)
{
try
{
byte[]bt=Convert.FromBase64String(str);
PasswordDeriveBytespdb=newPasswordDeriveBytes(pass,null);
byte[]key=pdb.GetBytes(24);
byte[]iv=pdb.GetBytes(8);
MemoryStreamms=newMemoryStream();
TripleDESCryptoServiceProvidertdesc=newTripleDESCryptoServiceProvider();
CryptoStreamcs=newCryptoStream(ms,tdesc.CreateDecryptor(key,iv),CryptoStreamMode.Write);
cs.Write(bt,0,bt.Length);
cs.FlushFinalBlock();
return(newSystem.Text.UnicodeEncoding()).GetString(ms.ToArray());
}
catch(Exceptionex)
{
throwex;
}
}
//使用:
//stringstr=Encrypt("bbb");
//Console.WriteLine(Decrypt(str,"bbb"));
//加密,使用密码产生加密算法的公钥,并使用TripleDES对密码进行加密。
publicstaticstringEncryptWithKey(stringpass,stringp_key)
{
try
{
byte[]bt=(newSystem.Text.UnicodeEncoding()).GetBytes(pass);
PasswordDeriveBytespdb=newPasswordDeriveBytes(p_key,null);
byte[]key=pdb.GetBytes(24);
byte[]iv=pdb.GetBytes(8);
MemoryStreamms=newMemoryStream();
TripleDESCryptoServiceProvidertdesc=newTripleDESCryptoServiceProvider();
CryptoStreamcs=newCryptoStream(ms,tdesc.CreateEncryptor(key,iv),CryptoStreamMode.Write);
cs.Write(bt,0,bt.Length);
cs.FlushFinalBlock();
returnConvert.ToBase64String(ms.ToArray());
}
catch(Exceptionex)
{
throwex;
}
}
//解密,使用密码产生加密算法的公钥,并使用TripleDES对加密数据进行解密。
publicstaticstringDecryptWithKey(stringstr,stringp_key)
{
try
{
byte[]bt=Convert.FromBase64String(str);
PasswordDeriveBytespdb=newPasswordDeriveBytes(p_key,null);
byte[]key=pdb.GetBytes(24);
byte[]iv=pdb.GetBytes(8);
MemoryStreamms=newMemoryStream();
TripleDESCryptoServiceProvidertdesc=newTripleDESCryptoServiceProvider();
CryptoStreamcs=newCryptoStream(ms,tdesc.CreateDecryptor(key,iv),CryptoStreamMode.Write);
cs.Write(bt,0,bt.Length);
cs.FlushFinalBlock();
return(newSystem.Text.UnicodeEncoding()).GetString(ms.ToArray());
}
catch(Exceptionex)
{
throwex;
}
}
}
#endregion
}
【asp.net TripleDES加密、解密算法】相关文章:
★ asp.net下Request.QueryString取不到值的解决方法