using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Security.Cryptography;using System.Text;
public static string GetMD5Password(string password){ MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider(); byte[] bytes = Encoding.UTF7.GetBytes(password); bytes = crypto.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); foreach (byte num in bytes) { sb.AppendFormat("{0:x2}", num); } return sb.ToString();}public static string GetSHA512Password(string password){ byte[] bytes = Encoding.UTF7.GetBytes(password); byte[] result; SHA512 shaM = new SHA512Managed(); result = shaM.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); foreach (byte num in result) { sb.AppendFormat("{0:x2}", num); } return sb.ToString();}
<pre class="csharp" name="code"> private static string HashString256(string stringToHash, string hachKey) { UTF8Encoding myEncoder = new UTF8Encoding(); byte[] key = myEncoder.GetBytes(hachKey); byte[] text = myEncoder.GetBytes(stringToHash); System.Security.Cryptography.HMACSHA256 myHMACSHA256 = new System.Security.Cryptography.HMACSHA256(key); byte[] hashCode = myHMACSHA256.ComputeHash(text); string hash = BitConverter.ToString(hashCode).Replace("-", ""); return hash.ToLower(); }
//百度的MD5加密,與上面不太一樣public static string MD5(string password){ byte[] textBytes = System.Text.Encoding.Default.GetBytes(password); try { System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler; cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash = cryptHandler.ComputeHash(textBytes); string ret = ""; foreach (byte a in hash) { if (a < 16) ret += "0" + a.ToString("x"); else ret += a.ToString("x"); } return ret; } catch { throw; }}
註:MD5產生32位的密碼
SHA512產生128的密碼
SHA256產生64的密碼
可逆的加密碼演算法:產生的密碼位元由密碼本身長度決定
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Security.Cryptography;using System.IO;namespace GlacierV2.Common{ public static class Global { private static int _newDBPrimaryKeySeed = 0; #region 加密和解密演算法 private static SymmetricAlgorithm mobjCryptoService = new RijndaelManaged(); /// <summary> /// 獲得密鑰 /// </summary> /// <returns>密鑰</returns> private static byte[] GetLegalKey() { string sTemp = "xfsdfgsfgsdgsdfgsdfg"; mobjCryptoService.GenerateKey(); byte[] bytTemp = mobjCryptoService.Key; int KeyLength = bytTemp.Length; if (sTemp.Length > KeyLength) sTemp = sTemp.Substring(0, KeyLength); else if (sTemp.Length < KeyLength) sTemp = sTemp.PadRight(KeyLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// <summary> /// 獲得初始向量IV /// </summary> /// <returns>初試向量IV</returns> private static byte[] GetLegalIV() { string sTemp = "swetwerehetyeryertyerty"; mobjCryptoService.GenerateIV(); byte[] bytTemp = mobjCryptoService.IV; int IVLength = bytTemp.Length; if (sTemp.Length > IVLength) sTemp = sTemp.Substring(0, IVLength); else if (sTemp.Length < IVLength) sTemp = sTemp.PadRight(IVLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// <summary> /// 加密方法 /// </summary> /// <param name="Source">待加密的串</param> /// <returns>經過加密的串</returns> public static string Encrypto(string Source) { byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); MemoryStream ms = new MemoryStream(); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); } /// <summary> /// 解密方法 /// </summary> /// <param name="Source">待解密的串</param> /// <returns>經過解密的串</returns> public static string Decrypto(string Source) { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } #endregion }}