与java一致的md5算法(C++)

工作中需要对网络传输的二进制配置文件进行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

与java一致的md5算法(C++)的相关文章

Java 实现Md5算法

package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * MD5 算法*/public class MD5 {        // 全局数组    private final static String[] strDigits = { "0", "1", "2", "3", "

密码技术应用设计实践-安全信息传输系统(SITS)(用Java实现DES、RSA、MD5算法)

本系统包括五个模块,注册模块.登录模块.RSA算法模块.DES算法模块.MD5算法模块.这五个模块每一个实现不同的功能.注册模块实现将用户名和密码写入文件中,登录模块则负责将其读入并且判断其是否正确.RSA算法模块实现生成密钥对.加密和解密功能.DES算法模块实现加密和解密功能.MD5算法模块是实现生成摘要的功能. (1).首先为注册界面: package test; import javax.swing.*; import java.awt.*;   //导入必要的包 import java.

Java MD5算法

JAVA MD5算法  MD5即Message-Digest Algorithm 5(信息-摘要算法5),是一种用于产生数字签名的单项散列算法. MD5算法的作用是让大容量信息在用数字签名软件签私人密匙前被"压缩"成一种保密的格式,将一个任意长度的"字节串"通过一个不可逆的字符串变换算法变换成一个128bit的大整数. 即使你看到源程序和算法描述,也无法将一个MD5的值变换回原始的字符串,从数学原理上说,是因为原始的字符串有无穷多个,这有点象不存在反函数的数学函数.

Java利用MessageDigest提供的MD5算法加密字符串或文件

MD5是常用的加密算法,也经常用于校验信息完整,如文件的完整性.用术语讲,MD5是一种消息摘要算法(Message Digest Algorithm).另外还有一种常用的消息摘要算法SHA1.如果想了解这些的话,可以去百度百科:MD5.SHA1.消息摘要算法. Java已经实现了MD5.SHA1算法.利用java.security.MessageDigest类就可以获取字符串和文件的MD5以及SHA1结果. 1.字符串的MD5(下面的代码有详细注释) public static String s

JAVA md5算法代码

public class MD5 { /** * 签名字符串 * @param text 需要签名的字符串 * @param key 密钥 * @param input_charset 编码格式 * @return 签名结果 */ public static String sign(String text, String key, String input_charset) { text = text + key; return DigestUtils.md5Hex(getContentByte

md5算法的java实现

http://blog.csdn.net/forgotaboutgirl/article/details/7258109 md5算法的java实现,码迷,mamicode.com

MD5算法实现

MD5算法的简要叙述为: MD5以512位分组来处理输入的信息(512位分组?每次处理都取出512位数据?), 每一分组又被划分为16个32位子分组(16乘32刚好是512), 经过一些列的处理后(怎么处理的?),算法的输出由四个32位分组组成, 将这4个32位分组级联后将生成一个128位的散列值(那么这个散列值是最后的加密结果咯). 在MD5算法中,首先需要对信息进行填充,使其位长度对512求余的结果等于448. 因此,信息的位长度(Bits Length)将被扩展至N*512+448,即N*

常用MD5算法代码

常用的MD5算法代码日期: 2014年8月4日作者: 铁锚 MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请参考 维基百科:MD5 MD5加密后是一个字节数组, 但我们一般是取其十六进制的字符串表示法,当然,十六进制数字符串是区分大小写,在 mysql数据库,Java,和JavaScript语言中,一般是使用小写的字符串来表示, 而在 Oracle数据库官方提供的包中,返回的是大写字符串,这算是一个坑,如果你想要执行多次 md5,可能需要转换为小

java加密算法小结--MD5加密算法

上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇来整理一下一个被广泛应用的加密算法---MD5. 简单了解 MD5(Message Digest Algorithm 5),翻译过来是消息摘要算法第五版,按照惯例,我们推理可能也有MD2,MD3这样名字的历史版本.. 即使完全不了解这个算法的原理,我们也可以从命名中看出一些眉道,所谓摘要,就是一个简短的概括,像我写过的毕业论文,上来第一部分就是摘要,它对后面长篇大论的文章做了一个简短有力的概