杨中科老师 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;
}

最新文章

  1. jQuery动画特效实例教程
  2. HTML5--div、span超出部分省略号显示
  3. CSS3魔法堂:CSS3滤镜及Canvas、SVG和IE滤镜替代方案详解[转]
  4. 验证时出错。HRESULT = &#39;8000000A&#39;
  5. Python自动化之sqlalchemy关联查询
  6. 通过JAVA反射,调用未知类的类方法
  7. JS中的工厂模式
  8. RabbitMQ学习(1):安装
  9. 记一次npapi插件无窗口(windowless )化下的妙巧思路然后解决问题的超爽体验过程
  10. java使用内部类的好处及其初始化
  11. [T]各种字符串Hash函数比较
  12. 腾讯文学动作密集 疑为手Q发力移动阅读铺路
  13. Android Studio 运行java程序
  14. C#互操作处理(一)
  15. chrome:禁用缓存
  16. 安装VNC
  17. UVA725 Division 除法【暴力】
  18. webpack4入门配置
  19. c的动态内存管理
  20. 这里包含几乎所有的xcode正式版本

热门文章

  1. C++四种强转
  2. SIP中OPTIONS方法的用法及示例
  3. Javascript 笔记与总结(2-3)Javascript 运算符、控制结构与对象操作
  4. ThinkPHP 学习笔记 ( 二 ) 控制器 ( Controller )
  5. jQuery 复合选择器的几个例子
  6. ios-滚动导航条页面
  7. vFloppy1.5-虚拟启动软盘
  8. git的某些默认行为--会推送pull的内容,即使commit的时候不显示
  9. 自己diy一个jquery分页插件
  10. 7Z命令行