中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! D7 S- K# Y. Z6 e8 w" k' c #include "md5.h"
: v) a9 w0 N+ }7 X: v0 H1 c
1 S) B( E2 ]$ F6 _3 H" c( w8 v( m /* Constants for MD5Transform routine.
*/
& g! `5 j4 G' B* l* i* z/ ?! Q
! J4 U7 d9 L+ b' ]; j5 K
& e, I9 m$ }2 o. r& V #define S11 7
# w) o% u; `8 v5 o+ v& l #define S12 12
1 {, M: s& b2 [# B5 M* d#define S13 17
* @9 C5 l' s' k1 ~' T" P m#define S14 22
2 h M. W- `( X: H; X #define S21 5
8 D2 E0 F4 [2 H1 a9 O; q/ V* [ #define S22 9
, k: H8 D: Q5 _8 a% ]4 d7 b$ S4 g+ K( S#define S23 14
2 C; ~0 G- B( c# {4 w% A5 K, @#define S24 20
6 z H9 v0 a; j7 R) f" P #define S31 4
$ V9 u$ p' t' \! @- e- W#define S32 11
, K! ~( e0 W8 z1 E5 c #define S33 16
1 a# l. n: z4 E: }" z$ [#define S34 23
% F- Q. @8 a; \3 l' f #define S41 6
7 D4 {! Z$ d+ A. b#define S42 10
& w4 ~; J/ h2 [1 G#define S43 15
2 w0 H9 a- ]" B #define S44 21
' G: E) R* H$ X6 D( }4 g: K
: j9 U f- {, L7 s1 d. k static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 m4 Q# _. r% \# B! P static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ k/ R n: I8 G' I static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 @; Q4 z" E) C4 y* J O$ T$ h static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 P. u! L& T2 h# O' X$ g5 b; s static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% k# [1 s) M! V) R
8 A: g' \' k/ w e8 istatic 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
4 H* ]$ B7 t5 M$ C7 O0 Y9 M};
) E* C4 W' Q" G! ^
, e" D2 p9 X Y; g2 }' R$ C* c/* F, G, H and I are basic MD5 functions.
*/
! \+ Z$ K i) X( |2 \3 F #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
u+ k# F" N/ _1 z#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 W7 i$ }5 M" |: ?$ ~ #define H(x, y, z) ((x) ^ (y) ^ (z))
, R8 R% k/ Z5 X#define I(x, y, z) ((y) ^ ((x) | (~z)))
' R) i5 F2 [. ]9 y# x
8 f B5 d0 i' S& r' w /* ROTATE_LEFT rotates x left n bits.
*/
@0 v% s" \: }7 M( C#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
* }2 j; W' r2 B1 _6 H0 H
9 B7 g3 T" i5 {; B% R0 J! ?/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 A% S' h' ? j F9 [, ^ Rotation is separate from addition to prevent recomputation.
*/
9 ]; n6 D9 l0 B3 D0 [#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 n$ Z8 U; `' P: z* y#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% W; D( p" v2 }6 K5 k#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: |+ O4 ^' C4 M5 _5 W7 z7 k #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 B3 h. g3 w$ I% c3 N! I* |5 D
4 j/ E/ R$ x( o* l6 B/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' Z. z5 w1 J# G% n% r0 ~3 b, R void MD5Init (context)
( ` l! g- b7 C' [ MD5_CTX *context; /* context */
, b- [8 p7 P7 G, u+ R. z0 _; Q {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) n- D5 M9 Y2 v*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. `& o& g' S1 Q6 j v}
/ d# @5 N2 F. k; b- B
6 A) M7 ^& I( \. o1 G/ ]/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; [. T: K: s# U& F0 {. d) ? void MD5Update (context, input, inputLen)
& i) ` j. N' a: ^$ a2 aMD5_CTX *context; /* context */
- C$ ^3 m+ F8 Zunsigned char *input; /* input block */
1 ^) ?6 X2 v4 o" s( munsigned int inputLen; /* length of input block */
: o6 T7 a/ h5 \{
unsigned int i, index, partLen;
+ O5 B; p' J( `/ Y- }. x! H
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! n/ L# q0 C- D, K C: k
/* 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++程序


" y) K' Q" ]9 h' P9 s8 r" \/ P- ^2 }: d
' v1 T' i1 i' ^/ i
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-8 19:50 , Processed in 0.085239 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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