Java and C + + implement the same MD5 encryption algorithm simple example _java

Source: Internet
Author: User
Tags md5 md5 encryption stringreplace

1, java version

Package Com.lyz.utils.common; 
Import java.io.UnsupportedEncodingException; 
Import Java.security.MessageDigest; 
Import java.security.NoSuchAlgorithmException; 
    /** * MD5 Encryption * @author Liuyazhuang/public class Md5hash {public static string Md5java (String message) { 
    String digest = null; 
      try {messagedigest MD = messagedigest.getinstance ("MD5"); 
 
      Byte[] hash = md.digest (Message.getbytes ("UTF-8")); 
      Converting byte array to hexadecimal String StringBuilder sb = new StringBuilder (2 * hash.length); 
      for (byte B:hash) {Sb.append (String.Format ("%02x", B & 0xFF)); 
 
    } digest = Sb.tostring (); The catch (Unsupportedencodingexception ex) {//logger.getlogger (StringReplace.class.getName ()). log (Level.severe, nul 
    L, ex); The catch (NoSuchAlgorithmException ex) {//logger.getlogger (StringReplace.class.getName ()). log (Level.severe, NULL, E 
    x); 
  return digest; } public StatIC void Main (string[] args) {System.out.println (Md5java ("admin"). toUpperCase ()); } 
}

2, C + + code

(1) md5.h

#include  <stdio.h> 
#include  <stdlib.h>
#include  <time.h> 
#include  <string.h> 
void  md5digest (char  *pszinput,  unsigned  long  ninputsize,  Char  *pszoutput);

(2) Md5.cpp

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #include "MD 
5.h "typedef unsigned char *pointer; 
typedef unsigned short int UINT2; 

typedef unsigned long int UINT4; 
 typedef struct {UINT4 state[4]; 
 UINT4 count[2]; 
unsigned char buffer[64]; 

} Md5_ctx; 
void Md5init (MD5_CTX *); 
void Md5update (MD5_CTX *, unsigned char *, unsigned int); 

void md5final (unsigned char [], md5_ctx *); 
#define S11 7 #define S12 #define S13 #define S14 #define S21 5 #define S22 9 #define S23 14 #define S24 #define S31 4 #define S32 #define S33 #define S34 #define S41 6 #define S42 1  0 #define S43 #define S44 static unsigned char padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0,  0-0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  #define F (x, Y, Z) (((x) &amp; (y)) | 
((~x) &amp; (z))  #define G (x, Y, Z) (((x) &amp; (z)) |  ((y) &amp; (~z)) #define H (x, Y, z) ((x) ^ (y) ^ (z)) #define I (x, Y, z) ((y) ^ ((x) | 

(~z)))  #define ROTATE_LEFT (x, N) (((x) &lt;&lt; (n)) | ((x) &gt;&gt; (32-(n))) #define FF (A, B, C, D, X, S, ac) {(a) + F ((b), (c), (d)) + (x) + (UINT4   ) (AC);   (a) = Rotate_left ((a), (s));    (a) + + (b);   #define GG (A, B, C, D, X, S, ac) {(a) + = G ((b), (c), (d)) + (x) + (UINT4) (AC);   (a) = Rotate_left ((a), (s));    (a) + + (b);   #define HH (A, B, C, D, X, S, ac) {(a) + = H ((b), (c), (d)) + (x) + (UINT4) (AC);   (a) = Rotate_left ((a), (s));    (a) + + (b);   #define II (A, B, C, D, X, S, ac) {(a) + = I ((b), (c), (d)) + (x) + (UINT4) (AC); (a) = Rotate_left ((a), (s));  (a) + + (b); 
 
 } inline void Encode (unsigned char *output, UINT4 *input, unsigned int len) {unsigned int i, J;  for (i = 0, j = 0;  J &lt; Len; 
  i++, J + + 4) {Output[j] = (unsigned char) (input[i) &amp; 0xff); 
  OUTPUT[J+1] = (unsigned char) ((input[i) &gt;&gt; 8) &amp; 0xff); 
  OUTPUT[J+2] = (unsigned char) ((Input[i] &gt;&gt;) &amp; 0xff); 
 OUTPUT[J+3] = (unsigned char) ((Input[i] &gt;&gt;) &amp; 0xff); 
 
 } inline void Decode (UINT4 *output, unsigned char *input, unsigned int len) {unsigned int i, J;  for (i = 0, j = 0;  J &lt; Len;  i++, J + + 4) output[i] = ((UINT4) input[j]) | 
 (((UINT4) input[j+1]) &lt;&lt; 8) |  (((UINT4) input[j+2]) &lt;&lt; 16) | 
(((UINT4) input[j+3]) &lt;&lt; 24);   } inline void Md5transform (UINT4 state[4], unsigned char block[64]) {UINT4 a = state[0], B = state[1], 
 c = state[2], d = state[3], x[16]; Decode  (x, block, 64);  
 FF (A, B, C, D, x[0], S11, 0xd76aa478);  
 FF (d, a, B, C, x[1], S12, 0xe8c7b756);  
 FF (c, D, a, B, x[2], S13, 0x242070db);  
 FF (b, C, D, A, x[3], S14, 0xc1bdceee);  
 FF (A, B, C, D, x[4], S11, 0XF57C0FAF);  
 FF (d, a, B, C, x[5], S12, 0x4787c62a);  
 FF (c, D, a, B, x[6], S13, 0xa8304613);  
 FF (b, C, D, A, x[7], S14, 0xfd469501); 
 FF (A, B, C, D, x[8], S11, 0X698098D8);  
 FF (d, a, B, C, x[9], S12, 0X8B44F7AF);  
 FF (c, D, a, B, x[10], S13, 0XFFFF5BB1); 
 FF (b, C, D, A, x[11], S14, 0x895cd7be);  
 FF (A, B, C, D, X[12], S11, 0x6b901122); 
 FF (d, a, B, C, x[13], S12, 0xfd987193); 
 FF (c, D, a, B, x[14], S13, 0xa679438e); 
 FF (b, C, D, A, x[15], S14, 0x49b40821); 
 GG (A, B, C, D, x[1], S21, 0xf61e2562);  
 GG (d, a, B, C, x[6], S22, 0xc040b340);  
 GG (c, D, a, B, x[11], S23, 0x265e5a51); Gg (b, C, D, A, x[0], S24, 0XE9B6C7AA);  
 GG (A, B, C, D, x[5], S21, 0xd62f105d); 
 GG (d, a, B, C, x[10], S22, 0x2441453);  
 GG (c, D, a, B, x[15], S23, 0xd8a1e681); 
 GG (b, C, D, A, x[4], S24, 0XE7D3FBC8);  
 GG (A, B, C, D, x[9], S21, 0x21e1cde6); 
 GG (d, a, B, C, x[14], S22, 0XC33707D6); 
 GG (c, D, a, B, x[3], S23, 0xf4d50d87); 
 GG (b, C, D, A, x[8], S24, 0x455a14ed);  
 GG (A, B, C, D, X[13], S21, 0xa9e3e905); 
 GG (d, a, B, C, x[2], S22, 0XFCEFA3F8); 
 GG (c, D, a, B, x[7], S23, 0X676F02D9);  
 GG (b, C, D, A, x[12], S24, 0x8d2a4c8a); 
 HH (A, B, C, D, x[5], S31, 0xfffa3942); 
 HH (d, a, B, C, x[8], S32, 0x8771f681); 
 HH (c, D, a, B, x[11], S33, 0x6d9d6122);  
 HH (b, C, D, A, x[14], S34, 0xfde5380c);  
 HH (A, B, C, D, x[1], S31, 0xa4beea44);  
 HH (d, a, B, C, x[4], S32, 0x4bdecfa9); HH (c, D, a, B, x[7], S33, 0XF6BB4B60);  
 HH (b, C, D, A, x[10], S34, 0XBEBFBC70);  
 HH (A, B, C, D, X[13], S31, 0X289B7EC6); 
 HH (d, a, B, C, x[0], S32, 0XEAA127FA); 
 HH (c, D, a, B, x[3], S33, 0xd4ef3085); 
 HH (b, C, D, A, x[6], S34, 0X4881D05);  
 HH (A, B, C, D, x[9], S31, 0xd9d4d039); 
 HH (d, a, B, C, x[12], S32, 0xe6db99e5);  
 HH (c, D, a, B, x[15], S33, 0X1FA27CF8); 
 HH (b, C, D, A, x[2], S34, 0xc4ac5665); 
 II (A, B, C, D, x[0], S41, 0xf4292244); 
 II (d, a, B, C, x[7], S42, 0X432AFF97);  
 II (c, D, a, B, x[14], S43, 0XAB9423A7);  
 II (b, C, D, A, x[5], S44, 0xfc93a039); 
 II (A, B, C, D, X[12], S41, 0X655B59C3);  
 II (d, a, B, C, x[3], S42, 0x8f0ccc92);  
 II (c, D, a, B, x[10], S43, 0xffeff47d);  
 II (b, C, D, A, x[1], S44, 0X85845DD1);  
 II (A, B, C, D, x[8], S41, 0x6fa87e4f); 
 II (d, a, B, C, x[15], S42, 0XFE2CE6E0);  II (c,D, a, B, x[6], S43, 0xa3014314); 
 II (b, C, D, A, x[13], S44, 0X4E0811A1); 
 II (A, B, C, D, x[4], S41, 0xf7537e82); 
 II (d, a, B, C, x[11], S42, 0xbd3af235);  
 II (c, D, a, B, x[2], S43, 0X2AD7D2BB); 
 II (b, C, D, A, x[9], S44, 0xeb86d391); 
 State[0] + = A; 
 STATE[1] + b; 
 STATE[2] + = C; 
 STATE[3] + = D; 
 memset ((pointer) x, 0, sizeof (x)); 
 } inline void Md5init (Md5_ctx *context) {context-&gt;count[0] = context-&gt;count[1] = 0; 
 Context-&gt;state[0] = 0x67452301; 
 CONTEXT-&GT;STATE[1] = 0xefcdab89; 
 CONTEXT-&GT;STATE[2] = 0x98badcfe; 
CONTEXT-&GT;STATE[3] = 0x10325476;  } inline void Md5update (Md5_ctx *context, unsigned char *input, unsigned int inputlen) {unsigned int i, 
 
 index, Partlen; 
 index = (unsigned int) ((Context-&gt;count[0] &gt;&gt; 3) &amp; 0x3F); if ((context-&gt;count[0] + = ((UINT4) Inputlen &lt;&lt; 3)) &lt; ((UINT4) Inputlen &lt;&lt; 3)) context-&gt;count[1]++; 
 
 CONTEXT-&GT;COUNT[1] + + ((UINT4) Inputlen &gt;&gt; 29); 
 
 Partlen = 64-index; 
  if (Inputlen &gt;= partlen) {memcpy (pointer) &amp;context-&gt;buffer[index], (pointer) input, partlen); 
  
  Md5transform (Context-&gt;state, Context-&gt;buffer);  for (i = Partlen;  i + &lt; Inputlen; 
  i + +) Md5transform (Context-&gt;state, &amp;input[i]); 
 index = 0; 
 
 else i = 0; 
memcpy ((pointer) &amp;context-&gt;buffer[index], (pointer) &amp;input[i], inputlen-i); 
 } inline void md5final (unsigned char digest[16], md5_ctx *context) {unsigned char bits[8]; 
 
 unsigned int index, Padlen; 
 Encode (Bits, context-&gt;count, 8); 
 index = (unsigned int) ((Context-&gt;count[0] &gt;&gt; 3) &amp; 0x3f);  Padlen = (Index &lt; 56)? 
 (56-index): (120-index); 
 Md5update (context, PADDING, Padlen); 
 Md5update (context, bits, 8); Encode (Digest, Context-&gt;state, 16); 
 memset ((pointer) context, 0, sizeof (*context)); 
 } void Md5digest (char *pszinput, unsigned long ninputsize, char *pszoutput) {MD5_CTX context; 
 
 unsigned int len = strlen (pszinput); 
 Md5init (&amp;context); 
 Md5update (&amp;context, (unsigned char *) pszinput, Len); 
Md5final ((unsigned char *) pszoutput, &amp;context); 
 Main () {char szdigest[16]; 
 Char encrypt[200];
 printf ("Enter the string to calculate the MD5 value:");
 Gets (encrypt);
 printf ("\ n encryption Result:"); 
 Md5digest (Encrypt,strlen (Encrypt), szdigest);
 int i;
 for (i=0;i&lt;16;i++) printf ("%02x", (unsigned char) szdigest[i);

GetChar ();
 }

3. Operation effect

Here we all take the input 123456 as an example

(1) The results of Java output are as follows:

(2) C + + output results are as follows:

The above is a small series for everyone to bring Java and C + + to achieve the same MD5 encryption algorithm Simple example of all the content, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.