常用的加密与解密类:
View Code
////// 用于对字符串加密解密的类。 /// public class Cryptography { ////// 3DES加密算法的种子,从Web.config中读取 /// private static string SEED = ConfigurationSettings.AppSettings["Seed"].ToString();//加密种子 #region 构造函数 ////// 用于对字符串加密解密的类。. /// private Cryptography() { //构造函数 } #endregion #region 对输入值进行哈希运算 ////// 对输入值进行哈希运算。使用的算法名称:MD5、SHA 、SHA256 、SHA384 、 SHA512 /// /// 源字符串内容。 public static string ComputeHash(string strSource) { const string Cryptography = "SHA"; return ComputeHash(strSource, Cryptography); } ////// 计算传入字符串的哈希值。 /// /// 源字符串内容。 /// 要使用的算法名称。 public static string ComputeHash(string strSource, string strCryptography) { byte[] data = ConvertStringToByteArray(strSource); byte[] result = ((HashAlgorithm)CryptoConfig.CreateFromName(strCryptography)).ComputeHash(data); return Convert.ToBase64String(result); } #endregion #region 3DES加密 ////// 对传入的字符串使用相应的密码进行加密(使用3DES加密算法) /// /// 需要加密的源字符串。 /// 密匙串。 public static string Encrypt(string strSource, string strPassword) { byte[] hashvalue = ConvertStringToByteArray(ComputeHash(strPassword, "SHA256")); byte[] desiv = new byte[8]; byte[] deskey = new byte[24]; int step = 0; for (int i = 0; i < 32; i++) { if (i < 8) desiv[i] = hashvalue[step]; else deskey[i - 8] = hashvalue[step]; step += 2; } TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); ICryptoTransform desencrypt = des.CreateEncryptor(deskey, desiv); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); byte[] plainBytes = Encoding.Unicode.GetBytes(strSource); try { cs.Write(plainBytes, 0, plainBytes.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } finally { ms.Close(); cs.Close(); } } ////// 对传入的字符串使用相应的密码进行加密(使用3DES加密算法) /// /// 需要加密的源字符串。 ///已加密的字符串 public static string Encrypt(string strPassword) { byte[] hashvalue = ConvertStringToByteArray(ComputeHash(SEED, "SHA256")); byte[] desiv = new byte[8]; byte[] deskey = new byte[24]; int step = 0; for (int i = 0; i < 32; i++) { if (i < 8) desiv[i] = hashvalue[step]; else deskey[i - 8] = hashvalue[step]; step += 2; } TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); ICryptoTransform desencrypt = des.CreateEncryptor(deskey, desiv); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); byte[] plainBytes = Encoding.Unicode.GetBytes(strPassword); try { cs.Write(plainBytes, 0, plainBytes.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } finally { ms.Close(); cs.Close(); } } #endregion #region MD5加密 ////// 标准MD5算法 /// /// 要加密的字符串 ///public static string MD5(string source) { string md5String = string.Empty; try { byte[] byteCode = System.Text.Encoding.GetEncoding("GB2312").GetBytes(source.Trim()); byteCode = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteCode); for (int i = 0; i < byteCode.Length; i++) { md5String += byteCode[i].ToString("x").PadLeft(2, '0'); } } catch (Exception ex) { throw ex; } return md5String; } #endregion #region 解密(只能是3DES) /// /// 对传入的字符串使用相应的密码进行解密(3DES算法) /// /// 需要解密的源字符串。 /// 密匙串。 public static string Decrypt(string strSource, string strPassword) { byte[] hashvalue = ConvertStringToByteArray(ComputeHash(strPassword, "SHA256")); byte[] desiv = new byte[8]; byte[] deskey = new byte[24]; int step = 0; for (int i = 0; i < 32; i++) { if (i < 8) desiv[i] = hashvalue[step]; else deskey[i - 8] = hashvalue[step]; step += 2; } TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); ICryptoTransform decrypt = des.CreateDecryptor(deskey, desiv); byte[] instream = Convert.FromBase64String(strSource); MemoryStream ms = new MemoryStream(instream); CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Read); byte[] plainBytes = new byte[instream.Length]; try { cs.Read(plainBytes, 0, plainBytes.Length); return Encoding.Unicode.GetString(plainBytes); //string strNew = Encoding.Unicode.GetString(plainBytes); //strNew = strNew.Substring(0, strNew.Length - 3); //return strNew; //return Encoding.Unicode.GetString(plainBytes); } catch { throw; } finally { ms.Close(); try { cs.Close(); } catch { } } } ////// 对传入的字符串使用相应的密码进行解密 /// /// 需要解密的源字符串 ///解密后的字符串 public static string Decrypt(string strPassword) { byte[] hashvalue = ConvertStringToByteArray(ComputeHash(SEED, "SHA256")); byte[] desiv = new byte[8]; byte[] deskey = new byte[24]; int step = 0; for (int i = 0; i < 32; i++) { if (i < 8) desiv[i] = hashvalue[step]; else deskey[i - 8] = hashvalue[step]; step += 2; } TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); ICryptoTransform decrypt = des.CreateDecryptor(deskey, desiv); byte[] instream = Convert.FromBase64String(strPassword); MemoryStream ms = new MemoryStream(instream); CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Read); byte[] plainBytes = new byte[instream.Length]; try { cs.Read(plainBytes, 0, plainBytes.Length); string strNew = Encoding.Unicode.GetString(plainBytes); strNew = strNew.Substring(0, strNew.Length - 3); return strNew; } catch { throw; } finally { ms.Close(); try { cs.Close(); } catch { } } } #endregion #region 字符串转换为字节数组 ////// 把字符串转换为字节数组 /// public static Byte[] ConvertStringToByteArray(string s) { return (new UnicodeEncoding()).GetBytes(s); } #endregion }