环境:vc2003

.h

 1 /* MD5.H - header file for MD5C.C
 2  */
 3
 4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
 5 rights reserved.
 6
 7 License to copy and use this software is granted provided that it
 8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
 9 Algorithm" in all material mentioning or referencing this software
10 or this function.
11
12 License is also granted to make and use derivative works provided
13 that such works are identified as "derived from the RSA Data
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
15 mentioning or referencing the derived work.
16
17 RSA Data Security, Inc. makes no representations concerning either
18 the merchantability of this software or the suitability of this
19 software for any particular purpose. It is provided "as is"
20 without express or implied warranty of any kind.
21
22 These notices must be retained in any copies of any part of this
23 documentation and/or software.
24  */
25
26 #ifndef MD5_SECRIT_H__
27 #define MD5_SECRIT_H__
28
29 namespace SECRIET
30 {
31     /* MD5 context. */
32     typedef unsigned int uint32_t;
33     typedef struct {
34     uint32_t state[4];            /* state (ABCD) */
35     uint32_t count[2];            /* number of bits, modulo 2^64 (lsb first) */
36     unsigned char buffer[64];     /* input buffer */
37     } MD5_CTX;
38
39     void MD5Init(MD5_CTX *context);
40     void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen);
41     void MD5Final(unsigned char digest[16], MD5_CTX *context);
42
43     class CMD5
44     {
45     public:
46         MD5_CTX _ctxMd5;
47
48     public:
49         CMD5() {MD5Init(&_ctxMd5);}
50         ~CMD5() {}
51
52     public:
53         void Encode( unsigned char *input, unsigned int inputLen, unsigned char digest[16] )
54         {
55             MD5Update( &_ctxMd5, input, inputLen );
56             MD5Final( digest, &_ctxMd5);
57         }
58
59     };
60 }
61 #endif

