zoukankan      html  css  js  c++  java
  • MD5

    /// <summary>
    /// 不可逆加密
    /// 1 防止被篡改
    /// 2 防止明文存储
    /// 3 防止抵赖,数字签名
    /// </summary>
    using System.Security.Cryptography;
    public class MD5Encrypt
    {
    #region MD5
    /// <summary>
    /// MD5加密,和动网上的16/32位MD5加密结果相同,
    /// 使用的UTF8编码
    /// </summary>
    /// <param name="strSource">待加密字串</param>
    /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
    /// <returns>加密后的字串</returns>
    public static string Encrypt(string source, int length = 32)//默认参数
    {
    HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
    if (string.IsNullOrEmpty(source)) return string.Empty;

    byte[] bytes = Encoding.UTF8.GetBytes(source);// Encoding.ASCII.GetBytes(source);
    byte[] hashValue = provider.ComputeHash(bytes);
    StringBuilder sb = new StringBuilder();
    switch (length)
    {
    case 16://16位密文是32位密文的9到24位字符
    for (int i = 4; i < 12; i++)
    sb.Append(hashValue[i].ToString("x2"));
    break;
    case 32:
    for (int i = 0; i < 16; i++)
    {
    sb.Append(hashValue[i].ToString("x2"));
    }
    break;
    default:
    for (int i = 0; i < hashValue.Length; i++)
    {
    sb.Append(hashValue[i].ToString("x2"));
    }
    break;
    }
    return sb.ToString();
    }
    #endregion MD5
    }

    MD5Encrypt.Encrypt("张三李四")

  • 相关阅读:
    C加加学习之路 1——开始
    哈夫曼树C++实现详解
    Linux常用命令
    Accp第二章:基础知识
    第一章Accp 8.0
    泛型集合
    深入C#数据类型
    初始wondows系统
    深入.NET框架
    二至十五章总结
  • 原文地址:https://www.cnblogs.com/anyihen/p/12773196.html
Copyright © 2011-2022 走看看