中国安防论坛

 找回密码
 注册
查看: 5707|回复: 1

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' `# k" U3 n7 z2 Z+ R. y #include "md5.h"
% `: a- P" w. C ?! ]
. w4 @/ f( A9 z& C/* Constants for MD5Transform routine.
*/
) e1 [7 K! l; f/ j
4 a# [- d% F1 y: L
& l: t9 y1 L9 `#define S11 7
# Z/ l( c f# _" G6 b, c#define S12 12
: v4 ], v) V7 h7 S, d #define S13 17
; D, F# |: C$ [; k \( j #define S14 22
6 y' L1 U. X3 Q- F! I#define S21 5
0 q, i6 o/ ^- f9 p8 q2 t2 q5 l #define S22 9
4 T8 J% S0 Q. o% G( b#define S23 14
6 U1 F' t3 `- s, }1 a2 A8 k#define S24 20
1 d$ E) A! L% J7 `- j L#define S31 4
7 X0 ^; S% q, e #define S32 11
2 _# D8 Q( n* z#define S33 16
' e+ u6 X j- X1 u$ W) s8 A' S#define S34 23
2 Q' h& ^* N0 E3 L6 `/ j #define S41 6
# g3 C( G5 t9 Y0 a#define S42 10
& e6 a* x/ b. D #define S43 15
# v* N% }9 z; k6 m" {& l# Z* V8 w2 f#define S44 21
# H- Q! @8 }- V3 M
1 P: t" g) Z1 z% ?3 m/ f static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: Q. n( c3 K; M9 zstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! ?" d1 l) j" z7 \% f. Qstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
( m6 W5 @% Z/ ^. j8 R3 A8 Y1 ~static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 F& q: w( u; s; cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 T; I5 F3 T7 }: g; q
- D: z4 i2 w8 I: W 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
- e7 _: o& H) W6 t) ^& w% M+ t};
5 |3 h5 A5 h7 t) T$ Y7 l9 z
" |$ n8 n v- F T /* F, G, H and I are basic MD5 functions.
*/
: V" h; K1 f8 D& m$ U#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ D" \' r, l4 v#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+ {* P7 `1 G' O#define H(x, y, z) ((x) ^ (y) ^ (z))
5 Z% O2 Q! l% c$ F, z4 _/ B5 Z #define I(x, y, z) ((y) ^ ((x) | (~z)))
3 ^9 ^4 A; Z1 C
2 E+ D/ L, _, s/* ROTATE_LEFT rotates x left n bits.
*/
. _+ M. g. S5 G0 K#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' d1 f/ I6 x' w {$ `7 y$ L% C" g7 j
1 w; y$ R; n# f4 C" m* m! @/ [# F1 K /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
F4 {/ q/ z) k0 ~2 n% q; A Rotation is separate from addition to prevent recomputation.
*/
1 l' [" r; e3 ]" I) @4 T2 V: d5 U% C #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! N. } O0 `9 F( R#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 m+ y. t8 K! n3 R#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ c' B/ G) N! ^5 q: D1 y #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* A. D, T; k+ q, d( Q' f- s1 |, C
, h$ m1 W* E9 z3 P/ e1 S /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 d/ R: L5 I+ `4 X: C void MD5Init (context)
# M' B9 H F% w1 x$ P- }MD5_CTX *context; /* context */
" Q" w8 X& y5 B# O: p+ H8 g# } {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 i9 X8 [0 _6 m; T( |. R */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* B% Q$ @2 J4 l- w) ` }
# O6 ?- u! h# n
0 {3 n$ \$ j: o& e /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 l0 K- {' p* h0 G) x, ~void MD5Update (context, input, inputLen)
! L' U" B0 F: c j. L. zMD5_CTX *context; /* context */
J1 P5 n4 Y, u5 y$ f r+ S7 [% cunsigned char *input; /* input block */
2 c# {- ~7 r9 a& }; k f0 x# vunsigned int inputLen; /* length of input block */
0 n+ @# K- s" i2 C3 } {
unsigned int i, index, partLen;
8 l5 R$ T1 E) |/ ]* |
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! d# p6 C, u, f) ]+ D
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
&
不在競爭中變坏,就在沉默中變態

安防知名大师

探花

Rank: 7Rank: 7Rank: 7

积分
1057
QQ
发表于 2004-11-26 20:22:48 | 显示全部楼层

md5加密算法C++程序


4 E, g. z7 Q" f- Z( j
) |* s8 \; ]4 k) A$ w/ S. l
guoyun786@sohu.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

安豆网|Archiver|手机版|中国安防论坛 ( 粤ICP备09063021号 )

GMT+8, 2026-7-1 17:08 , Processed in 0.150367 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表