.net實現md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

來源:互聯網
上載者:User

標籤:

 寫項目時,後台一直用md5加密,一天群裡人問,除了MD5還有其它的加密方法嗎?當時只知道還有個SHA,但怎麼實現什麼的都不清楚,於是當網上找了下,把幾種常見的加密方法都整理了下,用winform寫了個程式,:

 

關鍵代碼

using System.Security;
using System.Security.Cryptography;
using System.Web;
using System.IO;

 

        //MD5       不區分大小寫
        //type 類型,16位還是32位,16位就是取32位的第8到16位
        public string DoMd5Encode(string pwd,string type)
        {
            byte[] result = Encoding.Default.GetBytes(pwd);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            if (type == "16")
                return BitConverter.ToString(output).Replace("-", "").ToLower().Substring(8,16);    
            else
                return BitConverter.ToString(output).Replace("-", "").ToLower();    
            
        }

 

        /// <summary>
        /// 對字串進行SHA1加密
        /// </summary>
        /// <param name="strIN">需要加密的字串</param>
        /// <returns>密文</returns>
        public string SHA1_Encrypt(string Source_String)
        {
            byte[] StrRes = Encoding.Default.GetBytes(Source_String);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        /// <summary>
        /// SHA256加密,無法復原轉
        /// </summary>
        /// <param name="str">string str:被加密的字串</param>
        /// <returns>返回加密後的字串</returns>
        private string SHA256Encrypt(string str)
        {
            System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
            byte[] byte1;
            byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str));
            s256.Clear();
            return Convert.ToBase64String(byte1);
        }

       /// <summary>
        /// SHA384加密,無法復原轉
        /// </summary>
        /// <param name="str">string str:被加密的字串</param>
        /// <returns>返回加密後的字串</returns>
        private string SHA384Encrypt(string str)
        {
            System.Security.Cryptography.SHA384 s384 = new System.Security.Cryptography.SHA384Managed();
            byte[] byte1;
            byte1 = s384.ComputeHash(Encoding.Default.GetBytes(str));
            s384.Clear();
            return Convert.ToBase64String(byte1);
        }

 


        /// <summary>
        /// SHA512加密,無法復原轉
        /// </summary>
        /// <param name="str">string str:被加密的字串</param>
        /// <returns>返回加密後的字串</returns>
        private string SHA512Encrypt(string str)
        {
            System.Security.Cryptography.SHA512 s512 = new System.Security.Cryptography.SHA512Managed();
            byte[] byte1;
            byte1 = s512.ComputeHash(Encoding.Default.GetBytes(str));
            s512.Clear();
            return Convert.ToBase64String(byte1);
        }

 

 

 

 

 

        //預設密鑰向量
        private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
        /// <summary>
        /// DES加密字串
        /// </summary>
        /// <param name="encryptString">待加密的字串</param>
        /// <param name="encryptKey">加密金鑰,要求為8位</param>
        /// <returns>加密成功返回加密後的字串,失敗返回源串</returns>
        public string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密字串
        /// </summary>
        /// <param name="decryptString">待解密的字串</param>
        /// <param name="decryptKey">解密密鑰,要求為8位,和加密金鑰相同</param>
        /// <returns>解密成功返回解密後的字串,失敗返源串</returns>
        public string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
   

轉:goody9807  .net實現md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

.net實現md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.