How do Java and C++ implement the same MD5 encryption algorithm
This article mainly explains the "Java and C++ how to achieve the same MD5 encryption algorithm", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java and C++ how to achieve the same MD5 encryption algorithm" it!
Catalogue
Java implements the same MD5 encryption algorithm as C++.
1. Java version
2. C++ code
3. Running effect
Java implements the same MD5 encryption algorithm as C++. 1. Java 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 ("x", b & 0xff);} digest = sb.toString () } catch (UnsupportedEncodingException ex) {/ / Logger.getLogger (StringReplace.class.getName ()) .log (Level.SEVERE, null, ex);} catch (NoSuchAlgorithmException ex) {/ / Logger.getLogger (StringReplace.class.getName ()) .log (Level.SEVERE, null, ex);} return digest } public static void main (String [] args) {System.out.println (md5Java ("admin"). ToUpperCase ()) 2, C++ Code (1) md5.h#include # include void MD5Digest (char * pszInput, unsigned long nInputSize, char * pszOutPut); (2) md5.cpp#include # include "md5.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 [16], MD5_CTX *) # define S11 7 # define S12 12 # define S13 17 # define S14 22 # define S21 5 # define S22 9 # define S23 14 # define S24 20 # define S31 4 # define S32 11 # define S33 16 # define S34 23 # define S41 6 # define S42 10 # define S43 15 # define S44 21 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, 0, 0, 0, 0} # define F (x, y, z) ((x) & (y)) | (~ x) & (z)) # define G (x, y, z) ((x) & (z)) | ((y) & (~ z) # define H (x, y) Z) ((x) ^ (y) ^ (z)) # define I (x, y, z) ((y) ^ (x) | (~ z) # define ROTATE_LEFT (x, n) ((x) > (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
< len; i++, j += 4) { output[j] = (unsigned char)(input[i] & 0xff); output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); output [jacks 2] = (unsigned char) ((input [I] > 16) & 0xff); output [jacks 3] = (unsigned char) ((input [I] > 24) & 0xff);}} inline void Decode (UINT4 * output, unsigned char * input, unsigned int len) {unsigned int i, j For (I = 0, j = 0; j)
< len; i++, j += 4) output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) state[0] = 0x67452301; context->State [1] = 0xefcdab89; context- > state [2] = 0x98badcfe; context- > state [3] = 0x10325476;} inline void MD5Update (MD5_CTX * context, unsigned char * input, unsigned int inputLen) {unsigned int i, index, partLen; index = (unsigned int) ((context- > count [0] > 3) & 0x3F) If ((context- > count [0] + = (UINT4) inputLen count [1] + = ((UINT4) inputLen > > 29); partLen = 64-index; if (inputLen > = partLen) {memcpy ((POINTER) & context- > buffer [index], (POINTER) input, partLen); MD5Transform (context- > state, context- > buffer); for (I = partLen; I + 63)
< inputLen; i += 64) MD5Transform (context->State, & input [I]); index = 0;} else I = 0; memcpy ((POINTER) & context- > buffer [index], (POINTER) & 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- > count, 8) Index = (unsigned int) ((context- > count [0] > > 3) & 0x3f); padLen = (index
< 56) ? (56 - index) : (120 - index); MD5Update (context, PADDING, padLen); MD5Update (context, bits, 8); Encode (digest, context->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 (& context); MD5Update (& context, (unsigned char *) pszInput, len); MD5Final ((unsigned char *) pszOutPut, & context) } main () {char szDigest [16]; char encrypt [200]; printf ("Please enter the string to calculate the MD5 value:"); gets (encrypt); printf ("\ nencryption result:"); MD5Digest (encrypt,strlen (encrypt), szDigest); int i; for