标签:
寻找md5值是它自己的数(下称Z数)
Z数一个128位长度的数,找到它如同大海捞针
下面代码的核心是 md5_is_self 函数,当传入的是Z数时,函数返回真
main函数里面的实现只是一个使用 md5_is_self() 例子,main函数里面的代码并不能找到Z数
#include <stdio.h> int print_md5(const unsigned int in[4]) { int j; union { unsigned int i; unsigned char c[4]; } u; u.i = 1; if(u.c[0] == 1) { // little endian for(j = 0; j < 4; j++) { u.i = in[j]; printf("%02x%02x%02x%02x", u.c[0], u.c[1], u.c[2], u.c[3]); } } else { // big endian for(j = 0; j < 4; j++) { u.i = in[j]; printf("%02x%02x%02x%02x", u.c[3], u.c[2], u.c[1], u.c[0]); } } printf("\n"); return 0; } #define FF(b,c,d) ( d ^ (b & (c ^ d)) ) // ( (b & c) | (~b & d) ) #define FG(b,c,d) FF(d, b, c) // ( (b & d) | (c & ~d) ) #define FH(b,c,d) ( b ^ c ^ d ) #define FI(b,c,d) ( c ^ (b | ~d) ) #define ROLL(a,s) ( (a<<s) | (a>>(32-s)) ) #define OP(func, a,b,c,d, k,s,t) a+=func(b,c,d)+k+t; a=b+ROLL(a,s) int md5_is_self(const unsigned int in[4]) { unsigned int A = 0x67452301; unsigned int B = 0xefcdab89; unsigned int C = 0x98badcfe; unsigned int D = 0x10325476; OP(FF, A, B, C, D, in[0], 7, 0xd76aa478); OP(FF, D, A, B, C, in[1], 12, 0xe8c7b756); OP(FF, C, D, A, B, in[2], 17, 0x242070db); OP(FF, B, C, D, A, in[3], 22, 0xc1bdceee); OP(FF, A, B, C, D, 0x80, 7, 0xf57c0faf); OP(FF, D, A, B, C, 0, 12, 0x4787c62a); OP(FF, C, D, A, B, 0, 17, 0xa8304613); OP(FF, B, C, D, A, 0, 22, 0xfd469501); OP(FF, A, B, C, D, 0, 7, 0x698098d8); OP(FF, D, A, B, C, 0, 12, 0x8b44f7af); OP(FF, C, D, A, B, 0, 17, 0xffff5bb1); OP(FF, B, C, D, A, 0, 22, 0x895cd7be); OP(FF, A, B, C, D, 0, 7, 0x6b901122); OP(FF, D, A, B, C, 0, 12, 0xfd987193); OP(FF, C, D, A, B, 0x80, 17, 0xa679438e); OP(FF, B, C, D, A, 0, 22, 0x49b40821); OP(FG, A, B, C, D, in[1], 5, 0xf61e2562); OP(FG, D, A, B, C, 0, 9, 0xc040b340); OP(FG, C, D, A, B, 0, 14, 0x265e5a51); OP(FG, B, C, D, A, in[0], 20, 0xe9b6c7aa); OP(FG, A, B, C, D, 0, 5, 0xd62f105d); OP(FG, D, A, B, C, 0, 9, 0x02441453); OP(FG, C, D, A, B, 0, 14, 0xd8a1e681); OP(FG, B, C, D, A, 0x80, 20, 0xe7d3fbc8); OP(FG, A, B, C, D, 0, 5, 0x21e1cde6); OP(FG, D, A, B, C, 0x80, 9, 0xc33707d6); OP(FG, C, D, A, B, in[3], 14, 0xf4d50d87); OP(FG, B, C, D, A, 0, 20, 0x455a14ed); OP(FG, A, B, C, D, 0, 5, 0xa9e3e905); OP(FG, D, A, B, C, in[2], 9, 0xfcefa3f8); OP(FG, C, D, A, B, 0, 14, 0x676f02d9); OP(FG, B, C, D, A, 0, 20, 0x8d2a4c8a); OP(FH, A, B, C, D, 0, 4, 0xfffa3942); OP(FH, D, A, B, C, 0, 11, 0x8771f681); OP(FH, C, D, A, B, 0, 16, 0x6d9d6122); OP(FH, B, C, D, A, 0x80, 23, 0xfde5380c); OP(FH, A, B, C, D, in[1], 4, 0xa4beea44); OP(FH, D, A, B, C, 0x80, 11, 0x4bdecfa9); OP(FH, C, D, A, B, 0, 16, 0xf6bb4b60); OP(FH, B, C, D, A, 0, 23, 0xbebfbc70); OP(FH, A, B, C, D, 0, 4, 0x289b7ec6); OP(FH, D, A, B, C, in[0], 11, 0xeaa127fa); OP(FH, C, D, A, B, in[3], 16, 0xd4ef3085); OP(FH, B, C, D, A, 0, 23, 0x04881d05); OP(FH, A, B, C, D, 0, 4, 0xd9d4d039); OP(FH, D, A, B, C, 0, 11, 0xe6db99e5); OP(FH, C, D, A, B, 0, 16, 0x1fa27cf8); OP(FH, B, C, D, A, in[2], 23, 0xc4ac5665); OP(FI, A, B, C, D, in[0], 6, 0xf4292244); OP(FI, D, A, B, C, 0, 10, 0x432aff97); OP(FI, C, D, A, B, 0x80, 15, 0xab9423a7); OP(FI, B, C, D, A, 0, 21, 0xfc93a039); OP(FI, A, B, C, D, 0, 6, 0x655b59c3); OP(FI, D, A, B, C, in[3], 10, 0x8f0ccc92); OP(FI, C, D, A, B, 0, 15, 0xffeff47d); OP(FI, B, C, D, A, in[1], 21, 0x85845dd1); OP(FI, A, B, C, D, 0, 6, 0x6fa87e4f); OP(FI, D, A, B, C, 0, 10, 0xfe2ce6e0); OP(FI, C, D, A, B, 0, 15, 0xa3014314); OP(FI, B, C, D, A, 0, 21, 0x4e0811a1); OP(FI, A, B, C, D, 0x80, 6, 0xf7537e82); OP(FI, D, A, B, C, 0, 10, 0xbd3af235); OP(FI, C, D, A, B, in[2], 15, 0x2ad7d2bb); OP(FI, B, C, D, A, 0, 21, 0xeb86d391); return A+0x67452301==in[0] && B+0xefcdab89==in[1] && C+0x98badcfe==in[2] && D+0x10325476==in[3]; } int main(int argc, char * argv[]) { // example: unsigned int in[4] = { 0x5643237f, 0x1245e6a0, 0xd3e6aa01, 0x7f431234 }; while(in[3]++) { if(md5_is_self(in)) { print_md5(in); return 0; } } return 1; }
标签:
原文地址:http://my.oschina.net/2bit/blog/512670