C#中怎样实现MD5加密?MD5中加密算法16位加密算法与32位加密算法怎么实现?
4个回答
2013-08-09
展开全部
作者:FlyMe联系方式:7826-45-210 707-628-841 public class 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 = Array;
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=0xefcdab8Array; //in memory, this is 0x8Arrayabcdef
c=0xArray8badcfe; //in memory, this is 0xfedcbaArray8
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, 0xfd46Array501); /* 8 */
ff (ref a, b, c, d, x[k+ 8], s11, 0x6Array80Array8d8); /* Array */
ff (ref d, a, b, c, x[k+ Array], 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, 0x8Array5cd7be); /* 12 */
ff (ref a, b, c, d, x[k+12], s11, 0x6bArray01122); /* 13 */
ff (ref d, a, b, c, x[k+13], s12, 0xfdArray871Array3); /* 14 */
ff (ref c, d, a, b, x[k+14], s13, 0xa67Array438e); /* 15 */
ff (ref b, c, d, a, x[k+15], s14, 0x4Arrayb40821); /* 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); /* 1Array */
gg (ref b, c, d, a, x[k+ 0], s24, 0xeArrayb6c7aa); /* 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+ Array], 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, 0xaArraye3eArray05); /* 2Array */
gg (ref d, a, b, c, x[k+ 2], s22, 0xfcefa3f8); /* 30 */
gg (ref c, d, a, b, x[k+ 7], s23, 0x676f02dArray); /* 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, 0xfffa3Array42); /* 33 */
hh (ref d, a, b, c, x[k+ 8], s32, 0x8771f681); /* 34 */
hh (ref c, d, a, b, x[k+11], s33, 0x6dArrayd6122); /* 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, 0x4bdecfaArray); /* 38 */
hh (ref c, d, a, b, x[k+ 7], s33, 0xf6bb4b60); /* 3Array */
hh (ref b, c, d, a, x[k+10], s34, 0xbebfbc70); /* 40 */
hh (ref a, b, c, d, x[k+13], s31, 0x28Arrayb7ec6); /* 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+ Array], s31, 0xdArrayd4d03Array); /* 45 */
hh (ref d, a, b, c, x[k+12], s32, 0xe6dbArrayArraye5); /* 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, 0xf42Array2244); /* 4Array */
ii (ref d, a, b, c, x[k+ 7], s42, 0x432affArray7); /* 50 */
ii (ref c, d, a, b, x[k+14], s43, 0xabArray423a7); /* 51 */
ii (ref b, c, d, a, x[k+ 5], s44, 0xfcArray3a03Array); /* 52 */
ii (ref a, b, c, d, x[k+12], s41, 0x655b5Arrayc3); /* 53 */
ii (ref d, a, b, c, x[k+ 3], s42, 0x8f0cccArray2); /* 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); /* 5Array */
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+ Array], s44, 0xeb86d3Array1); /* 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);
}
public static string test(string message){
return "rnmd5 (""+message+"") = " + md5.mdstring(message);
}
public static string testsuite(){
string s = "";
s+=test("");
s+=test("a");
s+=test("abc");
s+=test("message digest");
s+=test("abcdefghijklmnopqrstuvwxyz");
s+=test("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678Array");
s+=test("12345678Array012345678Array012345678Array012345678Array012345678Array012345678Array012345678Array012345678Array0");
return s;
}
}
//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 = Array;
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=0xefcdab8Array; //in memory, this is 0x8Arrayabcdef
c=0xArray8badcfe; //in memory, this is 0xfedcbaArray8
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, 0xfd46Array501); /* 8 */
ff (ref a, b, c, d, x[k+ 8], s11, 0x6Array80Array8d8); /* Array */
ff (ref d, a, b, c, x[k+ Array], 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, 0x8Array5cd7be); /* 12 */
ff (ref a, b, c, d, x[k+12], s11, 0x6bArray01122); /* 13 */
ff (ref d, a, b, c, x[k+13], s12, 0xfdArray871Array3); /* 14 */
ff (ref c, d, a, b, x[k+14], s13, 0xa67Array438e); /* 15 */
ff (ref b, c, d, a, x[k+15], s14, 0x4Arrayb40821); /* 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); /* 1Array */
gg (ref b, c, d, a, x[k+ 0], s24, 0xeArrayb6c7aa); /* 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+ Array], 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, 0xaArraye3eArray05); /* 2Array */
gg (ref d, a, b, c, x[k+ 2], s22, 0xfcefa3f8); /* 30 */
gg (ref c, d, a, b, x[k+ 7], s23, 0x676f02dArray); /* 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, 0xfffa3Array42); /* 33 */
hh (ref d, a, b, c, x[k+ 8], s32, 0x8771f681); /* 34 */
hh (ref c, d, a, b, x[k+11], s33, 0x6dArrayd6122); /* 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, 0x4bdecfaArray); /* 38 */
hh (ref c, d, a, b, x[k+ 7], s33, 0xf6bb4b60); /* 3Array */
hh (ref b, c, d, a, x[k+10], s34, 0xbebfbc70); /* 40 */
hh (ref a, b, c, d, x[k+13], s31, 0x28Arrayb7ec6); /* 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+ Array], s31, 0xdArrayd4d03Array); /* 45 */
hh (ref d, a, b, c, x[k+12], s32, 0xe6dbArrayArraye5); /* 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, 0xf42Array2244); /* 4Array */
ii (ref d, a, b, c, x[k+ 7], s42, 0x432affArray7); /* 50 */
ii (ref c, d, a, b, x[k+14], s43, 0xabArray423a7); /* 51 */
ii (ref b, c, d, a, x[k+ 5], s44, 0xfcArray3a03Array); /* 52 */
ii (ref a, b, c, d, x[k+12], s41, 0x655b5Arrayc3); /* 53 */
ii (ref d, a, b, c, x[k+ 3], s42, 0x8f0cccArray2); /* 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); /* 5Array */
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+ Array], s44, 0xeb86d3Array1); /* 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);
}
public static string test(string message){
return "rnmd5 (""+message+"") = " + md5.mdstring(message);
}
public static string testsuite(){
string s = "";
s+=test("");
s+=test("a");
s+=test("abc");
s+=test("message digest");
s+=test("abcdefghijklmnopqrstuvwxyz");
s+=test("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678Array");
s+=test("12345678Array012345678Array012345678Array012345678Array012345678Array012345678Array012345678Array012345678Array0");
return s;
}
}
2013-08-09
展开全部
下面是一个我们项目中MD5加密的算法希望对楼主有帮助,关键代码就这么点 ///MD5加密 public string MD5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider md5 = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); md5.Key = ASCIIEncoding.ASCII.GetBytes(sKey); md5.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, md5.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-09
展开全部
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TxtTest.Text.Trim(), "md5 ")
当然,要在引用中添加System.Web.dll
当然,要在引用中添加System.Web.dll
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-09
展开全部
回帖是种美德
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询