手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >asp.net TripleDES加密、解密算法
asp.net TripleDES加密、解密算法
摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSyst...

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中MD5 16位和32位加密函数

asp.net下url传递中文的解决方案

asp.net导出excel的简单方法

asp.net页面与页面之间传参数值方法

asp.net webservice返回json的方法

asp.net 备份和恢复数据库的方法

asp.net实现md5加密

asp.net下Request.QueryString取不到值的解决方法

asp.net2.0中css失效的解决方法

asp.net querystring乱码解决方法

精品推荐
分类导航