.cpp

  1 /*
  2  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  3  */
  4
  5 /*
  6  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
  7  * reserved.
  8  *
  9  * License to copy and use this software is granted provided that it is
 10  * identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm"
 11  * in all material mentioning or referencing this software or this function.
 12  *
 13  * License is also granted to make and use derivative works provided that such
 14  * works are identified as "derived from the RSA Data Security, Inc. MD5
 15  * Message-Digest Algorithm" in all material mentioning or referencing the
 16  * derived work.
 17  *
 18  * RSA Data Security, Inc. makes no representations concerning either the
 19  * merchantability of this software or the suitability of this software for
 20  * any particular purpose. It is provided "as is" without express or implied
 21  * warranty of any kind.
 22  *
 23  * These notices must be retained in any copies of any part of this
 24  * documentation and/or software.
 25  */
 26
 27 #include "Stdafx.h"
 28 #include "md5.h"
 29
 30 /*
 31  * Constants for MD5Transform routine.
 32  */
 33 #define S11 7
 34 #define S12 12
 35 #define S13 17
 36 #define S14 22
 37 #define S21 5
 38 #define S22 9
 39 #define S23 14
 40 #define S24 20
 41 #define S31 4
 42 #define S32 11
 43 #define S33 16
 44 #define S34 23
 45 #define S41 6
 46 #define S42 10
 47 #define S43 15
 48 #define S44 21
 49
 50 namespace SECRIET
 51 {
 52
 53     static unsigned char PADDING[64] = {
 54         0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 55         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 56         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 57     };
 58
 59     /*
 60     * F, G, H and I are basic MD5 functions.
 61     */
 62     #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
 63     #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
 64     #define H(x, y, z) ((x) ^ (y) ^ (z))
 65     #define I(x, y, z) ((y) ^ ((x) | (~z)))
 66
 67     /*
 68     * ROTATE_LEFT rotates x left n bits.
 69     */
 70     #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
 71
 72     /*
 73     * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is
 74     * separate from addition to prevent recomputation.
 75     */
 76     #define FF(a, b, c, d, x, s, ac) { \
 77     (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
 78     (a) = ROTATE_LEFT ((a), (s)); \
 79     (a) += (b); \
 80     }
 81     #define GG(a, b, c, d, x, s, ac) { \
 82     (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
 83     (a) = ROTATE_LEFT ((a), (s)); \
 84     (a) += (b); \
 85     }
 86     #define HH(a, b, c, d, x, s, ac) { \
 87     (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
 88     (a) = ROTATE_LEFT ((a), (s)); \
 89     (a) += (b); \
 90     }
 91     #define II(a, b, c, d, x, s, ac) { \
 92     (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
 93     (a) = ROTATE_LEFT ((a), (s)); \
 94     (a) += (b); \
 95     }
 96
 97     /*
 98     * Encodes input (uint32_t) into output (unsigned char). Assumes len is a
 99     * multiple of 4.
100     */
101     static void
102     Encode(unsigned char *output, uint32_t * input, unsigned int len)
103     {
104         unsigned int    i, j;
105
106 //        ASSERT((len % 4) == 0);
107
108         for (i = 0, j = 0; j < len; i++, j += 4) {
109             output[j] = (unsigned char) (input[i] & 0xff);
110             output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
111             output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
112             output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
113         }
114     }
115
116     /*
117     * Decodes input (unsigned char) into output (uint32_t). Assumes len is a
118     * multiple of 4.
119     */
120     static void
121     Decode(uint32_t * output, unsigned char *input, unsigned int len)
122     {
123         unsigned int    i, j;
124
125         for (i = 0, j = 0; j < len; i++, j += 4)
126             output[i] = ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
127                 (((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
128     }
129
130     /*
131     * MD5 basic transformation. Transforms state based on block.
132     */
133     static void
134     MD5Transform(uint32_t state[4], unsigned char block[64])
135     {
136         uint32_t           a = state[0], b = state[1], c = state[2], d = state[3],
137                         x[16];
138
139         Decode(x, block, 64);
140
141         /* Round 1 */
142         FF(a, b, c, d, x[0], S11, 0xd76aa478);    /* 1 */
143         FF(d, a, b, c, x[1], S12, 0xe8c7b756);    /* 2 */
144         FF(c, d, a, b, x[2], S13, 0x242070db);    /* 3 */
145         FF(b, c, d, a, x[3], S14, 0xc1bdceee);    /* 4 */
146         FF(a, b, c, d, x[4], S11, 0xf57c0faf);    /* 5 */
147         FF(d, a, b, c, x[5], S12, 0x4787c62a);    /* 6 */
148         FF(c, d, a, b, x[6], S13, 0xa8304613);    /* 7 */
149         FF(b, c, d, a, x[7], S14, 0xfd469501);    /* 8 */
150         FF(a, b, c, d, x[8], S11, 0x698098d8);    /* 9 */
151         FF(d, a, b, c, x[9], S12, 0x8b44f7af);    /* 10 */
152         FF(c, d, a, b, x[10], S13, 0xffff5bb1);    /* 11 */
153         FF(b, c, d, a, x[11], S14, 0x895cd7be);    /* 12 */
154         FF(a, b, c, d, x[12], S11, 0x6b901122);    /* 13 */
155         FF(d, a, b, c, x[13], S12, 0xfd987193);    /* 14 */
156         FF(c, d, a, b, x[14], S13, 0xa679438e);    /* 15 */
157         FF(b, c, d, a, x[15], S14, 0x49b40821);    /* 16 */
158
159         /* Round 2 */
160         GG(a, b, c, d, x[1], S21, 0xf61e2562);    /* 17 */
161         GG(d, a, b, c, x[6], S22, 0xc040b340);    /* 18 */
162         GG(c, d, a, b, x[11], S23, 0x265e5a51);    /* 19 */
163         GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);    /* 20 */
164         GG(a, b, c, d, x[5], S21, 0xd62f105d);    /* 21 */
165         GG(d, a, b, c, x[10], S22, 0x2441453);    /* 22 */
166         GG(c, d, a, b, x[15], S23, 0xd8a1e681);    /* 23 */
167         GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);    /* 24 */
168         GG(a, b, c, d, x[9], S21, 0x21e1cde6);    /* 25 */
169         GG(d, a, b, c, x[14], S22, 0xc33707d6);    /* 26 */
170         GG(c, d, a, b, x[3], S23, 0xf4d50d87);    /* 27 */
171         GG(b, c, d, a, x[8], S24, 0x455a14ed);    /* 28 */
172         GG(a, b, c, d, x[13], S21, 0xa9e3e905);    /* 29 */
173         GG(d, a, b, c, x[2], S22, 0xfcefa3f8);    /* 30 */
174         GG(c, d, a, b, x[7], S23, 0x676f02d9);    /* 31 */
175         GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);    /* 32 */
176
177         /* Round 3 */
178         HH(a, b, c, d, x[5], S31, 0xfffa3942);    /* 33 */
179         HH(d, a, b, c, x[8], S32, 0x8771f681);    /* 34 */
180         HH(c, d, a, b, x[11], S33, 0x6d9d6122);    /* 35 */
181         HH(b, c, d, a, x[14], S34, 0xfde5380c);    /* 36 */
182         HH(a, b, c, d, x[1], S31, 0xa4beea44);    /* 37 */
183         HH(d, a, b, c, x[4], S32, 0x4bdecfa9);    /* 38 */
184         HH(c, d, a, b, x[7], S33, 0xf6bb4b60);    /* 39 */
185         HH(b, c, d, a, x[10], S34, 0xbebfbc70);    /* 40 */
186         HH(a, b, c, d, x[13], S31, 0x289b7ec6);    /* 41 */
187         HH(d, a, b, c, x[0], S32, 0xeaa127fa);    /* 42 */
188         HH(c, d, a, b, x[3], S33, 0xd4ef3085);    /* 43 */
189         HH(b, c, d, a, x[6], S34, 0x4881d05);    /* 44 */
190         HH(a, b, c, d, x[9], S31, 0xd9d4d039);    /* 45 */
191         HH(d, a, b, c, x[12], S32, 0xe6db99e5);    /* 46 */
192         HH(c, d, a, b, x[15], S33, 0x1fa27cf8);    /* 47 */
193         HH(b, c, d, a, x[2], S34, 0xc4ac5665);    /* 48 */
194
195         /* Round 4 */
196         II(a, b, c, d, x[0], S41, 0xf4292244);    /* 49 */
197         II(d, a, b, c, x[7], S42, 0x432aff97);    /* 50 */
198         II(c, d, a, b, x[14], S43, 0xab9423a7);    /* 51 */
199         II(b, c, d, a, x[5], S44, 0xfc93a039);    /* 52 */
200         II(a, b, c, d, x[12], S41, 0x655b59c3);    /* 53 */
201         II(d, a, b, c, x[3], S42, 0x8f0ccc92);    /* 54 */
202         II(c, d, a, b, x[10], S43, 0xffeff47d);    /* 55 */
203         II(b, c, d, a, x[1], S44, 0x85845dd1);    /* 56 */
204         II(a, b, c, d, x[8], S41, 0x6fa87e4f);    /* 57 */
205         II(d, a, b, c, x[15], S42, 0xfe2ce6e0);    /* 58 */
206         II(c, d, a, b, x[6], S43, 0xa3014314);    /* 59 */
207         II(b, c, d, a, x[13], S44, 0x4e0811a1);    /* 60 */
208         II(a, b, c, d, x[4], S41, 0xf7537e82);    /* 61 */
209         II(d, a, b, c, x[11], S42, 0xbd3af235);    /* 62 */
210         II(c, d, a, b, x[2], S43, 0x2ad7d2bb);    /* 63 */
211         II(b, c, d, a, x[9], S44, 0xeb86d391);    /* 64 */
212
213         state[0] += a;
214         state[1] += b;
215         state[2] += c;
216         state[3] += d;
217
218         /*
219         * Zeroize sensitive information.
220         */
221         memset((unsigned char *) x, 0, sizeof(x));
222     }
223
224     /**
225     * MD5Init:
226     * @context: MD5 context to be initialized.
227     *
228     * Initializes MD5 context for the start of message digest computation.
229     **/
230     void
231     MD5Init(MD5_CTX * context)
232     {
233         context->count[0] = context->count[1] = 0;
234         /* Load magic initialization constants.  */
235         context->state[0] = 0x67452301;
236         context->state[1] = 0xefcdab89;
237         context->state[2] = 0x98badcfe;
238         context->state[3] = 0x10325476;
239     }
240
241     /**
242     * MD5Update:
243     * @context: MD5 context to be updated.
244     * @input: pointer to data to be fed into MD5 algorithm.
245     * @inputLen: size of @input data in bytes.
246     *
247     * MD5 block update operation. Continues an MD5 message-digest operation,
248     * processing another message block, and updating the context.
249     **/
250
251     void
252     MD5Update(MD5_CTX * context, unsigned char *input, unsigned int inputLen)
253     {
254         unsigned int    i, index, partLen;
255
256         /* Compute number of bytes mod 64 */
257         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
258
259         /* Update number of bits */
260         if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
261             context->count[1]++;
262         }
263         context->count[1] += ((uint32_t) inputLen >> 29);
264
265         partLen = 64 - index;
266
267         /* Transform as many times as possible.  */
268         if (inputLen >= partLen) {
269             memcpy((unsigned char *) & context->buffer[index], (unsigned char *) input, partLen);
270             MD5Transform(context->state, context->buffer);
271
272             for (i = partLen; i + 63 < inputLen; i += 64) {
273                 MD5Transform(context->state, &input[i]);
274             }
275             index = 0;
276         } else {
277             i = 0;
278         }
279         /* Buffer remaining input */
280         if ((inputLen - i) != 0) {
281             memcpy((unsigned char *) & context->buffer[index], (unsigned char *) & input[i], inputLen - i);
282         }
283     }
284
285     /**
286     * MD5Final:
287     * @digest: 16-byte buffer to write MD5 checksum.
288     * @context: MD5 context to be finalized.
289     *
290     * Ends an MD5 message-digest operation, writing the the message
291     * digest and zeroing the context.  The context must be initialized
292     * with MD5Init() before being used for other MD5 checksum calculations.
293     **/
294
295     void
296     MD5Final(unsigned char digest[16], MD5_CTX * context)
297     {
298         unsigned char   bits[8];
299         unsigned int    index, padLen;
300
301         /* Save number of bits */
302         Encode(bits, context->count, 8);
303
304         /*
305         * Pad out to 56 mod 64.
306         */
307         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
308         padLen = (index < 56) ? (56 - index) : (120 - index);
309         MD5Update(context, PADDING, padLen);
310
311         /* Append length (before padding) */
312         MD5Update(context, bits, 8);
313         /* Store state in digest */
314         Encode(digest, context->state, 16);
315
316         /*
317         * Zeroize sensitive information.
318         */
319         memset((unsigned char *) context, 0, sizeof(*context));
320     }
321
322 }

