日期:2014-05-16  浏览次数:20366 次

谁能发一个JS版MD5函数,网上找的加密中文时就和VB版结果不一样:(
百度、Google找过了,蓝色论坛上也搜索过了
加密非中文字符时JS版和VB版结果一样
一旦加密的是中文字符,就不一样:(

本想将JS版和VB版函数都发上来,太长了发不了
大家可到蓝色论坛上看下我发的

http://bbs.blueidea.com/thread-2771831-1-1.html

------解决方案--------------------
/***************************************************************************************
* md5加密算法
* 此代码版权归原作者所有,
*
* 如果您对本程序有什么建议,请email to:ocean@forever.net.cn。
*
* 海洋工作室
* http://www.oceanstudio.net
* ocean(ocean@forever.net.cn) 制作
*****************************************************************************************/
/*****************************************************************************************
* md5加密算法
* 原作者的版权声明如下, 我将原作者的代码封装到String中
* 具体使用请参见例子
* md5.js
* 调用方法:object.value=string.calcMD5();
* A JavaScript implementation of the RSA Data Security, Inc. MD5
* Message-Digest Algorithm.
*
* Copyright (C) Paul Johnston 1999. Distributed under the LGPL.
*****************************************************************************************/

/* to convert strings to a list of ascii values */
String.prototype.sAscii = " !\ "#$%& '()*+,-./0123456789:; <=> ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
/* convert integer to hex string */
String.prototype.sHex = "0123456789ABCDEF ";
String.prototype.hex = function (i) {
h = " ";
for(j = 0; j <= 3; j++)
h += this.sHex.charAt((i > > (j * 8 + 4)) & 0x0F) + this.sHex.charAt((i > > (j * 8)) & 0x0F);
// for(j = 3; j > = 0; j--)
// h += this.sHex.charAt((i > > (j * 8 + 4)) & 0x0F) + this.sHex.charAt((i > > (j * 8)) & 0x0F);
return h;
}

/* add, handling overflows correctly */
String.prototype.add = function (x, y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}

/* MD5 rounds functions */
String.prototype.R1 = function (A, B, C, D, X, S, T) {
q = this.add(this.add(A, (B & C) | (~B & D)), this.add(X, T));
return this.add((q < < S) | ((q > > (32 - S)) & (Math.pow(2, S) - 1)), B);
}

String.prototype.R2 = function (A, B, C, D, X, S, T) {
q = this.add(this.add(A, (B & D) | (C & ~D)), this.add(X, T));
return this.add((q < < S) | ((q > > (32 - S)) & (Math.pow(2, S) - 1)), B);
}

String.prototype.R3 = function (A, B, C, D, X, S, T) {
q = this.add(this.add(A, B ^ C ^ D), this.add(X, T));
return this.add((q < < S) | ((q > > (32 - S)) & (Math.pow(2, S) - 1)), B);
}

String.prototype.R4 = function (A, B, C, D, X, S, T) {
q = this.add(this.add(A, C ^ (B | ~D)), this.add(X, T));
return this.add((q < < S) | ((q > > (32 - S)) & (Math.pow(2, S) - 1)), B);
}

/* main entry point */
String.prototype.calcMD5 = function () {
var sInp = this.valueOf();

/* Calculate length in machine words, including padding */
wLen = (((sInp.length + 8) > > 6) + 1) < < 4;
var X = new Array(wLen);

/* Convert string to array of words */
j = 4;
for (i = 0; (i * 4) < sInp.length; i++) {
X[i] = 0;
for (j = 0; (j < 4) && ((j + i * 4) < sInp.length