中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' {1 X" C( B j) G* _5 K#include "md5.h"
$ D# B k* k/ i1 ?5 i N7 V& ]/ x
0 S, `$ ?* U( t* [/* Constants for MD5Transform routine.
*/
9 g4 b% V4 I7 d8 i7 x
) E: Q% H( T2 L7 _
9 f# D, i: F% v. M#define S11 7
& ?& z4 A$ ~: |/ g/ }/ d5 Y #define S12 12
3 F, Z1 A* ]8 ~# u' ?* E) u: B4 ], r#define S13 17
* L& p8 \" l, e2 D#define S14 22
( _' \6 V0 S) D6 U. b4 O#define S21 5
3 h; w; K7 ^2 S; M$ ^#define S22 9
0 W9 C7 J* z! w4 ~4 _; p; Q #define S23 14
8 a0 Q9 O3 j$ N7 a$ ] #define S24 20
# _1 Z- j7 u) [. l/ a+ ?#define S31 4
* I @; ~8 l1 L7 }: \#define S32 11
: ~$ w0 X; t! L% i- y2 u #define S33 16
6 G: r" j# d" h1 D" W0 h #define S34 23
; x u8 ]7 L" X# p1 D5 J/ H #define S41 6
) }( t/ d' ~* _1 T# c4 D ]( t#define S42 10
: `$ v( M3 w$ Q+ L, P/ r; { #define S43 15
) F; H: x2 i, N2 b #define S44 21
' L: {) R' S% i8 P$ P
3 [/ u% s7 H8 a8 q8 [( i static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 `% d2 a& Y# r8 ~$ ~static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ }' ~0 `0 r" p# S/ v) L# k, l static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
( ~4 f- T5 [; x6 { static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 b2 T, p# k4 | static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( e/ d+ M% S% Q$ ]7 N3 ?
9 H( N1 a8 U; J9 i0 g 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
6 h7 \. N+ I* [8 @8 |" Z3 E n9 T* n };
! I# P7 z9 W1 Z0 k1 i6 E* I7 a7 l
0 m/ u! I6 j. `3 c) T/* F, G, H and I are basic MD5 functions.
*/
0 B4 U2 v; Z8 g3 R#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 K1 W( J$ g. G1 N0 r1 I#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 i5 D' g, K- Q- s. t#define H(x, y, z) ((x) ^ (y) ^ (z))
( f, @! X: Y2 ?+ T4 a. E5 a) ?4 x#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 x$ c& h" D. R5 Q( k" k
6 m7 G8 `' o) l8 _) e# i /* ROTATE_LEFT rotates x left n bits.
*/
7 K# \- u$ h! C b#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ y3 N [3 l, d" {
! l& m0 \# A- o- W* Q /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; K! v- W8 C3 W$ y' _( m Rotation is separate from addition to prevent recomputation.
*/
- P8 K3 D( s. A. I5 Q# L#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. N5 J' g. q+ Q( D #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& u. z2 w; I, J8 i- \, k2 ^9 \" X#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) X. Q! j& m, i# m) s#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 Z$ o1 s4 G5 k" C8 I+ C u
2 r1 V# L' z8 P" G1 k9 s /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! q+ w( V. `2 d- }4 [% P void MD5Init (context)
1 q$ |; b" L, Q4 T$ I- w- xMD5_CTX *context; /* context */
& c3 r$ E0 D/ k' Z{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
3 x3 m2 P: Z A. n3 f9 |2 O */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
& V5 {8 c" c. f( U" z, j }
2 B k: c) D, L0 {0 a# K0 \* [$ G
, i1 e }, @1 d/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 \9 q; f0 Z5 H void MD5Update (context, input, inputLen)
g. c! K- Q3 o( U5 c) A0 p MD5_CTX *context; /* context */
& v1 M p/ V' e( L" V+ F& W unsigned char *input; /* input block */
+ ^/ i" k9 l5 Xunsigned int inputLen; /* length of input block */
5 S! ?4 E2 q6 V6 r$ R2 R( m{
unsigned int i, index, partLen;
: ~5 D8 y% I6 m" v& q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
; L9 l- g, W4 E
/* 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++程序


' L4 r$ w# x- Q6 Y! S! [; f7 w o3 W
) M& R: y W/ \9 e% f
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-13 05:01 , Processed in 0.061495 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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