中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 l( L/ t9 R$ r1 h$ d#include "md5.h"
* |" H: Y# g. O) p6 v+ R
; s1 s+ }; O5 V" R- g0 j0 V+ |0 _$ Q/* Constants for MD5Transform routine.
*/
; S3 }" x% R0 b) y
x1 ]( q+ p. a' O! w
9 n2 B; ~+ u( S$ n* G7 J) h#define S11 7
) M0 h: G6 q2 o8 L$ ~8 j#define S12 12
5 f: u* k! B' }- j0 g$ } #define S13 17
/ Q( r7 b/ q7 E6 } #define S14 22
: N* d5 G' z. K5 S. F! W#define S21 5
7 ] ?1 ?( F0 J3 ], V0 } #define S22 9
% g: w4 z R2 W$ L( s#define S23 14
0 t" j' M( `: g" _, v/ p( b {) t# u#define S24 20
1 _/ G3 F8 R# S; i$ ?1 I0 P #define S31 4
( R; w$ K8 u: N# l7 g) Q, L7 ^: R! V#define S32 11
9 T/ j4 Z `# \1 S# e #define S33 16
7 a8 Y6 q: V2 i3 ?) j& W+ d#define S34 23
) L- i. K- S( |! S0 `* z- Z#define S41 6
* }% T8 c% @% N#define S42 10
4 A: h8 M/ s8 C3 T u #define S43 15
4 w" _* C7 @ _$ i #define S44 21
# z) F5 _( j3 ^- v
3 d* V& i# Z0 ?. i2 l9 w" `+ p9 z1 Wstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- W6 M1 K9 Q7 m# C static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
9 ~4 y& M: C) p3 l& E4 x static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* A6 s8 W6 [: U/ q Y: ] static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 U7 s0 k4 k$ s8 x. W) { static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& J" k! z- N/ |
5 M1 D }- C+ Q1 @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
' K4 M% |; W+ }& I- R3 H' g+ ]0 f; X};
! w% R+ H' C, D6 M1 _; Z0 ^
: U0 r6 n) p6 h- W' T, `+ Y /* F, G, H and I are basic MD5 functions.
*/
- R4 `% j# t7 C* P #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ Q; D+ {) }# W6 ?7 s. k #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
0 F& k2 a9 e! E" U( e4 N #define H(x, y, z) ((x) ^ (y) ^ (z))
& J1 }. M' g( D) ~ #define I(x, y, z) ((y) ^ ((x) | (~z)))
d+ A0 c& p' t3 G9 o
* n; ~# ~1 d z) | /* ROTATE_LEFT rotates x left n bits.
*/
8 ]6 T6 y9 T9 Z% v2 {( w3 j$ }#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, I, D* W4 w9 G+ r8 F! W/ K
. u/ x; J* P1 J0 Q+ K9 W0 W/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 N* m/ s+ T% r8 i4 X( m# C9 f Rotation is separate from addition to prevent recomputation.
*/
9 \: v. F( s+ z" Z0 D( K' q#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, A# m, |) B! t% Z& H8 C 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); \
}
) Y4 o( \0 A$ }6 I: a+ [2 } #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( S9 O0 [* X9 T #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 J- A# B/ j1 }
" j( x" n8 N: ^3 I /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, j) y s, j9 s' `. o6 k- h, y void MD5Init (context)
' s4 y: _$ C, ~( w) P4 ?$ w4 |MD5_CTX *context; /* context */
; S8 a0 ^2 }3 J5 T) D1 ?9 S" F{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 a) E, v4 _" g4 `*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 ^5 r/ T. _/ d3 U8 y}
3 u- Y' y% O( D D: X. h% T
: j) v" V7 e% t+ g' ~' e /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
9 m+ r& P$ X9 z* j+ uvoid MD5Update (context, input, inputLen)
, X |) Z- h7 J* p% B( E8 q% Y) oMD5_CTX *context; /* context */
* K0 L, N% V4 F# G unsigned char *input; /* input block */
$ ]& _. S' D# G8 c2 _% h6 U+ o. N unsigned int inputLen; /* length of input block */
+ C- a- S9 ]# s9 `4 [ {
unsigned int i, index, partLen;
; q. c+ p4 B# a8 ~/ Q( D
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 Q3 [7 s0 T" n( S4 s
/* 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++程序


; Z, \/ u J* F% m" ^( H
5 O% [& Q9 ]( ^6 ^6 b
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-2 19:16 , Processed in 0.060313 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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