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 ...
随机推荐
- jquery CDN(内容分发网络)使用
jquery CDN 给开发者提供一种捷径,即不下载jquary 就通过CDN能使用各个版本的jquery. 使用方法很简单,就是在HTML 文档中引用相关版本的jquery. 例如:我用百度的CDN ...
- 揪出Android流氓软件
揪出Android流氓软件 http://www.icpcw.com/Smartphone/Android/Android/1471/147142_all.htm http://www.william ...
- 用thinkphp进行微信开发的整体设计思考
用thinkphp进行微信开发的整体设计思考 http://www.2cto.com/weixin/201504/388423.html 2015-04-09 0个评论 作者:明 ...
- php调用empty出现错误Can't use function return value in write context
php调用empty出现错误Can't use function return value in write context 2012-10-28 09:33:22 | 11391次阅读 | 评论:0 ...
- Yii源码阅读笔记(十五)
Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...
- matches field ID value 17.9.1 The LABEL element
https://www.w3.org/TR/html401/interact/forms.html#edef-LABEL <!ELEMENT LABEL - - (%inline;)* -(LA ...
- android 设计工具栏
设计工具栏Action Bar(订制工具栏类型) 工具栏给用户提供了一种熟悉和可预测的方式来执行某种动作和操纵应用程序,但是这并不意味着它就需要和其他的应用程序看起来一样的.如果想设计工具栏以使得它能 ...
- P1032 字串变换
最近在练习bfs,看到了02年提高组的这个题,顿时来了兴致,联想到前一阵子的八数码问题,具体就是使用一个字符串来存储状态,把他存储到一个图中,然后开始bfs,如果10步之内无法完成就剪枝,同时使用哈希 ...
- Listener-监听器+ServletContext+ApplicationContext
参考资料 ServletContext和ApplicationContext有什么区别 ServletContext:是web容器的东西, 一个webapp一个, 比session作用范围要大, 从中 ...
- php curl流方式远程下载大文件
$url="http://*"; #下载文件 set_time_limit(0); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ...