SHA,SHA1,MD5(16位,32位,64位)加密方式(C#实现)

本文详细介绍了SHA和MD5两种常见加密算法的使用方法,包括SHA1、SHA256、MD5-32、MD5-16、MD5-64等不同变种的实现,并通过C#代码示例展示了加密过程及结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,直接上码

 

加密服务类:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace ConsoleApp.Services
{
    /// <summary>
    /// Sha加密和Md5加密   CreateDate:2020-01-04 16:00:13;Author:wenhao.zhang
    /// Sha1和Md5加密对比:
    /// Sha1加密更不易被破解
    /// Md5加密运算更快
    /// </summary>
    public class TestEncrypt1Service
    {
        /// <summary>
        /// Sha1加密(加密之后长度为40)   CreateDate:2020-01-04 15:51:37;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isUpper"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Sha1(string source, bool isUpper, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                var bytes = encoding.GetBytes(source);
                var data = SHA1.Create().ComputeHash(bytes);
                var sb = new StringBuilder();
                string o = "x2";
                if (isUpper)
                {
                    o = o.ToUpper();
                }
                foreach (var s in data)
                {
                    string t = s.ToString(o);
                    sb.Append(t);
                }
                result = sb.ToString();
                Console.WriteLine($"加密前:{source}(长度{source.Length})");
                Console.WriteLine($"加密后:{result}(长度{result.Length})");
            });
            return result;
        }

        /// <summary>
        /// Sha加密(加密之后长度为44)   CreateDate:2020-01-04 15:52:50;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Sha(string source, string key, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                string secret = "HMACSHA256";
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                using (var instance = KeyedHashAlgorithm.Create(secret))
                {
                    instance.Key = encoding.GetBytes(key.ToCharArray());
                    var bytes = encoding.GetBytes(source);
                    var bytesHash = instance.ComputeHash(bytes);
                    result = Convert.ToBase64String(bytesHash);
                }
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }

        /// <summary>
        /// Md5加密(加密之后长度为32)   CreateDate:2020-01-04 15:53:30;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isUpper"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Md5Old32(string source, bool isUpper, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                var provider = new MD5CryptoServiceProvider();
                var bytes = encoding.GetBytes(source);
                var hashBytes = provider.ComputeHash(bytes);
                string o = "x2";
                if (isUpper)
                {
                    o = o.ToUpper();
                }
                var sb = new StringBuilder();
                foreach (var b in hashBytes)
                {
                    string t = b.ToString(o);
                    sb.Append(t);
                }
                result = sb.ToString();
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }

        /// <summary>
        /// Md5-16加密(加密之后长度为32,但是加密结果上述结果不同)   CreateDate:2020-01-04 15:54:22;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Md5New16(string source, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                var provider = new MD5CryptoServiceProvider();
                var bytes = encoding.GetBytes(source);
                var hashBytes = provider.ComputeHash(bytes, 4, 8);
                string t = BitConverter.ToString(hashBytes);
                result = t.Replace("-", string.Empty);
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }

        /// <summary>
        /// Md5加密(加密之后长度为32,加密结果和上述的Md5加密结果相同,只是实现方式不同罢了)   CreateDate:2020-01-04 15:55:08;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isUpper"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Md5New32(string source, bool isUpper, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                var instance = MD5.Create();
                var bytes = encoding.GetBytes(source);
                var hashBytes = instance.ComputeHash(bytes);
                string o = "x2";
                if (isUpper)
                {
                    o = o.ToUpper();
                }
                var sb = new StringBuilder();
                foreach (var b in hashBytes)
                {
                    string t = b.ToString(o);
                    sb.Append(t);
                }
                result = sb.ToString();
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }

        /// <summary>
        /// Md5-64加密(加密之后长度为24,加密结果和上述的Md5加密结果不同)   CreateDate:2020-01-04 15:56:33;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Md5New64(string source, Encoding encoding = null)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                var instance = MD5.Create();
                var bytes = encoding.GetBytes(source);
                var hashBytes = instance.ComputeHash(bytes);
                result = Convert.ToBase64String(hashBytes);
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }

        /// <summary>
        /// Md5加密(加密之后长度为32,加密结果和上述的Md5加密结果相同,只是实现方式不同,该方法已过时,需要引用System.Web)   CreateDate:2020-01-04 15:57:32;Author:wenhao.zhang
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string Md5New32Overtime(string source, bool isUpper)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(source))
            {
                return result;
            }
            LingbugService.RunTry(() =>
            {
                string type = "MD5";
                var t = FormsAuthentication.HashPasswordForStoringInConfigFile(source, type);
                result = isUpper ? t : t.ToLower();
            });
            Console.WriteLine($"加密前:{source}(长度{source.Length})");
            Console.WriteLine($"加密后:{result}(长度{result.Length})");
            return result;
        }
    }
}

 

测试代码:

            string password = "testPassword123.0";
            Console.WriteLine("Sha1(大写):" + TestEncrypt1Service.Sha1(password, true));
            Console.WriteLine();
            Console.WriteLine("Sha1(小写):" + TestEncrypt1Service.Sha1(password, false));
            Console.WriteLine();
            Console.WriteLine("Sha:" + TestEncrypt1Service.Sha(password, "zwh"));
            Console.WriteLine();
            Console.WriteLine("Md5Old(大写):" + TestEncrypt1Service.Md5Old32(password, true));
            Console.WriteLine();
            Console.WriteLine("Md5Old(小写):" + TestEncrypt1Service.Md5Old32(password, false));
            Console.WriteLine();
            Console.WriteLine("Md5-16:" + TestEncrypt1Service.Md5New16(password));
            Console.WriteLine();
            Console.WriteLine("Md5New(大写):" + TestEncrypt1Service.Md5New32(password, true));
            Console.WriteLine();
            Console.WriteLine("Md5New(小写):" + TestEncrypt1Service.Md5New32(password, false));
            Console.WriteLine();
            Console.WriteLine("Md5-64:" + TestEncrypt1Service.Md5New64(password));
            Console.WriteLine();
            Console.WriteLine("过时的Md5-32(大写):" + TestEncrypt1Service.Md5New32Overtime(password, true));
            Console.WriteLine();
            Console.WriteLine("过时的Md5-32(小写):" + TestEncrypt1Service.Md5New32Overtime(password, false));
            Console.WriteLine();

 

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值