Java与C++实现相同的MD5加密算法

来源:互联网 发布:淘宝退换货流程图 编辑:程序博客网 时间:2024/06/03 11:35

原文转自:http://blog.csdn.net/l1028386804/article/details/47025267


1、Java版

[java] view plaincopy
  1. package com.lyz.utils.common;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.security.MessageDigest;  
  5. import java.security.NoSuchAlgorithmException;  
  6. /** 
  7.  * MD5加密 
  8.  * @author liuyazhuang 
  9.  */  
  10. public class MD5Hash {  
  11.   
  12.     public static String md5Java(String message) {  
  13.         String digest = null;  
  14.         try {  
  15.             MessageDigest md = MessageDigest.getInstance("MD5");  
  16.             byte[] hash = md.digest(message.getBytes("UTF-8"));  
  17.   
  18.             //converting byte array to Hexadecimal String  
  19.             StringBuilder sb = new StringBuilder(2 * hash.length);  
  20.             for (byte b : hash) {  
  21.                 sb.append(String.format("%02x", b & 0xff));  
  22.             }  
  23.   
  24.             digest = sb.toString();  
  25.   
  26.         } catch (UnsupportedEncodingException ex) {  
  27.             //Logger.getLogger(StringReplace.class.getName()).log(Level.SEVERE, null, ex);  
  28.         } catch (NoSuchAlgorithmException ex) {  
  29.             //Logger.getLogger(StringReplace.class.getName()).log(Level.SEVERE, null, ex);  
  30.         }  
  31.         return digest;  
  32.     }  
  33.     public static void main(String[] args) {  
  34.         System.out.println(md5Java("admin").toUpperCase());  
  35.     }  
  36. }  

2、C++代码

(1)md5.h
[cpp] view plaincopy
  1. #include   <stdio.h>    
  2. #include   <stdlib.h>  
  3. #include   <time.h>    
  4. #include   <string.h>    
  5. void   MD5Digest(char   *pszInput,   unsigned   long   nInputSize,   char   *pszOutPut);  
(2)md5.cpp

