MD5、SHA256、SHA512密碼編譯演算法,以及可逆演算法

來源:互聯網
上載者:User
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    }}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.