#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #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) & (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) << (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[J+2] = (unsigned char) ((Input[i] >>) & 0xff);
OUTPUT[J+3] = (unsigned char) ((Input[i] >>) & 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]) << 8) | (((UINT4) input[j+2]) << 16) |
(((UINT4) input[j+3]) << 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->count[0] = context->count[1] = 0;
Context->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 << 3)) < ((UINT4) Inputlen << 3)) context->count[1]++;
CONTEXT->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 + < Inputlen;
i + +) 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 ("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<16;i++) printf ("%02x", (unsigned char) szdigest[i);
GetChar ();
}
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 ~