[cpp] view plaincopy
  1. #include   <stdio.h>    
  2. #include   <stdlib.h>  
  3. #include   <time.h>    
  4. #include   <string.h>    
  5. #include   "md5.h"  
  6.   
  7. typedef   unsigned   char   *POINTER;    
  8. typedef   unsigned   short   int   UINT2;    
  9. typedef   unsigned   long   int   UINT4;    
  10.   
  11. typedef   struct      
  12. {    
  13.   UINT4   state[4];    
  14.   UINT4   count[2];    
  15.   unsigned   char   buffer[64];    
  16. }   MD5_CTX;    
  17.   
  18. void   MD5Init(MD5_CTX   *);    
  19. void   MD5Update(MD5_CTX   *,   unsigned   char   *,   unsigned   int);    
  20. void   MD5Final(unsigned   char   [16],   MD5_CTX   *);    
  21.   
  22. #define   S11   7    
  23. #define   S12   12    
  24. #define   S13   17    
  25. #define   S14   22    
  26. #define   S21   5    
  27. #define   S22   9    
  28. #define   S23   14    
  29. #define   S24   20    
  30. #define   S31   4    
  31. #define   S32   11    
  32. #define   S33   16    
  33. #define   S34   23    
  34. #define   S41   6    
  35. #define   S42   10    
  36. #define   S43   15    
  37. #define   S44   21    
  38.   
  39. static   unsigned   char   PADDING[64]   =   {    
  40.   0x80,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,    
  41.   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,    
  42.   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0    
  43. };    
  44.   
  45. #define   F(x,   y,   z)   (((x)   &   (y))   |   ((~x)   &   (z)))    
  46. #define   G(x,   y,   z)   (((x)   &   (z))   |   ((y)   &   (~z)))    
  47. #define   H(x,   y,   z)   ((x)   ^   (y)   ^   (z))    
  48. #define   I(x,   y,   z)   ((y)   ^   ((x)   |   (~z)))    
  49.   
  50. #define   ROTATE_LEFT(x,   n)   (((x)   <<   (n))   |   ((x)   >>   (32-(n))))    
  51.   
  52. #define   FF(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   F   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }    
  53. #define   GG(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   G   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }    
  54. #define   HH(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   H   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }    
  55. #define   II(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   I   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);   }    
  56.   
  57.   
  58. inline   void   Encode(unsigned   char   *output,   UINT4   *input,   unsigned   int   len)    
  59. {    
  60.   unsigned   int   i,   j;    
  61.    
  62.   for   (i   =   0,   j   =   0;   j   <   len;   i++,   j   +=   4)   {    
  63.     output[j]   =   (unsigned   char)(input[i]   &   0xff);    
  64.     output[j+1]   =   (unsigned   char)((input[i]   >>   8)   &   0xff);    
  65.     output[j+2]   =   (unsigned   char)((input[i]   >>   16)   &   0xff);    
  66.     output[j+3]   =   (unsigned   char)((input[i]   >>   24)   &   0xff);    
  67.   }    
  68. }    
  69.   
  70. inline   void   Decode(UINT4   *output,   unsigned   char   *input,   unsigned   int   len)    
  71. {    
  72.   unsigned   int   i,   j;    
  73.    
  74.   for   (i   =   0,   j   =   0;   j   <   len;   i++,   j   +=   4)    
  75.     output[i]   =   ((UINT4)input[j])   |   (((UINT4)input[j+1])   <<   8)   |    
  76.   (((UINT4)input[j+2])   <<   16)   |   (((UINT4)input[j+3])   <<   24);    
  77. }    
  78.   
  79. inline   void   MD5Transform   (UINT4   state[4],   unsigned   char   block[64])    
  80. {    
  81.   UINT4   a   =   state[0],   b   =   state[1],   c   =   state[2],   d   =   state[3],   x[16];    
  82.   Decode   (x,   block,   64);    
  83.   FF   (a,   b,   c,   d,   x[   0],   S11,   0xd76aa478);      
  84.   FF   (d,   a,   b,   c,   x[   1],   S12,   0xe8c7b756);      
  85.   FF   (c,   d,   a,   b,   x[   2],   S13,   0x242070db);      
  86.   FF   (b,   c,   d,   a,   x[   3],   S14,   0xc1bdceee);     
  87.   FF   (a,   b,   c,   d,   x[   4],   S11,   0xf57c0faf);     
  88.   FF   (d,   a,   b,   c,   x[   5],   S12,   0x4787c62a);     
  89.   FF   (c,   d,   a,   b,   x[   6],   S13,   0xa8304613);     
  90.   FF   (b,   c,   d,   a,   x[   7],   S14,   0xfd469501);     
  91.   FF   (a,   b,   c,   d,   x[   8],   S11,   0x698098d8);    
  92.   FF   (d,   a,   b,   c,   x[   9],   S12,   0x8b44f7af);     
  93.   FF   (c,   d,   a,   b,   x[10],   S13,   0xffff5bb1);     
  94.   FF   (b,   c,   d,   a,   x[11],   S14,   0x895cd7be);    
  95.   FF   (a,   b,   c,   d,   x[12],   S11,   0x6b901122);     
  96.   FF   (d,   a,   b,   c,   x[13],   S12,   0xfd987193);    
  97.   FF   (c,   d,   a,   b,   x[14],   S13,   0xa679438e);    
  98.   FF   (b,   c,   d,   a,   x[15],   S14,   0x49b40821);    
  99.   GG   (a,   b,   c,   d,   x[   1],   S21,   0xf61e2562);   
  100.   GG   (d,   a,   b,   c,   x[   6],   S22,   0xc040b340);     
  101.   GG   (c,   d,   a,   b,   x[11],   S23,   0x265e5a51);      
  102.   GG   (b,   c,   d,   a,   x[   0],   S24,   0xe9b6c7aa);      
  103.   GG   (a,   b,   c,   d,   x[   5],   S21,   0xd62f105d);     
  104.   GG   (d,   a,   b,   c,   x[10],   S22,     0x2441453);    
  105.   GG   (c,   d,   a,   b,   x[15],   S23,   0xd8a1e681);     
  106.   GG   (b,   c,   d,   a,   x[   4],   S24,   0xe7d3fbc8);    
  107.   GG   (a,   b,   c,   d,   x[   9],   S21,   0x21e1cde6);     
  108.   GG   (d,   a,   b,   c,   x[14],   S22,   0xc33707d6);    
  109.   GG   (c,   d,   a,   b,   x[   3],   S23,   0xf4d50d87);    
  110.   GG   (b,   c,   d,   a,   x[   8],   S24,   0x455a14ed);    
  111.   GG   (a,   b,   c,   d,   x[13],   S21,   0xa9e3e905);     
  112.   GG   (d,   a,   b,   c,   x[   2],   S22,   0xfcefa3f8);    
  113.   GG   (c,   d,   a,   b,   x[   7],   S23,   0x676f02d9);   
  114.   GG   (b,   c,   d,   a,   x[12],   S24,   0x8d2a4c8a);     
  115.   HH   (a,   b,   c,   d,   x[   5],   S31,   0xfffa3942);    
  116.   HH   (d,   a,   b,   c,   x[   8],   S32,   0x8771f681);    
  117.   HH   (c,   d,   a,   b,   x[11],   S33,   0x6d9d6122);    
  118.   HH   (b,   c,   d,   a,   x[14],   S34,   0xfde5380c);      
  119.   HH   (a,   b,   c,   d,   x[   1],   S31,   0xa4beea44);      
  120.   HH   (d,   a,   b,   c,   x[   4],   S32,   0x4bdecfa9);     
  121.   HH   (c,   d,   a,   b,   x[   7],   S33,   0xf6bb4b60);      
  122.   HH   (b,   c,   d,   a,   x[10],   S34,   0xbebfbc70);     
  123.   HH   (a,   b,   c,   d,   x[13],   S31,   0x289b7ec6);     
  124.   HH   (d,   a,   b,   c,   x[   0],   S32,   0xeaa127fa);    
  125.   HH   (c,   d,   a,   b,   x[   3],   S33,   0xd4ef3085);    
  126.   HH   (b,   c,   d,   a,   x[   6],   S34,     0x4881d05);    
  127.   HH   (a,   b,   c,   d,   x[   9],   S31,   0xd9d4d039);      
  128.   HH   (d,   a,   b,   c,   x[12],   S32,   0xe6db99e5);    
  129.   HH   (c,   d,   a,   b,   x[15],   S33,   0x1fa27cf8);     
  130.   HH   (b,   c,   d,   a,   x[   2],   S34,   0xc4ac5665);    
  131.   II   (a,   b,   c,   d,   x[   0],   S41,   0xf4292244);    
  132.   II   (d,   a,   b,   c,   x[   7],   S42,   0x432aff97);    
  133.   II   (c,   d,   a,   b,   x[14],   S43,   0xab9423a7);     
  134.   II   (b,   c,   d,   a,   x[   5],   S44,   0xfc93a039);     
  135.   II   (a,   b,   c,   d,   x[12],   S41,   0x655b59c3);    
  136.   II   (d,   a,   b,   c,   x[   3],   S42,   0x8f0ccc92);     
  137.   II   (c,   d,   a,   b,   x[10],   S43,   0xffeff47d);     
  138.   II   (b,   c,   d,   a,   x[   1],   S44,   0x85845dd1);     
  139.   II   (a,   b,   c,   d,   x[   8],   S41,   0x6fa87e4f);      
  140.   II   (d,   a,   b,   c,   x[15],   S42,   0xfe2ce6e0);    
  141.   II   (c,   d,   a,   b,   x[   6],   S43,   0xa3014314);    
  142.   II   (b,   c,   d,   a,   x[13],   S44,   0x4e0811a1);    
  143.   II   (a,   b,   c,   d,   x[   4],   S41,   0xf7537e82);    
  144.   II   (d,   a,   b,   c,   x[11],   S42,   0xbd3af235);    
  145.   II   (c,   d,   a,   b,   x[   2],   S43,   0x2ad7d2bb);     
  146.   II   (b,   c,   d,   a,   x[   9],   S44,   0xeb86d391);    
  147.   state[0]   +=   a;    
  148.   state[1]   +=   b;    
  149.   state[2]   +=   c;    
  150.   state[3]   +=   d;    
  151.   memset   ((POINTER)x,   0,   sizeof   (x));    
  152.   }    
  153.      
  154. inline   void   MD5Init(MD5_CTX   *context)    
  155. {    
  156.   context->count[0]   =   context->count[1]   =   0;    
  157.   context->state[0]   =   0x67452301;    
  158.   context->state[1]   =   0xefcdab89;    
  159.   context->state[2]   =   0x98badcfe;    
  160.   context->state[3]   =   0x10325476;    
  161. }    
  162.   
  163. inline   void   MD5Update(MD5_CTX   *context,   unsigned   char   *input,   unsigned   int   inputLen)    
  164. {    
  165.   unsigned   int   i,   index,   partLen;    
  166.    
  167.   index   =   (unsigned   int)((context->count[0]   >>   3)   &   0x3F);    
  168.   if   ((context->count[0]   +=   ((UINT4)inputLen   <<   3))    
  169.     <   ((UINT4)inputLen   <<   3))    
  170.     context->count[1]++;    
  171.   context->count[1]   +=   ((UINT4)inputLen   >>   29);    
  172.    
  173.   partLen   =   64   -   index;    
  174.    
  175.   if   (inputLen   >=   partLen)   {    
  176.     memcpy((POINTER)&context->buffer[index],   (POINTER)input,   partLen);    
  177.     MD5Transform(context->state,   context->buffer);    
  178.       
  179.     for   (i   =   partLen;   i   +   63   <   inputLen;   i   +=   64)    
  180.       MD5Transform   (context->state,   &input[i]);    
  181.     index   =   0;    
  182.   }    
  183.   else    
  184.     i   =   0;    
  185.    
  186.   memcpy((POINTER)&context->buffer[index],   (POINTER)&input[i],   inputLen-i);    
  187. }    
  188.   
  189. inline   void   MD5Final(unsigned   char   digest[16],   MD5_CTX   *context)    
  190. {    
  191.   unsigned   char   bits[8];    
  192.   unsigned   int   index,   padLen;    
  193.    
  194.   Encode   (bits,   context->count,   8);    
  195.   index   =   (unsigned   int)((context->count[0]   >>   3)   &   0x3f);    
  196.   padLen   =   (index   <   56)   ?   (56   -   index)   :   (120   -   index);    
  197.   MD5Update   (context,   PADDING,   padLen);    
  198.   MD5Update   (context,   bits,   8);    
  199.   Encode   (digest,   context->state,   16);    
  200.   memset   ((POINTER)context,   0,   sizeof   (*context));    
  201.   }    
  202.   
  203. void   MD5Digest(char   *pszInput,   unsigned   long   nInputSize,   char   *pszOutPut)    
  204. {    
  205.   MD5_CTX   context;    
  206.   unsigned   int   len   =   strlen   (pszInput);    
  207.    
  208.   MD5Init   (&context);    
  209.   MD5Update   (&context,   (unsigned   char   *)pszInput,   len);    
  210.   MD5Final   ((unsigned   char   *)pszOutPut,   &context);    
  211. }    
  212.   
  213. main()    
  214. char szDigest[16];    
  215.   char encrypt[200];    
  216.   printf("请输入要计算MD5值的字符串:");  
  217.   gets(encrypt);  
  218.   printf("\n加密结果:");  
  219.   MD5Digest(encrypt,strlen(encrypt),szDigest);    
  220.   int i;  
  221.   for (i=0;i<16;i++) printf ("%02X",(unsigned char)szDigest[i]);  
  222.   getchar();  
  223.   
  224. }  

3、运行效果

这里我们都以输入123456为例

(1)java输出结果如下:

(2)C++输出结果如下:


版权声明:本文为博主原创文章,未经博主允许不得转载。


0 0
原创粉丝点击