fork download
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <memory.h>
  6.  
  7. #include <math.h>
  8.  
  9. struct node
  10. {
  11. int flag;
  12. struct node* alphabet[26];
  13. };
  14. struct node* root;
  15. struct node* current;//記錄現在node產生的位置
  16. struct node* previous;//記錄新產生node的前一個node之位置
  17.  
  18. int search_word(char*);
  19.  
  20.  
  21. typedef unsigned char BYTE;
  22.  
  23. typedef unsigned int UINT;
  24.  
  25. typedef UINT MD5_SUB_ARRAY[16];
  26.  
  27. typedef UINT MD5_TRANSORM_FUNC(UINT,UINT,UINT);
  28.  
  29. typedef struct
  30.  
  31. {
  32.  
  33. UINT a;
  34.  
  35. UINT b;
  36.  
  37. UINT c;
  38.  
  39. UINT d;
  40.  
  41. MD5_SUB_ARRAY sub_array;
  42.  
  43. }MD5_TRANSFORM_PARAM;
  44.  
  45.  
  46. const double MAX_INT = (double)0xFFFFFFFF + 1.0;
  47.  
  48.  
  49. const UINT MD5_TRANSFORM_MATRIX[4][16][3] =
  50.  
  51. {
  52.  
  53.  
  54.  
  55. {
  56.  
  57. { 0, 7, 1}, { 1,12, 2}, { 2,17, 3}, { 3,22, 4},
  58.  
  59. { 4, 7, 5}, { 5,12, 6}, { 6,17, 7}, { 7,22, 8},
  60.  
  61. { 8, 7, 9}, { 9,12,10}, {10,17,11}, {11,22,12},
  62.  
  63. {12, 7,13}, {13,12,14}, {14,17,15}, {15,22,16},
  64.  
  65. },
  66.  
  67.  
  68.  
  69. {
  70.  
  71. { 1, 5,17}, { 6, 9,18}, {11,14,19}, { 0,20,20},
  72.  
  73. { 5, 5,21}, {10, 9,22}, {15,14,23}, { 4,20,24},
  74.  
  75. { 9, 5,25}, {14, 9,26}, { 3,14,27}, { 8,20,28},
  76.  
  77. {13, 5,29}, { 2, 9,30}, { 7,14,31}, {12,20,32},
  78.  
  79. },
  80.  
  81.  
  82.  
  83. {
  84.  
  85. {5, 4, 33}, { 8,11,34}, {11,16,35},{14, 23,36},
  86.  
  87. {1, 4, 37}, { 4,11,38}, { 7,16,39},{10, 23,40},
  88.  
  89. {13,4, 41}, { 0,11,42}, { 3,16,43},{ 6, 23,44},
  90.  
  91. {9, 4, 45}, {12,11,46}, {15,16,47},{ 2, 23,48},
  92.  
  93. },
  94.  
  95.  
  96.  
  97. {
  98.  
  99. { 0,6,49}, { 7,10,50}, {14,15,51},{ 5, 21,52},
  100.  
  101. {12,6,53}, { 3,10,54}, {10,15,55},{ 1, 21,56},
  102.  
  103. { 8,6,57}, {15,10,58}, { 6,15,59},{13, 21,60},
  104.  
  105. { 4,6,61}, {11,10,62}, { 2,15,63},{ 9, 21,64},
  106.  
  107. },
  108.  
  109. };
  110.  
  111.  
  112. static UINT MD5_TRANSFORM_ARRAY[65];
  113.  
  114.  
  115. void MD5_Init()
  116.  
  117. {
  118.  
  119. int x;
  120.  
  121. for(x = 1; x <= 64; x++)
  122.  
  123. {
  124.  
  125. MD5_TRANSFORM_ARRAY[x] = (UINT)(MAX_INT * fabs(sin(x)));
  126.  
  127. }
  128.  
  129. }
  130.  
  131.  
  132. UINT F(UINT x,UINT y,UINT z)
  133.  
  134. {
  135.  
  136. return ((x & y) | ((~x) & z));
  137.  
  138. }
  139.  
  140.  
  141. UINT G(UINT x,UINT y,UINT z)
  142.  
  143. {
  144.  
  145. return ((x & z) | (y & (~z)));
  146.  
  147. }
  148.  
  149.  
  150. UINT H(UINT x,UINT y,UINT z)
  151.  
  152. {
  153.  
  154. return (x ^ y ^ z);
  155.  
  156. }
  157.  
  158.  
  159. UINT I(UINT x,UINT y,UINT z)
  160.  
  161. {
  162.  
  163. return (y ^ (x | (~z)));
  164.  
  165. }
  166.  
  167.  
  168.  
  169.  
  170. BYTE* MD5_prepare_data(const BYTE* data,int len,int* new_len)
  171.  
  172. {
  173.  
  174. int rest,fill,size;
  175.  
  176. BYTE* new_data;
  177.  
  178. UINT bit_len;
  179.  
  180.  
  181. // (1) 字節補齊
  182.  
  183. rest = len % 56;
  184.  
  185. if (rest <= 56) fill = 56 - rest;
  186.  
  187. else fill = (64 - rest) + 56;
  188.  
  189.  
  190. new_data = (BYTE*)malloc(len + fill + 8);
  191.  
  192. if (NULL == new_data) return NULL;
  193.  
  194.  
  195. if (len > 0) memcpy(new_data,data,len);
  196.  
  197. if (fill > 0) memset(new_data + len,0x80,1);
  198.  
  199. if (fill > 1) memset(new_data + len + 1,0,fill - 1);
  200.  
  201.  
  202. size = fill + len;
  203.  
  204.  
  205. // (2) 附加數據的比特長度
  206.  
  207. bit_len = len * 8;
  208.  
  209. // (64位二進制數表示的)比特長度的低32位
  210.  
  211. memset(new_data + size + 0,(bit_len & 0x000000FF), 1);
  212.  
  213. memset(new_data + size + 1,(bit_len & 0x0000FF00) >> 8, 1);
  214.  
  215. memset(new_data + size + 2,(bit_len & 0x00FF0000) >> 16,1);
  216.  
  217. memset(new_data + size + 3,(bit_len & 0xFF000000) >> 24,1);
  218.  
  219. // 不考慮比特長度超出32位無符號數表示範圍,所以高32位總是0
  220.  
  221. memset(new_data + size + 4,0,4);
  222.  
  223.  
  224. *new_len = size + 8;
  225.  
  226.  
  227. return new_data;
  228.  
  229. }
  230.  
  231.  
  232. void MD5_transform(MD5_TRANSFORM_PARAM* param,int ring,MD5_TRANSORM_FUNC func)
  233.  
  234. {
  235.  
  236. UINT a,b,c,d,s,k,i;
  237.  
  238. UINT abcd[4];
  239.  
  240. UINT *X,*T;
  241.  
  242. int index;
  243.  
  244.  
  245. abcd[0] = param->a;
  246.  
  247. abcd[1] = param->b;
  248.  
  249. abcd[2] = param->c;
  250.  
  251. abcd[3] = param->d;
  252.  
  253. X = param->sub_array;
  254.  
  255. T = MD5_TRANSFORM_ARRAY;
  256.  
  257.  
  258. for(index = 0; index < 16; index++)
  259.  
  260. {
  261.  
  262. a = abcd[(3 * index + 0) % 4];
  263.  
  264. b = abcd[(3 * index + 1) % 4];
  265.  
  266. c = abcd[(3 * index + 2) % 4];
  267.  
  268. d = abcd[(3 * index + 3) % 4];
  269.  
  270.  
  271. k = MD5_TRANSFORM_MATRIX[ring][index][0];
  272.  
  273. s = MD5_TRANSFORM_MATRIX[ring][index][1];
  274.  
  275. i = MD5_TRANSFORM_MATRIX[ring][index][2];
  276.  
  277.  
  278. a = a + func(b,c,d) + X[k] + T[i];
  279.  
  280. a = ( a << s) | ( a >> (32 - s)); // 循環左移
  281.  
  282. a = a + b;
  283.  
  284.  
  285. abcd[(3 * index + 0) % 4] = a;
  286.  
  287. }
  288.  
  289.  
  290. param->a = abcd[0];
  291.  
  292. param->b = abcd[1];
  293.  
  294. param->c = abcd[2];
  295.  
  296. param->d = abcd[3];
  297.  
  298. }
  299.  
  300.  
  301. char* MD5(const BYTE* data,int len)
  302.  
  303. {
  304.  
  305. int x,y,new_len;
  306.  
  307. MD5_TRANSFORM_PARAM param;
  308.  
  309. UINT AA,BB,CC,DD;
  310.  
  311. BYTE* buf;
  312.  
  313.  
  314. MD5_Init();
  315.  
  316.  
  317. buf = MD5_prepare_data(data,len,&new_len);
  318.  
  319. if (buf == NULL) return NULL;
  320.  
  321.  
  322. AA = 0x67452301;
  323.  
  324. BB = 0xefcdab89;
  325.  
  326. CC = 0x98badcfe;
  327.  
  328. DD = 0x10325476;
  329.  
  330.  
  331. for(x = 0; x < new_len / 64; x++)
  332.  
  333. {
  334.  
  335. param.a = AA;
  336.  
  337. param.b = BB;
  338.  
  339. param.c = CC;
  340.  
  341. param.d = DD;
  342.  
  343.  
  344. for(y = 0; y < 16; y++)
  345.  
  346. {
  347.  
  348. param.sub_array[y] = buf[64 * x + 4 * y + 0];
  349.  
  350. param.sub_array[y] += buf[64 * x + 4 * y + 1] << 8;
  351.  
  352. param.sub_array[y] += buf[64 * x + 4 * y + 2] << 16;
  353.  
  354. param.sub_array[y] += buf[64 * x + 4 * y + 3] << 24;
  355.  
  356. }
  357.  
  358. MD5_transform(&param,0,F);
  359.  
  360. MD5_transform(&param,1,G);
  361.  
  362. MD5_transform(&param,2,H);
  363.  
  364. MD5_transform(&param,3,I);
  365.  
  366.  
  367. AA += param.a;
  368.  
  369. BB += param.b;
  370.  
  371. CC += param.c;
  372.  
  373. DD += param.d;
  374.  
  375. }
  376.  
  377.  
  378. //printf("MD5(\"%s\")=",data);
  379.  
  380. char* str=malloc(33*sizeof(char));
  381. memset(str,'\0',33);//對malloc出來的空間作初使化
  382.  
  383. int temp_num_1;
  384. int temp_num_2;
  385. int temp_num_3;
  386. int temp_num_4;
  387. char temp_str_1[4];
  388. char temp_str_2[4];
  389. char temp_str_3[4];
  390. char temp_str_4[4];
  391.  
  392. /*printf("%02X%02X%02X%02X",
  393.  
  394.   (AA & 0x000000FF),
  395.  
  396.   (AA & 0x0000FF00) >> 8,
  397.  
  398.   (AA & 0x00FF0000) >> 16,
  399.  
  400.   (AA & 0xFF000000) >> 24);*/
  401.  
  402. /*temp_num_1=(AA & 0x000000FF);
  403.   temp_num_2=(AA & 0x000000FF)>>8;
  404.   temp_num_3=(AA & 0x000000FF)>>16;
  405.   temp_num_4=(AA & 0x000000FF)>>24;*/
  406. sprintf(temp_str_1,"%02X",(AA & 0x000000FF));
  407. sprintf(temp_str_2,"%02X",(AA & 0x0000FF00) >> 8);
  408. sprintf(temp_str_3,"%02X",(AA & 0x00FF0000) >> 16);
  409. sprintf(temp_str_4,"%02X",(AA & 0xFF000000) >> 24);
  410.  
  411. strcat(str,temp_str_1);
  412. strcat(str,temp_str_2);
  413. strcat(str,temp_str_3);
  414. strcat(str,temp_str_4);
  415.  
  416. /*printf("%02X%02X%02X%02X",
  417.  
  418.   (BB & 0x000000FF),
  419.  
  420.   (BB & 0x0000FF00) >> 8,
  421.  
  422.   (BB & 0x00FF0000) >> 16,
  423.  
  424.   (BB & 0xFF000000) >> 24);*/
  425.  
  426. sprintf(temp_str_1,"%02X",(BB & 0x000000FF));
  427. sprintf(temp_str_2,"%02X",(BB & 0x0000FF00) >> 8);
  428. sprintf(temp_str_3,"%02X",(BB & 0x00FF0000) >> 16);
  429. sprintf(temp_str_4,"%02X",(BB & 0xFF000000) >> 24);
  430.  
  431. strcat(str,temp_str_1);
  432. strcat(str,temp_str_2);
  433. strcat(str,temp_str_3);
  434. strcat(str,temp_str_4);
  435.  
  436. /*printf("%02X%02X%02X%02X",
  437.  
  438.   (CC & 0x000000FF),
  439.  
  440.   (CC & 0x0000FF00) >> 8,
  441.  
  442.   (CC & 0x00FF0000) >> 16,
  443.  
  444.   (CC & 0xFF000000) >> 24);*/
  445.  
  446. sprintf(temp_str_1,"%02X",(CC & 0x000000FF));
  447. sprintf(temp_str_2,"%02X",(CC & 0x0000FF00) >> 8);
  448. sprintf(temp_str_3,"%02X",(CC & 0x00FF0000) >> 16);
  449. sprintf(temp_str_4,"%02X",(CC & 0xFF000000) >> 24);
  450.  
  451. strcat(str,temp_str_1);
  452. strcat(str,temp_str_2);
  453. strcat(str,temp_str_3);
  454. strcat(str,temp_str_4);
  455.  
  456. /*printf("%02X%02X%02X%02X",
  457.  
  458.   (DD & 0x000000FF),
  459.  
  460.   (DD & 0x0000FF00) >> 8,
  461.  
  462.   (DD & 0x00FF0000) >> 16,
  463.  
  464.   (DD & 0xFF000000) >> 24);*/
  465.  
  466. sprintf(temp_str_1,"%02X",(DD & 0x000000FF));
  467. sprintf(temp_str_2,"%02X",(DD & 0x0000FF00) >> 8);
  468. sprintf(temp_str_3,"%02X",(DD & 0x00FF0000) >> 16);
  469. sprintf(temp_str_4,"%02X",(DD & 0xFF000000) >> 24);
  470.  
  471. strcat(str,temp_str_1);
  472. strcat(str,temp_str_2);
  473. strcat(str,temp_str_3);
  474. strcat(str,temp_str_4);
  475.  
  476. //printf("%s",str);
  477.  
  478. return str;
  479.  
  480. }
  481.  
  482.  
  483.  
  484. int main()
  485.  
  486. {
  487. FILE* fp;
  488. FILE* fp1;
  489. fp=fopen("eng.txt","r");
  490. //fp1=fopen("HashedPassword.txt","r");
  491. fp1=fopen("test.txt","r");
  492. char word[20]={'\0'};
  493. char* hash_password=malloc(33*sizeof(char));
  494. memset(hash_password,'\0',33);
  495. char* md5_result;
  496. //char* md5_result=malloc(33*sizeof(char));//用array就不行??
  497. //memset(md5_result,'\0',33);
  498. int i,j,z;
  499. char ch;
  500. int find_password_num=0;
  501.  
  502. /*md5_result=MD5("",0);
  503.   printf("md5_result=%s\n",md5_result);
  504.   printf("\n");
  505.  
  506.   md5_result=MD5("123456",strlen("123456"));
  507.   printf("md5_result=%s\n",md5_result);
  508.   printf("\n");
  509.  
  510.   md5_result=MD5("abc",3);
  511.   printf("md5_result=%s\n",md5_result);
  512.   printf("\n");
  513.  
  514.   md5_result=MD5("message digest",14);
  515.   printf("md5_result=%s\n",md5_result);
  516.   printf("\n");
  517.  
  518.   md5_result=MD5("abcdefghijklmnopqrstuvwxyz",26);
  519.   printf("md5_result=%s\n",md5_result);
  520.   printf("\n");*/
  521.  
  522. while(fscanf(fp,"%s",word)==1)
  523. {
  524. md5_result=MD5(word,strlen(word));
  525. while(fscanf(fp1,"%s",hash_password)==1)
  526. {
  527. printf("%s\n",hash_password);
  528. if(stricmp(md5_result,hash_password)==0)
  529. {
  530. //printf("%s %s\n",md5_result,hash_password);
  531. find_password_num++;
  532. }
  533. }
  534. system("pause");
  535. rewind(fp1);
  536. //printf("word=%s md5_result=%s\n",word,md5_result);
  537.  
  538. }
  539. printf("find_password_num=%d",find_password_num);
  540. system("pause");
  541.  
  542. return 0;
  543.  
  544. }
  545.  
  546. int search_word(char* str)//,struct node* root)
  547. {
  548. struct node* current=root;
  549. int i=strlen(str)-1;
  550. char ch;
  551.  
  552. for(i=strlen(str)-1;i>=0;i--)
  553. {
  554. ch=str[i];
  555. if(current->alphabet[ch-'a']==NULL)
  556. {
  557. //printf("i=%d current->flag=%d\n",i,current->flag);
  558. return 0;
  559. }
  560. else if(current->alphabet[ch-'a']!=NULL)
  561. {
  562.  
  563. current=current->alphabet[ch-'a'];
  564. //printf("i=%d current->flag=%d\n",i,current->flag);
  565. if(i==0)
  566. {
  567. if(current->flag==1)
  568. {
  569. return 1;
  570. }
  571. else
  572. {
  573. return 0;
  574. }
  575. }
  576. else
  577. {
  578.  
  579. }
  580. }
  581. }
  582. }
  583.  
  584.  
  585.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘MD5’:
prog.c:386: warning: unused variable ‘temp_num_4’
prog.c:385: warning: unused variable ‘temp_num_3’
prog.c:384: warning: unused variable ‘temp_num_2’
prog.c:383: warning: unused variable ‘temp_num_1’
prog.c: In function ‘main’:
prog.c:524: warning: pointer targets in passing argument 1 of ‘MD5’ differ in signedness
prog.c:528: warning: implicit declaration of function ‘stricmp’
prog.c:499: warning: unused variable ‘ch’
prog.c:498: warning: unused variable ‘z’
prog.c:498: warning: unused variable ‘j’
prog.c:498: warning: unused variable ‘i’
prog.c:534: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result
prog.c:540: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result
prog.c: In function ‘search_word’:
prog.c:582: warning: control reaches end of non-void function
/home/T6c429/ccos6lIB.o: In function `main':
prog.c:(.text+0xb78): undefined reference to `stricmp'
collect2: ld returned 1 exit status
stdout
Standard output is empty