C# js asp.net 字符串MD5加密GetMD5Hash
杨中科老师 C#
/// <summary>
/// 把字符转换成MD5
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5Hash(String str)
{
//把字符串转换成字节数组
byte[] buffer = Encoding.Default.GetBytes(str); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
//md5加密
byte[] cryptBuffer = md5.ComputeHash(buffer);
string s = "";
//把每一个字节 0-255,转换成两位16进制数
for (int i = ; i < cryptBuffer.Length; i++)
{
//大X转黄的是大写字母,小X转换的是小写字母
s += cryptBuffer[i].ToString("x2");
}
return s;
}
赵小虎老师 C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO; namespace _02调用类库实现MD5值的计算
{
class Program
{
static void Main(string[] args)
{
#region 计算字符串的MD5值
//while (true)
//{
// Console.WriteLine("请输入一个字符串");
// string msg = Console.ReadLine();
// string md5String = GetMD5FromString(msg);
// Console.WriteLine(md5String);
//}
#endregion #region 计算文件的Md5
string path = @"d:\spring.xls";
Console.WriteLine(GetMD5FromFile(path));
Console.ReadLine();
#endregion
} /// <summary>
/// 计算字符串的MD5值
/// </summary>
/// <param name="msg">要计算的字符串</param>
/// <returns></returns>
private static string GetMD5FromString(string msg)
{ //1.创建一个用来计算MD5值的类的对象
using (MD5 md5 = MD5.Create())
{ //把字符串转换为byte[]
//注意:如果字符串中包含汉字,则这里会把汉字使用utf-8编码转换为byte[],当其他地方
//计算MD5值的时候,如果对汉字使用了不同的编码,则同样的汉字生成的byte[]是不一样的,所以计算出的MD5值也就不一样了。
byte[] msgBuffer = Encoding.Default.GetBytes(msg); //2.计算给定字符串的MD5值
//返回值就是就算后的MD5值,如何把一个长度为16的byte[]数组转换为一个长度为32的字符串:就是把每个byte转成16进制同时保留2位即可。
byte[] md5Buffer = md5.ComputeHash(msgBuffer);
md5.Clear();//释放资源 StringBuilder sbMd5 = new StringBuilder();
for (int i = ; i < md5Buffer.Length; i++)
{
sbMd5.Append(md5Buffer[i].ToString("x2"));
}
return sbMd5.ToString(); } } private static string GetMD5FromFile(string path)
{
using (MD5 md5 = MD5.Create())
{ using (FileStream fsRead = File.OpenRead(path))
{
byte[] bytes = md5.ComputeHash(fsRead);
md5.Clear();
StringBuilder sbMd5 = new StringBuilder();
for (int i = ; i < bytes.Length; i++)
{
sbMd5.Append(bytes[i].ToString("X2"));
}
return sbMd5.ToString();
} }
}
}
}
JS MD5加密
对 oldPwd 加密
hex_md5(oldPwd).toUpperCase()
JS文件:
/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*
*
* 使用方法 hex_md5("1").toUpperCase()
*/ /*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = ; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = ; /* bits per input character. 8 - ASCII; 16 - Unicode */ /*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); } /*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
} /*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> ] |= 0x80 << ((len) % );
x[(((len + ) >>> ) << ) + ] = len; var a = ;
var b = -;
var c = -;
var d = ; for(var i = ; i < x.length; i += )
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d; a = md5_ff(a, b, c, d, x[i+ ], , -);
d = md5_ff(d, a, b, c, x[i+ ], , -);
c = md5_ff(c, d, a, b, x[i+ ], , );
b = md5_ff(b, c, d, a, x[i+ ], , -);
a = md5_ff(a, b, c, d, x[i+ ], , -);
d = md5_ff(d, a, b, c, x[i+ ], , );
c = md5_ff(c, d, a, b, x[i+ ], , -);
b = md5_ff(b, c, d, a, x[i+ ], , -);
a = md5_ff(a, b, c, d, x[i+ ], , );
d = md5_ff(d, a, b, c, x[i+ ], , -);
c = md5_ff(c, d, a, b, x[i+], , -);
b = md5_ff(b, c, d, a, x[i+], , -);
a = md5_ff(a, b, c, d, x[i+], , );
d = md5_ff(d, a, b, c, x[i+], , -);
c = md5_ff(c, d, a, b, x[i+], , -);
b = md5_ff(b, c, d, a, x[i+], , ); a = md5_gg(a, b, c, d, x[i+ ], , -);
d = md5_gg(d, a, b, c, x[i+ ], , -);
c = md5_gg(c, d, a, b, x[i+], , );
b = md5_gg(b, c, d, a, x[i+ ], , -);
a = md5_gg(a, b, c, d, x[i+ ], , -);
d = md5_gg(d, a, b, c, x[i+], , );
c = md5_gg(c, d, a, b, x[i+], , -);
b = md5_gg(b, c, d, a, x[i+ ], , -);
a = md5_gg(a, b, c, d, x[i+ ], , );
d = md5_gg(d, a, b, c, x[i+], , -);
c = md5_gg(c, d, a, b, x[i+ ], , -);
b = md5_gg(b, c, d, a, x[i+ ], , );
a = md5_gg(a, b, c, d, x[i+], , -);
d = md5_gg(d, a, b, c, x[i+ ], , -);
c = md5_gg(c, d, a, b, x[i+ ], , );
b = md5_gg(b, c, d, a, x[i+], , -); a = md5_hh(a, b, c, d, x[i+ ], , -);
d = md5_hh(d, a, b, c, x[i+ ], , -);
c = md5_hh(c, d, a, b, x[i+], , );
b = md5_hh(b, c, d, a, x[i+], , -);
a = md5_hh(a, b, c, d, x[i+ ], , -);
d = md5_hh(d, a, b, c, x[i+ ], , );
c = md5_hh(c, d, a, b, x[i+ ], , -);
b = md5_hh(b, c, d, a, x[i+], , -);
a = md5_hh(a, b, c, d, x[i+], , );
d = md5_hh(d, a, b, c, x[i+ ], , -);
c = md5_hh(c, d, a, b, x[i+ ], , -);
b = md5_hh(b, c, d, a, x[i+ ], , );
a = md5_hh(a, b, c, d, x[i+ ], , -);
d = md5_hh(d, a, b, c, x[i+], , -);
c = md5_hh(c, d, a, b, x[i+], , );
b = md5_hh(b, c, d, a, x[i+ ], , -); a = md5_ii(a, b, c, d, x[i+ ], , -);
d = md5_ii(d, a, b, c, x[i+ ], , );
c = md5_ii(c, d, a, b, x[i+], , -);
b = md5_ii(b, c, d, a, x[i+ ], , -);
a = md5_ii(a, b, c, d, x[i+], , );
d = md5_ii(d, a, b, c, x[i+ ], , -);
c = md5_ii(c, d, a, b, x[i+], , -);
b = md5_ii(b, c, d, a, x[i+ ], , -);
a = md5_ii(a, b, c, d, x[i+ ], , );
d = md5_ii(d, a, b, c, x[i+], , -);
c = md5_ii(c, d, a, b, x[i+ ], , -);
b = md5_ii(b, c, d, a, x[i+], , );
a = md5_ii(a, b, c, d, x[i+ ], , -);
d = md5_ii(d, a, b, c, x[i+], , -);
c = md5_ii(c, d, a, b, x[i+ ], , );
b = md5_ii(b, c, d, a, x[i+ ], , -); a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d); } /*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
} /*
* Calculate the HMAC-MD5, of a key and some data
*/
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length > ) bkey = core_md5(bkey, key.length * chrsz); var ipad = Array(), opad = Array();
for(var i = ; i < ; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
} var hash = core_md5(ipad.concat(str2binl(data)), + data.length * chrsz);
return core_md5(opad.concat(hash), + );
} /*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> ) + (y >> ) + (lsw >> );
return (msw << ) | (lsw & 0xFFFF);
} /*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> ( - cnt));
} /*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = ( << chrsz) - ;
for(var i = ; i < str.length * chrsz; i += chrsz)
bin[i>>] |= (str.charCodeAt(i / chrsz) & mask) << (i%);
return bin;
} /*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str = "";
var mask = ( << chrsz) - ;
for(var i = ; i < bin.length * ; i += chrsz)
str += String.fromCharCode((bin[i>>] >>> (i % )) & mask);
return str;
} /*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = ; i < binarray.length * ; i++)
{
str += hex_tab.charAt((binarray[i>>] >> ((i%)*+)) & 0xF) +
hex_tab.charAt((binarray[i>>] >> ((i%)* )) & 0xF);
}
return str;
} /*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for(var i = ; i < binarray.length * ; i += )
{
var triplet = (((binarray[i >> ] >> * ( i %)) & 0xFF) << )
| (((binarray[i+ >> ] >> * ((i+)%)) & 0xFF) << )
| ((binarray[i+ >> ] >> * ((i+)%)) & 0xFF);
for(var j = ; j < ; j++)
{
if(i * + j * > binarray.length * ) str += b64pad;
else str += tab.charAt((triplet >> *(-j)) & 0x3F);
}
}
return str;
}
C# js asp.net 字符串MD5加密GetMD5Hash的更多相关文章
- MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能
MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...
- asp.net实现md5加密方法详解
MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文. 例如:明文为:abcdefg 通过一些列运算 得到 密文 7ac66c0f148de9519b8bd264312c4d64 它具有两个特 ...
- asp.net实现md5加密
MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文.在ASP.NET中MD5的加密方式很简单,详细介绍看下文 MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文.例如:明文为:ab ...
- 对字符串md5加密
public String getMD5(String str) { try { // 生成一个MD5加密计算摘要 MessageDigest md = MessageDigest.getInstan ...
- Java语言编写MD5加密方法,Jmeter如何给字符串MD5加密
package md5package; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; ...
- asp.net && javascript MD5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- Asp.Net(C#) MD5 加密
/// <summary> /// MD5 字符串加密 /// </summary> /// <param name="str">需要加密的字 ...
- java字符串MD5加密后再转16进制
话不多说上码 pom.xml <!-- MD5 --> <dependency> <groupId>org.apache.commons</groupId&g ...
- Android学习笔记----Java字符串MD5加密
代码如下: /** * MD5单向加密,32位,用于加密密码,因为明文密码在信道中传输不安全,明文保存在本地也不安全 * * @param str * @return */ public static ...
随机推荐
- jiffies
linux中的jiffies变量 全局变量jiffies用来记录自系统启动以来产生的节拍的总数.启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值.一秒内时钟中断的次数等于H ...
- 1. Programming in C is fun!
Programming in C is fun! #include <stdio.h> int main() { printf("Programming in C is fun! ...
- python实现查找指定文件
若不包含子目录的遍历: import glob for filename in glob.glob("/data/testdata/*.jpg"): print filename ...
- 关于APP接口设计
最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...
- Bootstrap页面布局23 - BS折叠内容
<div class='container-fluid'> <h3 class='page-header'>Bootstrap 折叠内容</h3> <!--如 ...
- 8.PHP内核探索:再次探讨SAPI
在PHP的生命周期的各个阶段,一些与服务相关的操作都是通过SAPI接口实现. 这些内置实现的物理位置在PHP源码的SAPI目录.这个目录存放了PHP对各个服务器抽象层的代码, 例如命令行程序的实现,A ...
- 图片lightbox2
1. 官网下载 http://lokeshdhakar.com/projects/lightbox2/ 2.引入 css jquery js 3. HTML格式 <a href=" ...
- MySQL常用SQL/函数汇总(持续更新)
自动生成ROWNUN SELECT (@rowNO := @rowNo+1) AS rowno,a.uuid FROM (SELECT * FROM h_log_proc) a,(SELECT @ro ...
- 【php学习】时间函数
手工画了一张图,来大体概括php中对于时间的处理函数 首先时间戳是这样“1441202665”的一串数字,虽然人看起来麻烦,但是计算机却很容易识别这样的时间表示形式. 所以给计算机看的时间是时间戳,给 ...
- 有序列表和无序列表、流、格式布局:position
列表方块: 有序列表和无序列表 ol/ul 例如<ol: style:"list-style:"" "> 1.<ol: style:&quo ...