关于MD5码的一些自我总结
MD5码现在使用的很广泛,其中不仅在web还是winform上。MD5码是不可逆的,虽然国内某大学女教师连破2种,但是她使用的碰撞算法,她也无法确定第二次就一定破的出来,所以就目前而言是安全的,不过时间长了就会有这种查询数据库出来也就不太安全了,目前有一些网站就可以查一些普通的密码,例如http://md5.rednoize.com/ 查不到的就可以添加进去,这样时间长了就可以查出来。
下面是一个md5算法
这个是C#中的,web中的FormsAuthentication.HashPasswordForStoringInConfigFile(字符串,"MD5")也可以实现,还有利用C#中函数实现的
但是我在做支付宝在线支付的时候使用中文老提示错误,就是算法对中文不太支持,于是我转换为utf-8后再编码问题依然在,但使用vbscript的这个算法问题就解决了。
后来我对比了一下Gb2312和GBK的字符表,就发现还是编码查找方式不同而引发的问题,现在正在研究C#中支持GBK编码的方法。
下面这个是VB.Net中的使用函数的实现办法
现在动网论坛也使用的比较多,它使用的16位的MD5加密方式,从上面的加密算法我们可以看到它是由4组8位的密码组成,而16位就是取其中的2组合并的,不同的设置就取不同位置的密码来组合,下面是在外部插入动网数据库生成随机密码的代码。
这里是一个朋友的加密成字符密码的算法
其实密码就是几种情况,位移,叠加,无非就是多几次,以及加减乘除的内容。最近在弄md5密码数据库,程序弄好勒,结果一运行机器死了,内存暴涨,最后还是算勒,呵呵。
下面是一个md5算法
MD5算法
//static state variables
private static UInt32 A;
private static UInt32 B;
private static UInt32 C;
private static UInt32 D;
//number of bits to rotate in tranforming
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
/* F, G, H and I are basic MD5 functions.
* 四个非线性函数:
*
* F(X,Y,Z) =(X&Y)|((~X)&Z)
* G(X,Y,Z) =(X&Z)|(Y&(~Z))
* H(X,Y,Z) =X^Y^Z
* I(X,Y,Z)=Y^(X|(~Z))
*
* (&与,|或,~非,^异或)
*/
private static UInt32 F(UInt32 x,UInt32 y,UInt32 z)
{
return (x&y)|((~x)&z);
}
private static UInt32 G(UInt32 x,UInt32 y,UInt32 z)
{
return (x&z)|(y&(~z));
}
private static UInt32 H(UInt32 x,UInt32 y,UInt32 z)
{
return x^y^z;
}
private static UInt32 I(UInt32 x,UInt32 y,UInt32 z)
{
return y^(x|(~z));
}
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
private static void FF(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + F(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void GG(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + G(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void HH(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + H(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void II(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + I(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void MD5_Init()
{
A=0x67452301; //in memory, this is 0x01234567
B=0xefcdab89; //in memory, this is 0x89abcdef
C=0x98badcfe; //in memory, this is 0xfedcba98
D=0x10325476; //in memory, this is 0x76543210
}
private static UInt32[] MD5_Append(byte[] input)
{
int zeros=0;
int ones =1;
int size=0;
int n = input.Length;
int m = n%64;
if( m < 56 )
{
zeros = 55-m;
size=n-m+64;
}
else if (m==56)
{
zeros = 0;
ones = 0;
size=n+8;
}
else
{
zeros = 63-m+56;
size=n+64-m+64;
}
ArrayList bs = new ArrayList(input);
if(ones==1)
{
bs.Add( (byte)0x80 ); // 0x80 = $10000000
}
for(int i=0;i<zeros;i++)
{
bs.Add( (byte)0 );
}
UInt64 N = (UInt64) n * 8;
byte h1=(byte)(N&0xFF);
byte h2=(byte)((N>>8)&0xFF);
byte h3=(byte)((N>>16)&0xFF);
byte h4=(byte)((N>>24)&0xFF);
byte h5=(byte)((N>>32)&0xFF);
byte h6=(byte)((N>>40)&0xFF);
byte h7=(byte)((N>>48)&0xFF);
byte h8=(byte)(N>>56);
bs.Add(h1);
bs.Add(h2);
bs.Add(h3);
bs.Add(h4);
bs.Add(h5);
bs.Add(h6);
bs.Add(h7);
bs.Add(h8);
byte[] ts=(byte[])bs.ToArray(typeof(byte));
/* Decodes input (byte[]) into output (UInt32[]). Assumes len is
* a multiple of 4.
*/
UInt32[] output = new UInt32[size/4];
for(Int64 i=0,j=0;i<size;j++,i+=4)
{
output[j]=(UInt32)(ts[i] | ts[i+1]<<8 | ts[i+2]<<16 | ts[i+3]<<24);
}
return output;
}
private static UInt32[] MD5_Trasform(UInt32[] x)
{
UInt32 a,b,c,d;
for(int k=0;k<x.Length;k+=16)
{
a=A;
b=B;
c=C;
d=D;
/* Round 1 */
FF (ref a, b, c, d, x[k+ 0], S11, 0xd76aa478); /* 1 */
FF (ref d, a, b, c, x[k+ 1], S12, 0xe8c7b756); /* 2 */
FF (ref c, d, a, b, x[k+ 2], S13, 0x242070db); /* 3 */
FF (ref b, c, d, a, x[k+ 3], S14, 0xc1bdceee); /* 4 */
FF (ref a, b, c, d, x[k+ 4], S11, 0xf57c0faf); /* 5 */
FF (ref d, a, b, c, x[k+ 5], S12, 0x4787c62a); /* 6 */
FF (ref c, d, a, b, x[k+ 6], S13, 0xa8304613); /* 7 */
FF (ref b, c, d, a, x[k+ 7], S14, 0xfd469501); /* 8 */
FF (ref a, b, c, d, x[k+ 8], S11, 0x698098d8); /* 9 */
FF (ref d, a, b, c, x[k+ 9], S12, 0x8b44f7af); /* 10 */
FF (ref c, d, a, b, x[k+10], S13, 0xffff5bb1); /* 11 */
FF (ref b, c, d, a, x[k+11], S14, 0x895cd7be); /* 12 */
FF (ref a, b, c, d, x[k+12], S11, 0x6b901122); /* 13 */
FF (ref d, a, b, c, x[k+13], S12, 0xfd987193); /* 14 */
FF (ref c, d, a, b, x[k+14], S13, 0xa679438e); /* 15 */
FF (ref b, c, d, a, x[k+15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (ref a, b, c, d, x[k+ 1], S21, 0xf61e2562); /* 17 */
GG (ref d, a, b, c, x[k+ 6], S22, 0xc040b340); /* 18 */
GG (ref c, d, a, b, x[k+11], S23, 0x265e5a51); /* 19 */
GG (ref b, c, d, a, x[k+ 0], S24, 0xe9b6c7aa); /* 20 */
GG (ref a, b, c, d, x[k+ 5], S21, 0xd62f105d); /* 21 */
GG (ref d, a, b, c, x[k+10], S22, 0x2441453); /* 22 */
GG (ref c, d, a, b, x[k+15], S23, 0xd8a1e681); /* 23 */
GG (ref b, c, d, a, x[k+ 4], S24, 0xe7d3fbc8); /* 24 */
GG (ref a, b, c, d, x[k+ 9], S21, 0x21e1cde6); /* 25 */
GG (ref d, a, b, c, x[k+14], S22, 0xc33707d6); /* 26 */
GG (ref c, d, a, b, x[k+ 3], S23, 0xf4d50d87); /* 27 */
GG (ref b, c, d, a, x[k+ 8], S24, 0x455a14ed); /* 28 */
GG (ref a, b, c, d, x[k+13], S21, 0xa9e3e905); /* 29 */
GG (ref d, a, b, c, x[k+ 2], S22, 0xfcefa3f8); /* 30 */
GG (ref c, d, a, b, x[k+ 7], S23, 0x676f02d9); /* 31 */
GG (ref b, c, d, a, x[k+12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (ref a, b, c, d, x[k+ 5], S31, 0xfffa3942); /* 33 */
HH (ref d, a, b, c, x[k+ 8], S32, 0x8771f681); /* 34 */
HH (ref c, d, a, b, x[k+11], S33, 0x6d9d6122); /* 35 */
HH (ref b, c, d, a, x[k+14], S34, 0xfde5380c); /* 36 */
HH (ref a, b, c, d, x[k+ 1], S31, 0xa4beea44); /* 37 */
HH (ref d, a, b, c, x[k+ 4], S32, 0x4bdecfa9); /* 38 */
HH (ref c, d, a, b, x[k+ 7], S33, 0xf6bb4b60); /* 39 */
HH (ref b, c, d, a, x[k+10], S34, 0xbebfbc70); /* 40 */
HH (ref a, b, c, d, x[k+13], S31, 0x289b7ec6); /* 41 */
HH (ref d, a, b, c, x[k+ 0], S32, 0xeaa127fa); /* 42 */
HH (ref c, d, a, b, x[k+ 3], S33, 0xd4ef3085); /* 43 */
HH (ref b, c, d, a, x[k+ 6], S34, 0x4881d05); /* 44 */
HH (ref a, b, c, d, x[k+ 9], S31, 0xd9d4d039); /* 45 */
HH (ref d, a, b, c, x[k+12], S32, 0xe6db99e5); /* 46 */
HH (ref c, d, a, b, x[k+15], S33, 0x1fa27cf8); /* 47 */
HH (ref b, c, d, a, x[k+ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (ref a, b, c, d, x[k+ 0], S41, 0xf4292244); /* 49 */
II (ref d, a, b, c, x[k+ 7], S42, 0x432aff97); /* 50 */
II (ref c, d, a, b, x[k+14], S43, 0xab9423a7); /* 51 */
II (ref b, c, d, a, x[k+ 5], S44, 0xfc93a039); /* 52 */
II (ref a, b, c, d, x[k+12], S41, 0x655b59c3); /* 53 */
II (ref d, a, b, c, x[k+ 3], S42, 0x8f0ccc92); /* 54 */
II (ref c, d, a, b, x[k+10], S43, 0xffeff47d); /* 55 */
II (ref b, c, d, a, x[k+ 1], S44, 0x85845dd1); /* 56 */
II (ref a, b, c, d, x[k+ 8], S41, 0x6fa87e4f); /* 57 */
II (ref d, a, b, c, x[k+15], S42, 0xfe2ce6e0); /* 58 */
II (ref c, d, a, b, x[k+ 6], S43, 0xa3014314); /* 59 */
II (ref b, c, d, a, x[k+13], S44, 0x4e0811a1); /* 60 */
II (ref a, b, c, d, x[k+ 4], S41, 0xf7537e82); /* 61 */
II (ref d, a, b, c, x[k+11], S42, 0xbd3af235); /* 62 */
II (ref c, d, a, b, x[k+ 2], S43, 0x2ad7d2bb); /* 63 */
II (ref b, c, d, a, x[k+ 9], S44, 0xeb86d391); /* 64 */
A+=a;
B+=b;
C+=c;
D+=d;
}
return new UInt32[]{A,B,C,D};
}
public static byte[] MD5Array(byte[] input)
{
MD5_Init();
UInt32[] block = MD5_Append(input);
UInt32[] bits = MD5_Trasform(block);
/* Encodes bits (UInt32[]) into output (byte[]). Assumes len is
* a multiple of 4.
*/
byte[] output=new byte[bits.Length*4];
for(int i=0,j=0;i<bits.Length;i++,j+=4)
{
output[j] = (byte)(bits[i] & 0xff);
output[j+1] = (byte)((bits[i] >> 8) & 0xff);
output[j+2] = (byte)((bits[i] >> 16) & 0xff);
output[j+3] = (byte)((bits[i] >> 24) & 0xff);
}
return output;
}
public static string ArrayToHexString(byte[] array,bool uppercase)
{
string hexString="";
string format="x2";
if(uppercase)
{
format="X2";
}
foreach(byte b in array)
{
hexString += b.ToString(format);
}
return hexString;
}
public static string MDString(string message)
{
char[] c = message.ToCharArray();
byte[] b = new byte[c.Length];
for(int i=0;i<c.Length;i++)
{
b[i]=(byte)c[i];
}
byte[] digest = MD5Array(b);
return ArrayToHexString(digest,false);
}
public static string MDFile(string fileName)
{
FileStream fs=File.Open(fileName,FileMode.Open,FileAccess.Read);
byte[] array=new byte[fs.Length];
fs.Read(array,0,(int)fs.Length);
byte[] digest = MD5Array(array);
fs.Close();
return ArrayToHexString(digest,false);
}
}
//static state variables
private static UInt32 A;
private static UInt32 B;
private static UInt32 C;
private static UInt32 D;
//number of bits to rotate in tranforming
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
/* F, G, H and I are basic MD5 functions.
* 四个非线性函数:
*
* F(X,Y,Z) =(X&Y)|((~X)&Z)
* G(X,Y,Z) =(X&Z)|(Y&(~Z))
* H(X,Y,Z) =X^Y^Z
* I(X,Y,Z)=Y^(X|(~Z))
*
* (&与,|或,~非,^异或)
*/
private static UInt32 F(UInt32 x,UInt32 y,UInt32 z)
{
return (x&y)|((~x)&z);
}
private static UInt32 G(UInt32 x,UInt32 y,UInt32 z)
{
return (x&z)|(y&(~z));
}
private static UInt32 H(UInt32 x,UInt32 y,UInt32 z)
{
return x^y^z;
}
private static UInt32 I(UInt32 x,UInt32 y,UInt32 z)
{
return y^(x|(~z));
}
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
private static void FF(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + F(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void GG(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + G(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void HH(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + H(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void II(ref UInt32 a,UInt32 b,UInt32 c,UInt32 d,UInt32 mj,int s,UInt32 ti)
{
a = a + I(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void MD5_Init()
{
A=0x67452301; //in memory, this is 0x01234567
B=0xefcdab89; //in memory, this is 0x89abcdef
C=0x98badcfe; //in memory, this is 0xfedcba98
D=0x10325476; //in memory, this is 0x76543210
}
private static UInt32[] MD5_Append(byte[] input)
{
int zeros=0;
int ones =1;
int size=0;
int n = input.Length;
int m = n%64;
if( m < 56 )
{
zeros = 55-m;
size=n-m+64;
}
else if (m==56)
{
zeros = 0;
ones = 0;
size=n+8;
}
else
{
zeros = 63-m+56;
size=n+64-m+64;
}
ArrayList bs = new ArrayList(input);
if(ones==1)
{
bs.Add( (byte)0x80 ); // 0x80 = $10000000
}
for(int i=0;i<zeros;i++)
{
bs.Add( (byte)0 );
}
UInt64 N = (UInt64) n * 8;
byte h1=(byte)(N&0xFF);
byte h2=(byte)((N>>8)&0xFF);
byte h3=(byte)((N>>16)&0xFF);
byte h4=(byte)((N>>24)&0xFF);
byte h5=(byte)((N>>32)&0xFF);
byte h6=(byte)((N>>40)&0xFF);
byte h7=(byte)((N>>48)&0xFF);
byte h8=(byte)(N>>56);
bs.Add(h1);
bs.Add(h2);
bs.Add(h3);
bs.Add(h4);
bs.Add(h5);
bs.Add(h6);
bs.Add(h7);
bs.Add(h8);
byte[] ts=(byte[])bs.ToArray(typeof(byte));
/* Decodes input (byte[]) into output (UInt32[]). Assumes len is
* a multiple of 4.
*/
UInt32[] output = new UInt32[size/4];
for(Int64 i=0,j=0;i<size;j++,i+=4)
{
output[j]=(UInt32)(ts[i] | ts[i+1]<<8 | ts[i+2]<<16 | ts[i+3]<<24);
}
return output;
}
private static UInt32[] MD5_Trasform(UInt32[] x)
{
UInt32 a,b,c,d;
for(int k=0;k<x.Length;k+=16)
{
a=A;
b=B;
c=C;
d=D;
/* Round 1 */
FF (ref a, b, c, d, x[k+ 0], S11, 0xd76aa478); /* 1 */
FF (ref d, a, b, c, x[k+ 1], S12, 0xe8c7b756); /* 2 */
FF (ref c, d, a, b, x[k+ 2], S13, 0x242070db); /* 3 */
FF (ref b, c, d, a, x[k+ 3], S14, 0xc1bdceee); /* 4 */
FF (ref a, b, c, d, x[k+ 4], S11, 0xf57c0faf); /* 5 */
FF (ref d, a, b, c, x[k+ 5], S12, 0x4787c62a); /* 6 */
FF (ref c, d, a, b, x[k+ 6], S13, 0xa8304613); /* 7 */
FF (ref b, c, d, a, x[k+ 7], S14, 0xfd469501); /* 8 */
FF (ref a, b, c, d, x[k+ 8], S11, 0x698098d8); /* 9 */
FF (ref d, a, b, c, x[k+ 9], S12, 0x8b44f7af); /* 10 */
FF (ref c, d, a, b, x[k+10], S13, 0xffff5bb1); /* 11 */
FF (ref b, c, d, a, x[k+11], S14, 0x895cd7be); /* 12 */
FF (ref a, b, c, d, x[k+12], S11, 0x6b901122); /* 13 */
FF (ref d, a, b, c, x[k+13], S12, 0xfd987193); /* 14 */
FF (ref c, d, a, b, x[k+14], S13, 0xa679438e); /* 15 */
FF (ref b, c, d, a, x[k+15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (ref a, b, c, d, x[k+ 1], S21, 0xf61e2562); /* 17 */
GG (ref d, a, b, c, x[k+ 6], S22, 0xc040b340); /* 18 */
GG (ref c, d, a, b, x[k+11], S23, 0x265e5a51); /* 19 */
GG (ref b, c, d, a, x[k+ 0], S24, 0xe9b6c7aa); /* 20 */
GG (ref a, b, c, d, x[k+ 5], S21, 0xd62f105d); /* 21 */
GG (ref d, a, b, c, x[k+10], S22, 0x2441453); /* 22 */
GG (ref c, d, a, b, x[k+15], S23, 0xd8a1e681); /* 23 */
GG (ref b, c, d, a, x[k+ 4], S24, 0xe7d3fbc8); /* 24 */
GG (ref a, b, c, d, x[k+ 9], S21, 0x21e1cde6); /* 25 */
GG (ref d, a, b, c, x[k+14], S22, 0xc33707d6); /* 26 */
GG (ref c, d, a, b, x[k+ 3], S23, 0xf4d50d87); /* 27 */
GG (ref b, c, d, a, x[k+ 8], S24, 0x455a14ed); /* 28 */
GG (ref a, b, c, d, x[k+13], S21, 0xa9e3e905); /* 29 */
GG (ref d, a, b, c, x[k+ 2], S22, 0xfcefa3f8); /* 30 */
GG (ref c, d, a, b, x[k+ 7], S23, 0x676f02d9); /* 31 */
GG (ref b, c, d, a, x[k+12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (ref a, b, c, d, x[k+ 5], S31, 0xfffa3942); /* 33 */
HH (ref d, a, b, c, x[k+ 8], S32, 0x8771f681); /* 34 */
HH (ref c, d, a, b, x[k+11], S33, 0x6d9d6122); /* 35 */
HH (ref b, c, d, a, x[k+14], S34, 0xfde5380c); /* 36 */
HH (ref a, b, c, d, x[k+ 1], S31, 0xa4beea44); /* 37 */
HH (ref d, a, b, c, x[k+ 4], S32, 0x4bdecfa9); /* 38 */
HH (ref c, d, a, b, x[k+ 7], S33, 0xf6bb4b60); /* 39 */
HH (ref b, c, d, a, x[k+10], S34, 0xbebfbc70); /* 40 */
HH (ref a, b, c, d, x[k+13], S31, 0x289b7ec6); /* 41 */
HH (ref d, a, b, c, x[k+ 0], S32, 0xeaa127fa); /* 42 */
HH (ref c, d, a, b, x[k+ 3], S33, 0xd4ef3085); /* 43 */
HH (ref b, c, d, a, x[k+ 6], S34, 0x4881d05); /* 44 */
HH (ref a, b, c, d, x[k+ 9], S31, 0xd9d4d039); /* 45 */
HH (ref d, a, b, c, x[k+12], S32, 0xe6db99e5); /* 46 */
HH (ref c, d, a, b, x[k+15], S33, 0x1fa27cf8); /* 47 */
HH (ref b, c, d, a, x[k+ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (ref a, b, c, d, x[k+ 0], S41, 0xf4292244); /* 49 */
II (ref d, a, b, c, x[k+ 7], S42, 0x432aff97); /* 50 */
II (ref c, d, a, b, x[k+14], S43, 0xab9423a7); /* 51 */
II (ref b, c, d, a, x[k+ 5], S44, 0xfc93a039); /* 52 */
II (ref a, b, c, d, x[k+12], S41, 0x655b59c3); /* 53 */
II (ref d, a, b, c, x[k+ 3], S42, 0x8f0ccc92); /* 54 */
II (ref c, d, a, b, x[k+10], S43, 0xffeff47d); /* 55 */
II (ref b, c, d, a, x[k+ 1], S44, 0x85845dd1); /* 56 */
II (ref a, b, c, d, x[k+ 8], S41, 0x6fa87e4f); /* 57 */
II (ref d, a, b, c, x[k+15], S42, 0xfe2ce6e0); /* 58 */
II (ref c, d, a, b, x[k+ 6], S43, 0xa3014314); /* 59 */
II (ref b, c, d, a, x[k+13], S44, 0x4e0811a1); /* 60 */
II (ref a, b, c, d, x[k+ 4], S41, 0xf7537e82); /* 61 */
II (ref d, a, b, c, x[k+11], S42, 0xbd3af235); /* 62 */
II (ref c, d, a, b, x[k+ 2], S43, 0x2ad7d2bb); /* 63 */
II (ref b, c, d, a, x[k+ 9], S44, 0xeb86d391); /* 64 */
A+=a;
B+=b;
C+=c;
D+=d;
}
return new UInt32[]{A,B,C,D};
}
public static byte[] MD5Array(byte[] input)
{
MD5_Init();
UInt32[] block = MD5_Append(input);
UInt32[] bits = MD5_Trasform(block);
/* Encodes bits (UInt32[]) into output (byte[]). Assumes len is
* a multiple of 4.
*/
byte[] output=new byte[bits.Length*4];
for(int i=0,j=0;i<bits.Length;i++,j+=4)
{
output[j] = (byte)(bits[i] & 0xff);
output[j+1] = (byte)((bits[i] >> 8) & 0xff);
output[j+2] = (byte)((bits[i] >> 16) & 0xff);
output[j+3] = (byte)((bits[i] >> 24) & 0xff);
}
return output;
}
public static string ArrayToHexString(byte[] array,bool uppercase)
{
string hexString="";
string format="x2";
if(uppercase)
{
format="X2";
}
foreach(byte b in array)
{
hexString += b.ToString(format);
}
return hexString;
}
public static string MDString(string message)
{
char[] c = message.ToCharArray();
byte[] b = new byte[c.Length];
for(int i=0;i<c.Length;i++)
{
b[i]=(byte)c[i];
}
byte[] digest = MD5Array(b);
return ArrayToHexString(digest,false);
}
public static string MDFile(string fileName)
{
FileStream fs=File.Open(fileName,FileMode.Open,FileAccess.Read);
byte[] array=new byte[fs.Length];
fs.Read(array,0,(int)fs.Length);
byte[] digest = MD5Array(array);
fs.Close();
return ArrayToHexString(digest,false);
}
}
这个是C#中的,web中的FormsAuthentication.HashPasswordForStoringInConfigFile(字符串,"MD5")也可以实现,还有利用C#中函数实现的
函数实现
public string GetMD5(string s,string encoding)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding(encoding).GetBytes(s));
System.Text.StringBuilder sb = new System.Text.StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString().ToLower();
}
public string GetMD5(string s,string encoding)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding(encoding).GetBytes(s));
System.Text.StringBuilder sb = new System.Text.StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString().ToLower();
}
但是我在做支付宝在线支付的时候使用中文老提示错误,就是算法对中文不太支持,于是我转换为utf-8后再编码问题依然在,但使用vbscript的这个算法问题就解决了。
vb中的算法
Class MD5
Private BITS_TO_A_BYTE
Private BYTES_TO_A_WORD
Private BITS_TO_A_WORD
Private m_lOnBits(30)
Private m_l2Power(30)
'初始化变量
Public Sub MD5_Initialize()
BITS_TO_A_BYTE = 8
BYTES_TO_A_WORD = 4
BITS_TO_A_WORD = 32
End Sub
Private Function LShift(lValue, iShiftBits)
If iShiftBits = 0 Then
LShift = lValue
Exit Function
ElseIf iShiftBits = 31 Then
If lValue And 1 Then
LShift = &H80000000
Else
LShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
If (lValue And m_l2Power(31 - iShiftBits)) Then
LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
Else
LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
End If
End Function
Private Function str2bin(varstr)
Dim varasc
Dim i
Dim varchar
Dim varlow
Dim varhigh
str2bin=""
For i=1 To Len(varstr)
varchar=mid(varstr,i,1)
varasc = Asc(varchar)
If varasc<0 Then
varasc = varasc + 65535
End If
If varasc>255 Then
varlow = Left(Hex(Asc(varchar)),2)
varhigh = right(Hex(Asc(varchar)),2)
str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)
Else
str2bin = str2bin & chrB(AscB(varchar))
End If
Next
End Function
Private Function RShift(lValue, iShiftBits)
If iShiftBits = 0 Then
RShift = lValue
Exit Function
ElseIf iShiftBits = 31 Then
If lValue And &H80000000 Then
RShift = 1
Else
RShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
If (lValue And &H80000000) Then
RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
End If
End Function
Private Function RotateLeft(lValue, iShiftBits)
RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
End Function
Private Function AddUnsigned(lX, lY)
Dim lX4
Dim lY4
Dim lX8
Dim lY8
Dim lResult
lX8 = lX And &H80000000
lY8 = lY And &H80000000
lX4 = lX And &H40000000
lY4 = lY And &H40000000
lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
If lX4 And lY4 Then
lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
ElseIf lX4 Or lY4 Then
If lResult And &H40000000 Then
lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
Else
lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
End If
Else
lResult = lResult Xor lX8 Xor lY8
End If
AddUnsigned = lResult
End Function
Private Function md5_F(x, y, z)
md5_F = (x And y) Or ((Not x) And z)
End Function
Private Function md5_G(x, y, z)
md5_G = (x And z) Or (y And (Not z))
End Function
Private Function md5_H(x, y, z)
md5_H = (x Xor y Xor z)
End Function
Private Function md5_I(x, y, z)
md5_I = (y Xor (x Or (Not z)))
End Function
Private Sub md5_FF(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_GG(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_HH(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_II(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Function ConvertToWordArray(sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = LenB(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(AscB(MidB(sMessage, lByteCount + 1, 1)), lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
ConvertToWordArray = lWordArray
End Function
Private Function WordToHex(lValue)
Dim lByte
Dim lCount
For lCount = 0 To 3
lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
Next
End Function
Public Function MD5(sMessage)
m_lOnBits(0) = CLng(1)
m_lOnBits(1) = CLng(3)
m_lOnBits(2) = CLng(7)
m_lOnBits(3) = CLng(15)
m_lOnBits(4) = CLng(31)
m_lOnBits(5) = CLng(63)
m_lOnBits(6) = CLng(127)
m_lOnBits(7) = CLng(255)
m_lOnBits(8) = CLng(511)
m_lOnBits(9) = CLng(1023)
m_lOnBits(10) = CLng(2047)
m_lOnBits(11) = CLng(4095)
m_lOnBits(12) = CLng(8191)
m_lOnBits(13) = CLng(16383)
m_lOnBits(14) = CLng(32767)
m_lOnBits(15) = CLng(65535)
m_lOnBits(16) = CLng(131071)
m_lOnBits(17) = CLng(262143)
m_lOnBits(18) = CLng(524287)
m_lOnBits(19) = CLng(1048575)
m_lOnBits(20) = CLng(2097151)
m_lOnBits(21) = CLng(4194303)
m_lOnBits(22) = CLng(8388607)
m_lOnBits(23) = CLng(16777215)
m_lOnBits(24) = CLng(33554431)
m_lOnBits(25) = CLng(67108863)
m_lOnBits(26) = CLng(134217727)
m_lOnBits(27) = CLng(268435455)
m_lOnBits(28) = CLng(536870911)
m_lOnBits(29) = CLng(1073741823)
m_lOnBits(30) = CLng(2147483647)
m_l2Power(0) = CLng(1)
m_l2Power(1) = CLng(2)
m_l2Power(2) = CLng(4)
m_l2Power(3) = CLng(8)
m_l2Power(4) = CLng(16)
m_l2Power(5) = CLng(32)
m_l2Power(6) = CLng(64)
m_l2Power(7) = CLng(128)
m_l2Power(8) = CLng(256)
m_l2Power(9) = CLng(512)
m_l2Power(10) = CLng(1024)
m_l2Power(11) = CLng(2048)
m_l2Power(12) = CLng(4096)
m_l2Power(13) = CLng(8192)
m_l2Power(14) = CLng(16384)
m_l2Power(15) = CLng(32768)
m_l2Power(16) = CLng(65536)
m_l2Power(17) = CLng(131072)
m_l2Power(18) = CLng(262144)
m_l2Power(19) = CLng(524288)
m_l2Power(20) = CLng(1048576)
m_l2Power(21) = CLng(2097152)
m_l2Power(22) = CLng(4194304)
m_l2Power(23) = CLng(8388608)
m_l2Power(24) = CLng(16777216)
m_l2Power(25) = CLng(33554432)
m_l2Power(26) = CLng(67108864)
m_l2Power(27) = CLng(134217728)
m_l2Power(28) = CLng(268435456)
m_l2Power(29) = CLng(536870912)
m_l2Power(30) = CLng(1073741824)
Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim a
Dim b
Dim c
Dim d
Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21
x = ConvertToWordArray(str2bin(sMessage))
a = &H67452301
b = &HEFCDAB89
c = &H98BADCFE
d = &H10325476
For k = 0 To UBound(x) Step 16
AA = a
BB = b
CC = c
DD = d
md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478
md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756
md5_FF c, d, a, b, x(k + 2), S13, &H242070DB
md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A
md5_FF c, d, a, b, x(k + 6), S13, &HA8304613
md5_FF b, c, d, a, x(k + 7), S14, &HFD469501
md5_FF a, b, c, d, x(k + 8), S11, &H698098D8
md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE
md5_FF a, b, c, d, x(k + 12), S11, &H6B901122
md5_FF d, a, b, c, x(k + 13), S12, &HFD987193
md5_FF c, d, a, b, x(k + 14), S13, &HA679438E
md5_FF b, c, d, a, x(k + 15), S14, &H49B40821
md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562
md5_GG d, a, b, c, x(k + 6), S22, &HC040B340
md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51
md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D
md5_GG d, a, b, c, x(k + 10), S22, &H2441453
md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681
md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6
md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87
md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED
md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905
md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9
md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942
md5_HH d, a, b, c, x(k + 8), S32, &H8771F681
md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122
md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C
md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6
md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA
md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085
md5_HH b, c, d, a, x(k + 6), S34, &H4881D05
md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039
md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665
md5_II a, b, c, d, x(k + 0), S41, &HF4292244
md5_II d, a, b, c, x(k + 7), S42, &H432AFF97
md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7
md5_II b, c, d, a, x(k + 5), S44, &HFC93A039
md5_II a, b, c, d, x(k + 12), S41, &H655B59C3
md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92
md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D
md5_II b, c, d, a, x(k + 1), S44, &H85845DD1
md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F
md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
md5_II c, d, a, b, x(k + 6), S43, &HA3014314
md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1
md5_II a, b, c, d, x(k + 4), S41, &HF7537E82
md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235
md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
md5_II b, c, d, a, x(k + 9), S44, &HEB86D391
a = AddUnsigned(a, AA)
b = AddUnsigned(b, BB)
c = AddUnsigned(c, CC)
d = AddUnsigned(d, DD)
Next
MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
End Function
End Class
Class MD5
Private BITS_TO_A_BYTE
Private BYTES_TO_A_WORD
Private BITS_TO_A_WORD
Private m_lOnBits(30)
Private m_l2Power(30)
'初始化变量
Public Sub MD5_Initialize()
BITS_TO_A_BYTE = 8
BYTES_TO_A_WORD = 4
BITS_TO_A_WORD = 32
End Sub
Private Function LShift(lValue, iShiftBits)
If iShiftBits = 0 Then
LShift = lValue
Exit Function
ElseIf iShiftBits = 31 Then
If lValue And 1 Then
LShift = &H80000000
Else
LShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
If (lValue And m_l2Power(31 - iShiftBits)) Then
LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
Else
LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
End If
End Function
Private Function str2bin(varstr)
Dim varasc
Dim i
Dim varchar
Dim varlow
Dim varhigh
str2bin=""
For i=1 To Len(varstr)
varchar=mid(varstr,i,1)
varasc = Asc(varchar)
If varasc<0 Then
varasc = varasc + 65535
End If
If varasc>255 Then
varlow = Left(Hex(Asc(varchar)),2)
varhigh = right(Hex(Asc(varchar)),2)
str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)
Else
str2bin = str2bin & chrB(AscB(varchar))
End If
Next
End Function
Private Function RShift(lValue, iShiftBits)
If iShiftBits = 0 Then
RShift = lValue
Exit Function
ElseIf iShiftBits = 31 Then
If lValue And &H80000000 Then
RShift = 1
Else
RShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
If (lValue And &H80000000) Then
RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
End If
End Function
Private Function RotateLeft(lValue, iShiftBits)
RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
End Function
Private Function AddUnsigned(lX, lY)
Dim lX4
Dim lY4
Dim lX8
Dim lY8
Dim lResult
lX8 = lX And &H80000000
lY8 = lY And &H80000000
lX4 = lX And &H40000000
lY4 = lY And &H40000000
lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
If lX4 And lY4 Then
lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
ElseIf lX4 Or lY4 Then
If lResult And &H40000000 Then
lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
Else
lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
End If
Else
lResult = lResult Xor lX8 Xor lY8
End If
AddUnsigned = lResult
End Function
Private Function md5_F(x, y, z)
md5_F = (x And y) Or ((Not x) And z)
End Function
Private Function md5_G(x, y, z)
md5_G = (x And z) Or (y And (Not z))
End Function
Private Function md5_H(x, y, z)
md5_H = (x Xor y Xor z)
End Function
Private Function md5_I(x, y, z)
md5_I = (y Xor (x Or (Not z)))
End Function
Private Sub md5_FF(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_GG(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_HH(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_II(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Function ConvertToWordArray(sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = LenB(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(AscB(MidB(sMessage, lByteCount + 1, 1)), lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
ConvertToWordArray = lWordArray
End Function
Private Function WordToHex(lValue)
Dim lByte
Dim lCount
For lCount = 0 To 3
lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
Next
End Function
Public Function MD5(sMessage)
m_lOnBits(0) = CLng(1)
m_lOnBits(1) = CLng(3)
m_lOnBits(2) = CLng(7)
m_lOnBits(3) = CLng(15)
m_lOnBits(4) = CLng(31)
m_lOnBits(5) = CLng(63)
m_lOnBits(6) = CLng(127)
m_lOnBits(7) = CLng(255)
m_lOnBits(8) = CLng(511)
m_lOnBits(9) = CLng(1023)
m_lOnBits(10) = CLng(2047)
m_lOnBits(11) = CLng(4095)
m_lOnBits(12) = CLng(8191)
m_lOnBits(13) = CLng(16383)
m_lOnBits(14) = CLng(32767)
m_lOnBits(15) = CLng(65535)
m_lOnBits(16) = CLng(131071)
m_lOnBits(17) = CLng(262143)
m_lOnBits(18) = CLng(524287)
m_lOnBits(19) = CLng(1048575)
m_lOnBits(20) = CLng(2097151)
m_lOnBits(21) = CLng(4194303)
m_lOnBits(22) = CLng(8388607)
m_lOnBits(23) = CLng(16777215)
m_lOnBits(24) = CLng(33554431)
m_lOnBits(25) = CLng(67108863)
m_lOnBits(26) = CLng(134217727)
m_lOnBits(27) = CLng(268435455)
m_lOnBits(28) = CLng(536870911)
m_lOnBits(29) = CLng(1073741823)
m_lOnBits(30) = CLng(2147483647)
m_l2Power(0) = CLng(1)
m_l2Power(1) = CLng(2)
m_l2Power(2) = CLng(4)
m_l2Power(3) = CLng(8)
m_l2Power(4) = CLng(16)
m_l2Power(5) = CLng(32)
m_l2Power(6) = CLng(64)
m_l2Power(7) = CLng(128)
m_l2Power(8) = CLng(256)
m_l2Power(9) = CLng(512)
m_l2Power(10) = CLng(1024)
m_l2Power(11) = CLng(2048)
m_l2Power(12) = CLng(4096)
m_l2Power(13) = CLng(8192)
m_l2Power(14) = CLng(16384)
m_l2Power(15) = CLng(32768)
m_l2Power(16) = CLng(65536)
m_l2Power(17) = CLng(131072)
m_l2Power(18) = CLng(262144)
m_l2Power(19) = CLng(524288)
m_l2Power(20) = CLng(1048576)
m_l2Power(21) = CLng(2097152)
m_l2Power(22) = CLng(4194304)
m_l2Power(23) = CLng(8388608)
m_l2Power(24) = CLng(16777216)
m_l2Power(25) = CLng(33554432)
m_l2Power(26) = CLng(67108864)
m_l2Power(27) = CLng(134217728)
m_l2Power(28) = CLng(268435456)
m_l2Power(29) = CLng(536870912)
m_l2Power(30) = CLng(1073741824)
Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim a
Dim b
Dim c
Dim d
Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21
x = ConvertToWordArray(str2bin(sMessage))
a = &H67452301
b = &HEFCDAB89
c = &H98BADCFE
d = &H10325476
For k = 0 To UBound(x) Step 16
AA = a
BB = b
CC = c
DD = d
md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478
md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756
md5_FF c, d, a, b, x(k + 2), S13, &H242070DB
md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A
md5_FF c, d, a, b, x(k + 6), S13, &HA8304613
md5_FF b, c, d, a, x(k + 7), S14, &HFD469501
md5_FF a, b, c, d, x(k + 8), S11, &H698098D8
md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE
md5_FF a, b, c, d, x(k + 12), S11, &H6B901122
md5_FF d, a, b, c, x(k + 13), S12, &HFD987193
md5_FF c, d, a, b, x(k + 14), S13, &HA679438E
md5_FF b, c, d, a, x(k + 15), S14, &H49B40821
md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562
md5_GG d, a, b, c, x(k + 6), S22, &HC040B340
md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51
md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D
md5_GG d, a, b, c, x(k + 10), S22, &H2441453
md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681
md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6
md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87
md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED
md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905
md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9
md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942
md5_HH d, a, b, c, x(k + 8), S32, &H8771F681
md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122
md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C
md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6
md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA
md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085
md5_HH b, c, d, a, x(k + 6), S34, &H4881D05
md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039
md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665
md5_II a, b, c, d, x(k + 0), S41, &HF4292244
md5_II d, a, b, c, x(k + 7), S42, &H432AFF97
md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7
md5_II b, c, d, a, x(k + 5), S44, &HFC93A039
md5_II a, b, c, d, x(k + 12), S41, &H655B59C3
md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92
md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D
md5_II b, c, d, a, x(k + 1), S44, &H85845DD1
md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F
md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
md5_II c, d, a, b, x(k + 6), S43, &HA3014314
md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1
md5_II a, b, c, d, x(k + 4), S41, &HF7537E82
md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235
md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
md5_II b, c, d, a, x(k + 9), S44, &HEB86D391
a = AddUnsigned(a, AA)
b = AddUnsigned(b, BB)
c = AddUnsigned(c, CC)
d = AddUnsigned(d, DD)
Next
MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
End Function
End Class
后来我对比了一下Gb2312和GBK的字符表,就发现还是编码查找方式不同而引发的问题,现在正在研究C#中支持GBK编码的方法。
下面这个是VB.Net中的使用函数的实现办法
Dim md5 As New MD5CryptoServiceProvider
Dim username As Byte() = (New ASCIIEncoding).GetBytes(TextBox1.Text)
Dim mdString As String, mdString1 As String, mdString2 As String
Dim i As Integer
'转换为哈希值Byte数组
Dim mdByte As Byte() = md5.ComputeHash(username)
mdString = System.BitConverter.ToString(mdByte)
For i = 1 To 15
mdString = mdString.Substring(0, 2 * i) & mdString.Substring(3 + 2 * (i - 1), 47 - i * 3)
Next
'mdString = System.BitConverter.ToString(mdByte)
'mdString = (New ASCIIEncoding).GetString(mdByte)
TextBox2.Text = mdString
Dim username As Byte() = (New ASCIIEncoding).GetBytes(TextBox1.Text)
Dim mdString As String, mdString1 As String, mdString2 As String
Dim i As Integer
'转换为哈希值Byte数组
Dim mdByte As Byte() = md5.ComputeHash(username)
mdString = System.BitConverter.ToString(mdByte)
For i = 1 To 15
mdString = mdString.Substring(0, 2 * i) & mdString.Substring(3 + 2 * (i - 1), 47 - i * 3)
Next
'mdString = System.BitConverter.ToString(mdByte)
'mdString = (New ASCIIEncoding).GetString(mdByte)
TextBox2.Text = mdString
现在动网论坛也使用的比较多,它使用的16位的MD5加密方式,从上面的加密算法我们可以看到它是由4组8位的密码组成,而16位就是取其中的2组合并的,不同的设置就取不同位置的密码来组合,下面是在外部插入动网数据库生成随机密码的代码。
public string createpass()
{
int Ran,LengthNum=16;
string Createpass="";
for(int i=1;i<LengthNum;i++)
{
Random ram=new Random();
Ran = Convert.ToInt32(ram.NextDouble() * 2);
if(Ran== 0)
{
Ran =Convert.ToInt32(ram.NextDouble() * 25) + 97;
Createpass =Createpass+Convert.ToString(Convert.ToChar(Ran)).ToUpper();
}
else if(Ran == 1)
{
Ran = Convert.ToInt32(ram.NextDouble() * 9);
Createpass = Createpass + Ran;
}
else if(Ran == 2)
{
Ran = Convert.ToInt32(ram.NextDouble() * 25) + 97;
Createpass =Createpass+Convert.ToString(Convert.ToChar(Ran));
}
}
return Createpass;
}
{
int Ran,LengthNum=16;
string Createpass="";
for(int i=1;i<LengthNum;i++)
{
Random ram=new Random();
Ran = Convert.ToInt32(ram.NextDouble() * 2);
if(Ran== 0)
{
Ran =Convert.ToInt32(ram.NextDouble() * 25) + 97;
Createpass =Createpass+Convert.ToString(Convert.ToChar(Ran)).ToUpper();
}
else if(Ran == 1)
{
Ran = Convert.ToInt32(ram.NextDouble() * 9);
Createpass = Createpass + Ran;
}
else if(Ran == 2)
{
Ran = Convert.ToInt32(ram.NextDouble() * 25) + 97;
Createpass =Createpass+Convert.ToString(Convert.ToChar(Ran));
}
}
return Createpass;
}
这里是一个朋友的加密成字符密码的算法
加密为字符
namespace MD5
{
/// <summary>
/// 一个加密类
/// 结果是文字类
/// <br>Author: Bigeagle@163.net</br>
/// <br>Date: 2001/09/25</br>
/// <br>History: 2001/10/25 finished</br>
/// </summary>
/// <remarks>
/// 封装常用的加密算法
/// </remarks>
public class Cryptography
{
/// <summary>
/// md5加密指定字符串
/// </summary>
/// <param name="a_strValue">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptMD5String(string a_strValue)
{
#if DEBUG
Debug.Assert(a_strValue.Trim() != "" , "空字符串" , "空字符串不需要加密") ;
#endif//DEBUG
//转换成bytearray
Byte[] hba = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).
ComputeHash(StringToByteArray(a_strValue));
return ByteArrayToString(hba) ;
}
/// <summary>
/// 判断两个加密字符串是否相同
/// </summary>
/// <param name="a_str1"></param>
/// <param name="a_str2"></param>
/// <returns>如果相同返回真</returns>
public static bool IsSame(string a_str1 , string a_str2)
{
Byte[] b1 = StringToByteArray(a_str1) ;
Byte[] b2 = StringToByteArray(a_str2) ;
if(b1.Length != b2.Length)
{
return false ;
}
for(int i = 0 ; i < b1.Length ; i ++)
{
if(b1[i] != b2[i])
{
return false ;
}
}
return true ;
}
/// <summary>
/// 转换string到Byte树组
/// </summary>
/// <param name="s">要转换的字符串</param>
/// <returns>转换的Byte数组</returns>
public static Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
return ba;*/
return Encoding.UTF8.GetBytes(s) ;
}
/// <summary>
/// 转换Byte数组到字符串
/// </summary>
/// <param name="a_arrByte">Byte数组</param>
/// <returns>字符串</returns>
public static string ByteArrayToString(Byte[] a_arrByte)
{
/*
//char[] ca = new char[a_arrByte.Length] ;
for(int i = 0 ; i < a_arrByte.Length ; i ++)
{
result += (char)a_arrByte[i] ;
}*/
return Encoding.UTF8.GetString(a_arrByte) ;
}
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>加密后并经base64编码的字符串</returns>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}//end method
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>加密后并经base63编码的字符串</returns>
/// <remarks>重载,指定编码方式</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = encoding.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}
return result ;
}//end method
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,指定编码方式</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = encoding.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}
return result ;
}//end method
}
}
namespace MD5
{
/// <summary>
/// 一个加密类
/// 结果是文字类
/// <br>Author: Bigeagle@163.net</br>
/// <br>Date: 2001/09/25</br>
/// <br>History: 2001/10/25 finished</br>
/// </summary>
/// <remarks>
/// 封装常用的加密算法
/// </remarks>
public class Cryptography
{
/// <summary>
/// md5加密指定字符串
/// </summary>
/// <param name="a_strValue">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptMD5String(string a_strValue)
{
#if DEBUG
Debug.Assert(a_strValue.Trim() != "" , "空字符串" , "空字符串不需要加密") ;
#endif//DEBUG
//转换成bytearray
Byte[] hba = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).
ComputeHash(StringToByteArray(a_strValue));
return ByteArrayToString(hba) ;
}
/// <summary>
/// 判断两个加密字符串是否相同
/// </summary>
/// <param name="a_str1"></param>
/// <param name="a_str2"></param>
/// <returns>如果相同返回真</returns>
public static bool IsSame(string a_str1 , string a_str2)
{
Byte[] b1 = StringToByteArray(a_str1) ;
Byte[] b2 = StringToByteArray(a_str2) ;
if(b1.Length != b2.Length)
{
return false ;
}
for(int i = 0 ; i < b1.Length ; i ++)
{
if(b1[i] != b2[i])
{
return false ;
}
}
return true ;
}
/// <summary>
/// 转换string到Byte树组
/// </summary>
/// <param name="s">要转换的字符串</param>
/// <returns>转换的Byte数组</returns>
public static Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
return ba;*/
return Encoding.UTF8.GetBytes(s) ;
}
/// <summary>
/// 转换Byte数组到字符串
/// </summary>
/// <param name="a_arrByte">Byte数组</param>
/// <returns>字符串</returns>
public static string ByteArrayToString(Byte[] a_arrByte)
{
/*
//char[] ca = new char[a_arrByte.Length] ;
for(int i = 0 ; i < a_arrByte.Length ; i ++)
{
result += (char)a_arrByte[i] ;
}*/
return Encoding.UTF8.GetString(a_arrByte) ;
}
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>加密后并经base64编码的字符串</returns>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}//end method
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>加密后并经base63编码的字符串</returns>
/// <remarks>重载,指定编码方式</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = encoding.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}
return result ;
}//end method
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,指定编码方式</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = encoding.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}
return result ;
}//end method
}
}
其实密码就是几种情况,位移,叠加,无非就是多几次,以及加减乘除的内容。最近在弄md5密码数据库,程序弄好勒,结果一运行机器死了,内存暴涨,最后还是算勒,呵呵。