转载于:https://www.cnblogs.com/CPYER/p/3276926.html

Encryption-基础:MD5加密相关推荐

  1. ios android md5加密,iOS中使用MD5加密

    在iOS中使用MD5加密较简单,需要引入头文件CommonCrypto/CommonDigest.h,我们单独新建一个用于MD5加密的类Encryption,此类继承NSObject. Encrypt ...

  2. 《MySQL》入门基础知识点大全:数据库操作、增删改查、联表查询、常用函数、MD5加密、事务特性、隔离级别

    MySQL基础知识大全 1.操作数据库 1.1 创建表 1.2 修改表名 1.3 增加表的字段 1.4 修改表的字段 1.4.1 修改表的字段 1.4.2 修改表名 1.5 删除表的字段 1.6 删除 ...

  3. JavaScript中md5加密基础使用方法

    关于JavaScript中md5加密使用方法重点 不说别的,先上代码,代码其实不难,更多需要理解 //这边是前端代码 <form class="loginForm" acti ...

  4. 【Java基础】加密与安全基础

    文章目录 一.编码算法 什么是编码? URL编码 1.什么是URL编码算法 2.Java中使用URL编码算法 Base64编码 1.什么是Base64编码算法 2.Java中使用Base64编码算法 ...

  5. JMeter-Eclipse添加自定义函数 MD5加密 32位和16位

    最近公司的接口都是MD5  16位加密,所以要使用加密功能. 之前也做过加密,因为用的比较少,所以是写了一个加密方法,导出JAR包,调用的.用起来需要很多设置,并且换算效率也不高.听前同事说,jmet ...

  6. python凯撒密码加密写入文件_Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作...

    #-*- coding: gb2312 -*-#用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作#作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/k ...

  7. java md5 32位加密算法_java 32位md5加密类

    下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. import java.security.MessageDigest; import ja ...

  8. SQL2000 MD5加密

    原文:SQL2000 MD5加密 /***************************************************************************** * Na ...

  9. python使用md5加密_如何使用Python构建加密机器人并将其连接到Facebook Messenger

    python使用md5加密 by Paul Pinard 保罗·皮纳德(Paul Pinard) 认识Sato the Cryptobot,他能够从外部API获取任何加密货币的价格! (Meet Sa ...

  10. md5加密工具类_贼好用的 Java 工具类库! GitHub 星标 10k+,你在用吗?

    来源:ryanc.cc/archives/hutool-java-tools-lib 简介 Hutool是Hu + tool的自造词,前者致敬我的"前任公司",后者为工具之意,谐音 ...

最新文章

  1. pyqt5 输入确认_对PyQt5的输入对话框使用(QInputDialog)详解
  2. java面试必背知识点
  3. mysql insert报错_mysql数据库使用insert语句插入中文数据报错
  4. 读取一个文件中的字符,统计每个字符出现的次数
  5. php文件上传指定路径,php上传文件到指定文件夹
  6. 小学生图片_中秋节手抄报,小学生中秋节手抄报图片大全
  7. java实现线程的方式_java多线程实现的四种方式
  8. python读取print输出的内容_Python文件中将print的输出内容重定向到变量中
  9. 已知原函数和导函数的关系_原函数与导函数的关系
  10. CMakeLists编译
  11. win7关闭UAC的方法
  12. sRGB Color Space
  13. 迷宫算法(JAVA实现)
  14. android 指南针不稳定,Android指南针方向不可靠(低通滤波器)
  15. 大学物理实验 基本量的测量
  16. 实体零售纷纷转型,苏宁、乐语到底能给我们带来哪些启示?
  17. 目录大全_安卓面经_Android面经_150道安卓基础面试题全解析
  18. 搭建Nginx-rtmp流媒体服务器+使用ffmpeg推流
  19. Java笔记整理六(File类,递归,字节流IO,字符流IO,流中的异常处理,属性集Properties,缓冲流,转换流,序列化,打印流)
  20. linux学习笔记2.0

热门文章

  1. java调用视图如何传参_Spring MVC中 视图 向 控制器 传参(接收方式)
  2. 解决MySQL新建表时Row size too large (> 8126). Changing some columns to TEXT or BLOB may help.
  3. 5.18 综合案例2.0-家庭入侵报警系统(仅支持2.2以上版本)
  4. 焊接件技术要求怎么写_钣金焊接规范及要求
  5. 5-11~5-12成都29所,10所参观记录
  6. 一些常用APP包名汇总
  7. PIC单片机RC振荡器的使用及校准方法
  8. android 小人,Android 仿360桌面小人
  9. leetcode系列--17.电话号码的字母组合
  10. leetcode系列--69.Sqrt(x)