一、概念:

MD5码以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值

二、C#使用MD5进行加密字符串

MD5的引用命名空间是System.Security.Cryptography;

1.固定返回固定长度字符串(16位或者32位)

/// <summary>
/// 用MD5加密字符串,可选择生成16位或者32位的加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <param name="bit">位数,一般取值16 或 32</param>
/// <returns>返回的加密后的字符串</returns>
public string MD5Encrypt(string password, int bit)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
if (bit == )
return tmp.ToString().A(, );
else
if (bit == ) return tmp.ToString();//默认情况
else return string.Empty;
}

2、加密字符串

/// <summary>
/// 用MD5加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <returns></returns>
public string MD5Encrypt(string password)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
return tmp.ToString();
}

  3、标准的MD5加密32位小写的:

  public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
//byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
byte[] fromData = System.Text.Encoding.UTF8.GetBytes(myString);//
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null; for (int i = ; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
} return byte2String;
}

如果用上面这个标准的会有一个问题,就是丢失位数,所以字节转换成字符串的时候要保证是2位宽度啊,某个字节为0转换成字符串的时候必须是00的,否则就会丢失位数啊。不仅是0,1~9也一样。

用以下代码就可以避免:

public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
//byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
byte[] fromData = System.Text.Encoding.UTF8.GetBytes(myString);//
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null; for (int i = ; i < targetData.Length; i++)
{
//这个是很常见的错误,你字节转换成字符串的时候要保证是2位宽度啊,某个字节为0转换成字符串的时候必须是00的,否则就会丢失位数啊。不仅是0,1~9也一样。
//byte2String += targetData[i].ToString("x");//这个会丢失
byte2String = byte2String+ targetData[i].ToString("x2");
} return byte2String;
}

使用MD5加密字符串的更多相关文章

  1. linux md5 加密字符串和文件方法

    linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...

  2. Java生成MD5加密字符串代码实例

    这篇文章主要介绍了Java生成MD5加密字符串代码实例,本文对MD5的作用作了一些介绍,然后给出了Java下生成MD5加密字符串的代码示例,需要的朋友可以参考下   (1)一般使用的数据库中都会保存用 ...

  3. iOS中MD5加密字符串实现

    1.MD5加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321 ...

  4. C# MD5加密字符串

    /// <summary> /// 用MD5加密字符串,可选择生成16位或者32位的加密字符串 /// </summary> /// <param name=" ...

  5. iOS MD5加密字符串

    参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...

  6. C#中使用SHA1和MD5加密字符串

    SHA1和MD5加密均为不可逆加密.代码如下: using System.Security.Cryptography; //添加Using static void Main(string[] args ...

  7. MD5加密字符串--基于python

    import hashlib#md5加密32位def md5(str): import hashlib m = hashlib.md5() m.update(str) return m.hexdige ...

  8. MD5 加密字符串

    public class MD5 { /*** * MD5加码 生成32位md5码 */ public static String string2MD5(String inStr){ MessageD ...

  9. C#加密方法汇总(SHA1加密字符串,MD5加密字符串,可逆加密等)

    using System;using System.Collections.Generic;using System.Text; namespace StringEncry{ class Encode ...

随机推荐

  1. 回声消除(AEC)原理

    一.前言 因为工作的关系,笔者从2004年开始接触回声消除(Echo Cancellation)技术,而后一直在某大型通讯企业从事与回声消除技术相关的工作,对回声消除这个看似神秘.高端和难以理解的技术 ...

  2. python3 selenium模块Chrome设置代理ip的实现

    python3 selenium模块Chrome设置代理ip的实现 selenium模块Chrome设置代理ip的实现代码: from selenium import webdriver chrome ...

  3. 000 装docker

    直接参考别人的文章,经过验证,没有问题,需要网络. URL: https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html 我是虚拟机 ...

  4. Grande插件和版本的对应关系

  5. centos7.6环境zabbix3.2源码编译安装版升级到zabbix4.0长期支持版

    zabbix3.2源码编译安装版升级到zabbix4.0长期支持版 项目需求: .2版本不再支持,想升级成4.0的长期支持版 环境介绍: zabbix服务端是编译安装的,数据库和web在一台机器上 整 ...

  6. Python3基础 for-else break、continue跳出循环示例

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  7. Flutter Form正确使用方法【可正确获取提交的表单数据】

    import 'package:flutter/material.dart'; void main() => runApp(new HomePage()); class HomePage ext ...

  8. this page isn't working (ERR_EMPTY_RESPONSE)

    特定情况触发了PHP的Call to undefined function(函数不存在)的Fatal error(致命错误),PHP异常终止执行,Apache收到PHP的异常信号时,认为PHP处理请求 ...

  9. fa-list-alt

    你可以用 <i> 标签把 Font Awesome 图标放在任意位置. <i class="fa fa-list-alt" aria-hidden="t ...

  10. Dockerfile-server2

    [root@lab2 docker-file]# cd server2/ [root@lab2 server2]# ls ddbes-server2-0.0.1-SNAPSHOT.jar Docker ...