Microsoft .NET 中的简化加密
http://www.51cto.com/specbook/15/3407.htm
1.SHA1
using System.Security.Cryptography;
SHA1CryptoServiceProvider sha1=new SHA1CryptoServiceProvider();
string encryptedString = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes("testPwd")));
2.MD5只是用的类不同
MD5CryptoServiceProvider md5=New MD5CryptoServiceProvider();
3.给散列算法加salt
private string CreateSalt()
{
byte[] bytSalt = new byte[8];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytSalt);
return Convert.ToBase64String(bytSalt);
}
4.DES(64位)
SymmetricAlgorithm mCSP =new DESCryptoServiceProvider();
mCSP.GenerateKey();
mCSP.GenerateIV();
string txtKey= Convert.ToBase64String(mCSP.Key);
string txtIV= Convert.ToBase64String(mCSP.IV);
private string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
private string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
5.TripleDES(192位)
SymmetricAlgorithm mCSP =new TripleDESCryptoServiceProvider();
6.Rijndael
RijndaelManaged objRijndaelManaged=new=RijndaelManaged();