MD5签名 加密比对字符串

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace TestProject.Common
{
    /// <summary>
    /// 加密字符串类
    /// </summary>
    public class EncryptionUtil
    {

        /// <summary>
        /// 创建字符串的MD5哈希值(不适用于中文)
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns>字符串MD5哈希值的十六进制字符串</returns>
        public static string StringToMD5Hash(string inputString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// MD5签名 (appid+body+appkey)(中文适用)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string source)
        {
            string strResult = string.Empty;
            byte[] result = Encoding.UTF8.GetBytes(source);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            strResult = BitConverter.ToString(output).Replace("-", "");
            return strResult;
        }

    }
}

  

posted @ 2023-08-22 10:50  *ち黑サカ  阅读(13)  评论(0)    收藏  举报