1.MD5是什么?
一种单向的加密方法,也就是说只有从明文到密文这一步,没有密文到明文这一步。
2.为什么用MD5呢?
作为识别ID,我们实际上并不需要知道它具体是多少,只需要保证两点,唯一的明文对着唯一密文,有高度的安全性,就行了。
3.MD5算法基本过程?
任意长度字符串输入->转化成byte数组->求余补长->分组->每组都做四轮数据变换->固定128位信息
4.第一步
将字符串转化为byte数组:
byte[] inputBytes = Encoding.Default.GetBytes (_input);利用C#的Encoding里的方法即可。
5.第二步
求余补长:
1.求余,求余的目的是为了把每64个byte分成一组,也就是64*8=512个bit,512又将依据每32个的合成16个int,作为四轮数据变换的输入端。
2.当然,由于输入的字符串是任意长的,显然不能保证整除64之类的,所以我们必须对剩下的byte做补长,把他补成64位,补长规则是这样的:对于余数不足56位的,首先将他补至56位,补足规则是:填充1个1,N个0;对于56位以上的,补充规则都是一样的,总是将最后8位替换成byte数组的长度信息。
int byteLen = inputBytes.Length; int []groups=null; int groupCount=0; groupCount=byteLen/64; int rest=byteLen%64; byte [] tempBytes=new byte[64]; if(rest<=56) { for(int i=0;i<rest;i++) tempBytes[i]=inputBytes[byteLen-rest+i]; if(rest<56) { tempBytes[rest]=(byte)(1<<7); for(int i=1;i<56-rest;i++) tempBytes[rest+i]=0; } long len=(long)(byteLen<<3); for(int i=0;i<8;i++) { tempBytes[56+i]=(byte)(len&0xFFL); len=len>>8; } groups=divGroup(tempBytes,0); trans(groups); } else { for(int i=0;i<rest;i++) tempBytes[i]=inputBytes[byteLen-rest+i]; long len=(long)(byteLen<<3); for(int i=0;i<8;i++) { tempBytes[56+i]=(byte)(len&0xFFL); len=len>>8; } groups=divGroup(tempBytes,0); trans (groups); }
代码中的divgroup,trans,就是后面的分组,变换操作,我们稍后再说。
当然,对于byte数组的前N组64位信息组,也都要执行这个操作:
for(int step=0;step<groupCount;step++) { groups=divGroup(inputBytes,step*64); trans(groups); }6.第三步
分组:对于64个byte重分成16个int:
private static int[] divGroup(byte[] inputBytes,int index) { int [] temp=new int[16]; for(int i=0;i<16;i++) { temp[i]=b2iu(inputBytes[4*i+index])| (b2iu(inputBytes[4*i+1+index]))<<8| (b2iu(inputBytes[4*i+2+index]))<<16| (b2iu(inputBytes[4*i+3+index]))<<24; } return temp; } public static int b2iu(byte b) { return b < 0 ? b & 0x7F + 128 : b; }其实就是按着顺序,每4个作为int即可。
7.第四步:
最为复杂的一步:对输入的16个int做各种变换,并把效应叠加到最后的结果上:
首先有4种基础操作F、G、H、I:
private static long F(long x, long y, long z) { return (x & y) | ((~x) & z); } private static long G(long x, long y, long z) { return (x & z) | (y & (~z)); } private static long H(long x, long y, long z) { return x ^ y ^ z; } private static long I(long x, long y, long z) { return y ^ (x | (~z)); }接着有在这基础上的4种复杂一点的运算FF、GG、HH、II:
rivate static long FF(long a, long b, long c, long d, int x, long s, long ac) { a += (F(b, c, d)&0xFFFFFFFFL) + x + ac; a = ((a&0xFFFFFFFFL)<< (int)s) | ((int)(a&0xFFFFFFFFL) >> (int)(32 - s)); a += b; return (a&0xFFFFFFFFL); } private static long GG(long a, long b, long c, long d, int x, long s, long ac) { a += (G(b, c, d)&0xFFFFFFFFL) + x + ac; a = ((a&0xFFFFFFFFL) <<(int) s) | ((int)(a&0xFFFFFFFFL) >> (int)(32 - s)); a += b; return (a&0xFFFFFFFFL); } private static long HH(long a, long b, long c, long d, int x, long s, long ac) { a += (H(b, c, d)&0xFFFFFFFFL) + x + ac; a = ((a&0xFFFFFFFFL) << (int)s) | ((int)(a&0xFFFFFFFFL) >> (int)(32 - s)); a += b; return (a&0xFFFFFFFFL); } private static long II(long a, long b, long c, long d, int x, long s, long ac) { a += (I(b, c, d)&0xFFFFFFFFL) + x + ac; a = ((a&0xFFFFFFFFL) <<(int) s) | ((int)(a&0xFFFFFFFFL) >> (int)(32 - s)); a += b; return (a&0xFFFFFFFFL); }然而我们要对着16个int做4组16轮的变换,每一组中的一轮,对应着16个int的一个数,4组分别用FF、GG、HH、II操作的一种。
先看看操作中用到的数据:
private static long A=0x67452301L; private static long B=0xefcdab89L; private static long C=0x98badcfeL; private static long D=0x10325476L; static int S11 = 7; static int S12 = 12; static int S13 = 17; static int S14 = 22; static int S21 = 5; static int S22 = 9; static int S23 = 14; static int S24 = 20; static int S31 = 4; static int S32 = 11; static int S33 = 16; static int S34 = 23; static int S41 = 6; static int S42 = 10; static int S43 = 15; static int S44 = 21; private static long [] result={A,B,C,D};
trans函数最终形态:
private static void trans(int[] groups) { long a = result[0], b = result[1], c = result[2], d = result[3]; a = FF(a, b, c, d, groups[0], S11, 0xd76aa478L); /* 1 */ d = FF(d, a, b, c, groups[1], S12, 0xe8c7b756L); /* 2 */ c = FF(c, d, a, b, groups[2], S13, 0x242070dbL); /* 3 */ b = FF(b, c, d, a, groups[3], S14, 0xc1bdceeeL); /* 4 */ a = FF(a, b, c, d, groups[4], S11, 0xf57c0fafL); /* 5 */ d = FF(d, a, b, c, groups[5], S12, 0x4787c62aL); /* 6 */ c = FF(c, d, a, b, groups[6], S13, 0xa8304613L); /* 7 */ b = FF(b, c, d, a, groups[7], S14, 0xfd469501L); /* 8 */ a = FF(a, b, c, d, groups[8], S11, 0x698098d8L); /* 9 */ d = FF(d, a, b, c, groups[9], S12, 0x8b44f7afL); /* 10 */ c = FF(c, d, a, b, groups[10], S13, 0xffff5bb1L); /* 11 */ b = FF(b, c, d, a, groups[11], S14, 0x895cd7beL); /* 12 */ a = FF(a, b, c, d, groups[12], S11, 0x6b901122L); /* 13 */ d = FF(d, a, b, c, groups[13], S12, 0xfd987193L); /* 14 */ c = FF(c, d, a, b, groups[14], S13, 0xa679438eL); /* 15 */ b = FF(b, c, d, a, groups[15], S14, 0x49b40821L); /* 16 */ a = GG(a, b, c, d, groups[1], S21, 0xf61e2562L); /* 17 */ d = GG(d, a, b, c, groups[6], S22, 0xc040b340L); /* 18 */ c = GG(c, d, a, b, groups[11], S23, 0x265e5a51L); /* 19 */ b = GG(b, c, d, a, groups[0], S24, 0xe9b6c7aaL); /* 20 */ a = GG(a, b, c, d, groups[5], S21, 0xd62f105dL); /* 21 */ d = GG(d, a, b, c, groups[10], S22, 0x2441453L); /* 22 */ c = GG(c, d, a, b, groups[15], S23, 0xd8a1e681L); /* 23 */ b = GG(b, c, d, a, groups[4], S24, 0xe7d3fbc8L); /* 24 */ a = GG(a, b, c, d, groups[9], S21, 0x21e1cde6L); /* 25 */ d = GG(d, a, b, c, groups[14], S22, 0xc33707d6L); /* 26 */ c = GG(c, d, a, b, groups[3], S23, 0xf4d50d87L); /* 27 */ b = GG(b, c, d, a, groups[8], S24, 0x455a14edL); /* 28 */ a = GG(a, b, c, d, groups[13], S21, 0xa9e3e905L); /* 29 */ d = GG(d, a, b, c, groups[2], S22, 0xfcefa3f8L); /* 30 */ c = GG(c, d, a, b, groups[7], S23, 0x676f02d9L); /* 31 */ b = GG(b, c, d, a, groups[12], S24, 0x8d2a4c8aL); /* 32 */ a = HH(a, b, c, d, groups[5], S31, 0xfffa3942L); /* 33 */ d = HH(d, a, b, c, groups[8], S32, 0x8771f681L); /* 34 */ c = HH(c, d, a, b, groups[11], S33, 0x6d9d6122L); /* 35 */ b = HH(b, c, d, a, groups[14], S34, 0xfde5380cL); /* 36 */ a = HH(a, b, c, d, groups[1], S31, 0xa4beea44L); /* 37 */ d = HH(d, a, b, c, groups[4], S32, 0x4bdecfa9L); /* 38 */ c = HH(c, d, a, b, groups[7], S33, 0xf6bb4b60L); /* 39 */ b = HH(b, c, d, a, groups[10], S34, 0xbebfbc70L); /* 40 */ a = HH(a, b, c, d, groups[13], S31, 0x289b7ec6L); /* 41 */ d = HH(d, a, b, c, groups[0], S32, 0xeaa127faL); /* 42 */ c = HH(c, d, a, b, groups[3], S33, 0xd4ef3085L); /* 43 */ b = HH(b, c, d, a, groups[6], S34, 0x4881d05L); /* 44 */ a = HH(a, b, c, d, groups[9], S31, 0xd9d4d039L); /* 45 */ d = HH(d, a, b, c, groups[12], S32, 0xe6db99e5L); /* 46 */ c = HH(c, d, a, b, groups[15], S33, 0x1fa27cf8L); /* 47 */ b = HH(b, c, d, a, groups[2], S34, 0xc4ac5665L); /* 48 */ a = II(a, b, c, d, groups[0], S41, 0xf4292244L); /* 49 */ d = II(d, a, b, c, groups[7], S42, 0x432aff97L); /* 50 */ c = II(c, d, a, b, groups[14], S43, 0xab9423a7L); /* 51 */ b = II(b, c, d, a, groups[5], S44, 0xfc93a039L); /* 52 */ a = II(a, b, c, d, groups[12], S41, 0x655b59c3L); /* 53 */ d = II(d, a, b, c, groups[3], S42, 0x8f0ccc92L); /* 54 */ c = II(c, d, a, b, groups[10], S43, 0xffeff47dL); /* 55 */ b = II(b, c, d, a, groups[1], S44, 0x85845dd1L); /* 56 */ a = II(a, b, c, d, groups[8], S41, 0x6fa87e4fL); /* 57 */ d = II(d, a, b, c, groups[15], S42, 0xfe2ce6e0L); /* 58 */ c = II(c, d, a, b, groups[6], S43, 0xa3014314L); /* 59 */ b = II(b, c, d, a, groups[13], S44, 0x4e0811a1L); /* 60 */ a = II(a, b, c, d, groups[4], S41, 0xf7537e82L); /* 61 */ d = II(d, a, b, c, groups[11], S42, 0xbd3af235L); /* 62 */ c = II(c, d, a, b, groups[2], S43, 0x2ad7d2bbL); /* 63 */ b = II(b, c, d, a, groups[9], S44, 0xeb86d391L); /* 64 */ result[0] += a; result[1] += b; result[2] += c; result[3] += d; result[0]=result[0]&0xFFFFFFFFL; result[1]=result[1]&0xFFFFFFFFL; result[2]=result[2]&0xFFFFFFFFL; result[3]=result[3]&0xFFFFFFFFL; }
8.最后一步
将result中的64X4按16进制输出即可:
static string[] hexs={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; string resStr=""; long temp=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { temp=result[i]&0x0FL; string a=hexs[(int)(temp)]; result[i]=result[i]>>4; temp=result[i]&0x0FL; resStr+=hexs[(int)(temp)]+a; result[i]=result[i]>>4; } }