使用MD5加密字符串
一、概念:
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加密字符串的更多相关文章
- linux md5 加密字符串和文件方法
linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...
- Java生成MD5加密字符串代码实例
这篇文章主要介绍了Java生成MD5加密字符串代码实例,本文对MD5的作用作了一些介绍,然后给出了Java下生成MD5加密字符串的代码示例,需要的朋友可以参考下 (1)一般使用的数据库中都会保存用 ...
- iOS中MD5加密字符串实现
1.MD5加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321 ...
- C# MD5加密字符串
/// <summary> /// 用MD5加密字符串,可选择生成16位或者32位的加密字符串 /// </summary> /// <param name=" ...
- iOS MD5加密字符串
参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...
- C#中使用SHA1和MD5加密字符串
SHA1和MD5加密均为不可逆加密.代码如下: using System.Security.Cryptography; //添加Using static void Main(string[] args ...
- MD5加密字符串--基于python
import hashlib#md5加密32位def md5(str): import hashlib m = hashlib.md5() m.update(str) return m.hexdige ...
- MD5 加密字符串
public class MD5 { /*** * MD5加码 生成32位md5码 */ public static String string2MD5(String inStr){ MessageD ...
- C#加密方法汇总(SHA1加密字符串,MD5加密字符串,可逆加密等)
using System;using System.Collections.Generic;using System.Text; namespace StringEncry{ class Encode ...
随机推荐
- tensorflow 如何看shape
https://blog.csdn.net/yinxingtianxia/article/details/78121941 输入: x= tf.truncated_normal([, , ], dty ...
- 初始化html font-size
(function () { var docEl = document.documentElement, resizeEvt = 'orientationchange' in window ? 'or ...
- 【转】asp获取【微信公众平台】Access Token的源代码下载
在做微信开发时候,经常要用到Access Token,但是官网提供的都是基于php写的,我用asp写了,有需要可以直接复制去用,模板消息,jdk上传图片,客服消息等全需要这个:'获取 access_t ...
- 004-行为型-03-观察者模式(Observer)
一.概述 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象. 定义了对象之间的一对多依赖,让多个观察者对象同时监听某一 ...
- 002-05-RestTemplate 使用常见问题-非200请求捕获
一.概述 1.1.RestTemplate报错时时获取不到错误信息 捕获异常后 try { result = restTemplate.postForEntity(faceConfig.getApiU ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...
- RHEL7 的注册
RHEL7 的注册 2016年09月12日 20:37:19 wojiushiwoba 阅读数 13002更多 分类专栏: 嵌入式linux 版权声明:本文为博主原创文章,遵循CC 4.0 BY- ...
- springboot 项目基本目录包结构
1.基本目录结构 controller service impl mapper utils domain config interceoter(拦截器) dto
- [导航教程] [C#基类库大全]官方产品发布与源码下载---苏飞版
http://www.sufeinet.com/thread-655-1-1.html
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...