工作中需要对网络传输的二进制配置文件进行md5运算生成校验码,以避免在网络传输中,文件的内容发生损坏和篡改。过程中大致是这样的:
1.服务端c++,请求端java;
2.请求端POST请求服务器上的某个文件,要求传输到请求端;
3.向服务端请求文件时,服务端需要对要传输的文件内容作md5加密,生成32字符校验码,将校验码作为http头信息字段传输到请求端;
4.请求端对传过来的文件也进行MD5加密,生成的校验码和服务器传来的做比较,相同即保证了数据的完整性和合法性,并对内容进行处理,否则,丢弃。
遇到的问题:需要保证请求端和服务器的md5算法一致,而往往C++和java的md5算法有多种,这里我们采用C++依照java的md5算法来实现,即用与java的MD5算法一致的C++算法来解决。
代码链接来源:http://blog.csdn.net/beyondhaven/article/details/8275110(谢谢BeyondHaven^_^)
MD5.h
1 #ifndef _DMSUTIL_H 2 #define _DMSUTIL_H 3 #ifdef __cplusplus 4 extern "C" { 5 #endif 6 /*————————————————————————. 7 | md5 encrypt function 8 .————————————————————————*/ 9 void EncryptMD5(unsigned char *output, unsigned char *input); 10 void EncryptMD5str(char *output, unsigned char *input, int len); 11 /*————————————————————————. 12 | hash encrypt function 13 .————————————————————————*/ 14 void EncryptHash(char *crypt, char *source); 15 /*————————————————————————. 16 | DES encrypt and decrypt function 17 .————————————————————————*/ 18 void EncryptDes(unsigned char *source, unsigned char *encrypt); 19 void DecryptDes(unsigned char *encrypt, unsigned char *source); 20 #ifdef __cplusplus 21 } 22 #endif 23 #endif
MD5.cpp
1 #include <stdio.h> 2 #include <string.h> 3 #include "MD5Encrypt.h" 4 /*MD5 context*/ 5 typedef struct MD5_CTX_S{ 6 unsigned int state[4]; 7 unsigned int count[2]; 8 char buffer[64]; 9 }MD5_CTX; 10 11 static unsigned char digest[16]; 12 /*MD5 initialization*/ 13 static void MD5Init(MD5_CTX *); 14 /*MD5 block update operation*/ 15 static void MD5Update(MD5_CTX *, unsigned char *, unsigned int); 16 /*Ends an MD5 message-digest operation*/ 17 static void MD5Final(unsigned char [16], MD5_CTX *); 18 /*MD5 padding*/ 19 static void __md5_Pad (MD5_CTX *); 20 /*MD5 basic transformation*/ 21 static void __md5_Transform (unsigned int [4], unsigned char [64]); 22 #ifdef i386 23 #define __md5_Encode memcpy 24 #define __md5_Decode memcpy 25 #else /* i386 */ 26 /* 27 * __md5_Encodes input (u_int32_t) into output (unsigned char). Assumes len is 28 * a multiple of 4. 29 */ 30 static void 31 __md5_Encode (unsigned char *output, unsigned int *input, unsigned int len) 32 { 33 unsigned int i, j; 34 for (i = 0, j = 0; j < len; i++, j += 4) { 35 output[j] = (unsigned char)(input[i] & 0xff); 36 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 37 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 38 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 39 } 40 } 41 /* 42 * __md5_Decodes input (unsigned char) into output (u_int32_t). Assumes len is 43 * a multiple of 4. 44 */ 45 static void 46 __md5_Decode (unsigned int *output, unsigned char *input, unsigned int len) 47 { 48 unsigned int i, j; 49 for (i = 0, j = 0; j < len; i++, j += 4) 50 output[i] = ((unsigned int)input[j]) | (((unsigned int)input[j+1]) << 8) | 51 (((unsigned int)input[j+2]) << 16) | (((unsigned int)input[j+3]) << 24); 52 } 53 #endif /* i386 */ 54 /* F, G, H and I are basic MD5 functions. */ 55 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 56 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 57 #define H(x, y, z) ((x) ^ (y) ^ (z)) 58 #define I(x, y, z) ((y) ^ ((x) | (~z))) 59 /* ROTATE_LEFT rotates x left n bits. */ 60 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 61 /* 62 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 63 * Rotation is separate from addition to prevent recomputation. 64 */ 65 #define FF(a, b, c, d, x, s, ac) { 66 (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); 67 (a) = ROTATE_LEFT ((a), (s)); 68 (a) += (b); 69 } 70 #define GG(a, b, c, d, x, s, ac) { 71 (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); 72 (a) = ROTATE_LEFT ((a), (s)); 73 (a) += (b); 74 } 75 #define HH(a, b, c, d, x, s, ac) { 76 (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); 77 (a) = ROTATE_LEFT ((a), (s)); 78 (a) += (b); 79 } 80 #define II(a, b, c, d, x, s, ac) { 81 (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); 82 (a) = ROTATE_LEFT ((a), (s)); 83 (a) += (b); 84 } 85 /* MD5 initialization. Begins an MD5 operation, writing a new context. */ 86 static void MD5Init (MD5_CTX *context) 87 { 88 context->count[0] = context->count[1] = 0; 89 /* Load magic initialization constants. */ 90 context->state[0] = 0x67452301; 91 context->state[1] = 0xefcdab89; 92 context->state[2] = 0x98badcfe; 93 context->state[3] = 0x10325476; 94 } 95 /* 96 * MD5 block update operation. Continues an MD5 message-digest 97 * operation, processing another message block, and updating the 98 * context. 99 */ 100 static void MD5Update ( MD5_CTX *context, unsigned char *input, unsigned int inputLen) 101 { 102 unsigned int i, index, partLen; 103 /* Compute number of bytes mod 64 */ 104 index = (unsigned int)((context->count[0] >> 3) & 0x3F); 105 /* Update number of bits */ 106 if ((context->count[0] += ((unsigned int)inputLen << 3)) 107 < ((unsigned int)inputLen << 3)) 108 context->count[1]++; 109 context->count[1] += ((unsigned int)inputLen >> 29); 110 partLen = 64 -index; 111 /* Transform as many times as possible. */ 112 if (inputLen >= partLen) { 113 memcpy((void *)&context->buffer[index], (const void *)input, 114 partLen); 115 __md5_Transform (context->state, (unsigned char*)context->buffer); 116 for (i = partLen; i + 63 < inputLen; i += 64) 117 __md5_Transform (context->state, &input[i]); 118 index = 0; 119 } 120 else 121 i = 0; 122 /* Buffer remaining input */ 123 memcpy ((void *)&context->buffer[index], (const void *)&input[i], 124 inputLen-i); 125 } 126 /* 127 * MD5 padding. Adds padding followed by original length. 128 */ 129 static void __md5_Pad ( MD5_CTX *context) 130 { 131 unsigned char bits[8]; 132 unsigned int index, padLen; 133 unsigned char PADDING[64]; 134 memset(PADDING, 0, sizeof(PADDING)); 135 PADDING[0] = 0x80; 136 /* Save number of bits */ 137 __md5_Encode (bits, context->count, 8); 138 /* Pad out to 56 mod 64. */ 139 index = (unsigned int)((context->count[0] >> 3) & 0x3f); 140 padLen = (index < 56) ? (56 - index) : (120 - index); 141 MD5Update (context, PADDING, padLen); 142 /* Append length (before padding) */ 143 MD5Update (context, bits, 8); 144 } 145 /* 146 * MD5 finalization. Ends an MD5 message-digest operation, writing the 147 * the message digest and zeroizing the context. 148 */ 149 static void MD5Final ( unsigned char digest[16], MD5_CTX *context) 150 { 151 /* Do padding. */ 152 __md5_Pad (context); 153 /* Store state in digest */ 154 __md5_Encode (digest, context->state, 16); 155 /* Zeroize sensitive information. */ 156 memset ((void *)context, 0, sizeof (*context)); 157 } 158 /* MD5 basic transformation. Transforms state based on block. */ 159 static void 160 __md5_Transform (unsigned int state[4], unsigned char block[64]) 161 { 162 unsigned int a, b, c, d, x[16]; 163 a=state[0]; 164 b=state[1]; 165 c=state[2]; 166 d=state[3]; 167 __md5_Decode (x, block, 64); 168 /* Round 1 */ 169 #define S11 7 170 #define S12 12 171 #define S13 17 172 #define S14 22 173 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 174 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 175 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 176 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 177 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 178 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 179 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 180 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 181 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 182 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 183 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 184 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 185 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 186 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 187 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 188 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 189 /* Round 2 */ 190 #define S21 5 191 #define S22 9 192 #define S23 14 193 #define S24 20 194 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 195 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 196 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 197 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 198 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 199 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 200 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 201 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 202 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 203 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 204 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 205 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 206 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 207 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 208 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 209 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 210 /* Round 3 */ 211 #define S31 4 212 #define S32 11 213 #define S33 16 214 #define S34 23 215 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 216 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 217 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 218 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 219 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 220 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 221 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 222 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 223 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 224 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 225 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 226 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ 227 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 228 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 229 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 230 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 231 /* Round 4 */ 232 #define S41 6 233 #define S42 10 234 #define S43 15 235 #define S44 21 236 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 237 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 238 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 239 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 240 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 241 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 242 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 243 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 244 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 245 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 246 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 247 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 248 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 249 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 250 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 251 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 252 state[0] += a; 253 state[1] += b; 254 state[2] += c; 255 state[3] += d; 256 /* Zeroize sensitive information. */ 257 memset ((void *)x, 0, sizeof (x)); 258 } 259 void EncryptMD5(unsigned char *output, unsigned char *input) 260 { 261 MD5_CTX context; 262 MD5Init(&context); 263 MD5Update(&context, input, (unsigned int)strlen((char *)input)); 264 MD5Final(output, &context); 265 return; 266 } 267 void EncryptMD5str(char *output, unsigned char *input, int len) 268 { 269 unsigned char strcrypt[16]; 270 int i; 271 MD5_CTX context; 272 MD5Init(&context); 273 MD5Update(&context, input, len); 274 MD5Final(strcrypt, &context); 275 for (i=0; i < 16; i++) 276 sprintf(&output[i*2], "%02x", strcrypt[i]); 277 return; 278 }
测试用例
1 int main(int argc, char **argv) 2 { 3 int content_source_len = 0; 4 char* source_content = NULL; 5 FILE* fp = fopen(m_contentSource.c_str() , "rb"); 6 if (fp != NULL) 7 { 8 fseek(fp, 0, SEEK_END); 9 content_source_len = ftell(fp); 10 fseek(fp, 0, SEEK_SET); 11 source_content = (char*) malloc(content_source_len * sizeof(char)); 12 fread(source_content, content_source_len , sizeof(char), fp); 13 fclose(fp); 14 } //md5code_string是要发送的校验码 15 char md5code_string[50] = {0}; 16 EncryptMD5str(md5code_string, (unsigned char *)source_content, content_source_len); 17 }
时间: 2024-10-12 05:19:06