zoukankan      html  css  js  c++  java
  • .net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

             写项目时,后台一直用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;
                }
            }
      

  • 相关阅读:
    Android零基础入门第22节:ImageView的属性和方法大全
    Android零基础入门第21节:ToggleButton和Switch使用大全
    Android零基础入门第20节:CheckBox和RadioButton使用大全
    Android零基础入门第19节:Button使用详解
    Android零基础入门第18节:EditText的属性和使用方法
    Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全
    Android零基础入门第16节:Android用户界面开发概述
    Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航
    Android零基础入门第14节:使用高速Genymotion,跨入火箭时代
    <bean> 中的单例 和 原型 scope="singleton" //单例 scope="prototype" //原型
  • 原文地址:https://www.cnblogs.com/goody9807/p/2094407.html
Copyright © 2011-2022 走看看