杨中科老师 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的更多相关文章

  1. MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能

    MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...

  2. asp.net实现md5加密方法详解

    MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文. 例如:明文为:abcdefg 通过一些列运算 得到 密文 7ac66c0f148de9519b8bd264312c4d64 它具有两个特 ...

  3. asp.net实现md5加密

    MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文.在ASP.NET中MD5的加密方式很简单,详细介绍看下文 MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文.例如:明文为:ab ...

  4. 对字符串md5加密

    public String getMD5(String str) { try { // 生成一个MD5加密计算摘要 MessageDigest md = MessageDigest.getInstan ...

  5. Java语言编写MD5加密方法,Jmeter如何给字符串MD5加密

    package md5package; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; ...

  6. asp.net && javascript MD5加密

    /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...

  7. Asp.Net(C#) MD5 加密

    /// <summary> /// MD5 字符串加密  /// </summary> /// <param name="str">需要加密的字 ...

  8. java字符串MD5加密后再转16进制

    话不多说上码 pom.xml <!-- MD5 --> <dependency> <groupId>org.apache.commons</groupId&g ...

  9. Android学习笔记----Java字符串MD5加密

    代码如下: /** * MD5单向加密,32位,用于加密密码,因为明文密码在信道中传输不安全,明文保存在本地也不安全 * * @param str * @return */ public static ...

随机推荐

  1. Java判断一个时间是否在另一个时间段内

    需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @para ...

  2. SqlServer表数据与excel中数据的互相复制

    一.SqlServer表数据复制到excel 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键,复制(也可以点击连同标题复制),复制到记事本中(不然会乱码) 3.然后再把记事本的内容 ...

  3. Win10开始菜单打不开?两个办法可破

    很多人在安装Windows10后,都遇到过开始菜 单无法打开和Cortana框架无法输入文字的问题, 这种问题在系统更新后特别频繁.Windows会报错 为关键错误,并提示在下次登录会进行解决,同时要 ...

  4. IDM 通过防火墙规则阻止激活验证

    1. 打开Windows防火墙 2. 高级设置-->出站规则-->新建规则 3. 添加IDM程序路径,阻止连接 4. 在属性中添加作用域,远程IP地址: DNS解析出IP:register ...

  5. MySQL服务器状态变量

    一,最值得检查的状态变量使用show global status进行检测二.变量部分1.Aborted_clients如果这个变量持续增加,确定连接是否被关闭了.如果不是检查网络性能,并且检查max_ ...

  6. 【转】设计模式(十一)代理模式Proxy(结构型)

    设计模式(十一)代理模式Proxy(结构型) 1.概述 因为某个对象消耗太多资源,而且你的代码并不是每个逻辑路径都需要此对象, 你曾有过延迟创建对象的想法吗 ( if和else就是不同的两条逻辑路径) ...

  7. Google 开源项目风格指南

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  8. 获得sql对应的binary

    Declare @max Varbinary(max) select @Max = convert(Varbinary(max) , ' Select ' ) Print ' DECLARE @sql ...

  9. MySQL- 锁(3)

    InnoDB在不同隔离级别下的一致性读及锁的差异 前面讲过,锁和多版本数据是InnoDB实现一致性读和ISO/ANSI SQL92隔离级别的手段,因此,在不同的隔离级别下,InnoDB处理SQL时采用 ...

  10. hadoop-2.7.3 在windows环境下安装(无需Cygwin)

    http://blog.csdn.net/kokjuis/article/details/53537029