一、概念:

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. The Practical Importance of Feature Selection(变量筛选重要性)

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  2. [转]WebMercator与经纬度互转

    //经纬度转Web墨卡托 Vector3 lonLat2WebMercator(Vector3 lonLat) { Vector3 mercator; ; + lonLat.y)*)) / (); y ...

  3. ISO/IEC 9899:2011 条款6.7——声明

    6.7 声明 语法 1.declaration: declaration-specifiers    init-declarator-listopt    ; static_assert-declar ...

  4. ISO/IEC 9899:2011 条款6.7.4——函数说明符

    6.7.4 函数说明符 语法 1.function-specifier: inline _Noreturn 约束 2.函数说明符应该只能被用在对一个函数标识符的声明中. 3.对一个含有外部连接函数的内 ...

  5. Python3入门(十三)——连接数据库

    以Mysql为例: 要操作关系数据库,首先需要连接到数据库,一个数据库连接称为Connection: 连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结 ...

  6. 【FreeMarker】FreeMarker使用(三)

    搭建一个 1.FreeMarker取值 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  7. 【PHP】php7.2报错The each() function is deprecated. This message will be suppressed on furthe

    php7.2以上 废除了 each()方法,项目中用到的地方会出现以下报错 The each() function is deprecated. This message will be suppre ...

  8. SET IDENTITY_INSERT的用法,具体去体验一下

    如果将值插入到表的标识列中,需要启用 SET IDENTITY_INSERT. 举例如下: 创建表Orders.Products,Orders表与Products表分别有标识列OrderID与Prod ...

  9. 小程序JS框架

  10. MFC无法使用CDialogEx类

    在stdafx.h中添加以下代码: #include <afxcontrolbars.h>