中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( L- w# g7 }( q4 w7 { D% S& y% W#include "md5.h"
0 ~1 W2 t7 ?# M1 G0 y7 b
) O7 G5 j: M# b/* Constants for MD5Transform routine.
*/
/ O, E1 P; M+ B' G
$ R. [. M% f* d2 Z* O0 ?( a
- Q& e; D. K9 b" i6 V: H1 | #define S11 7
8 t- ~" ~/ l1 X3 Z3 \, g #define S12 12
, C4 ~- v( d* Q) P4 G2 C$ [, H( _* s #define S13 17
8 S+ h2 ^* G J#define S14 22
5 a. h" Q* f" Y5 l5 @ #define S21 5
; s) @% e' E5 u" n1 ? T6 f5 Q#define S22 9
6 ~ Y& n8 W7 \% x #define S23 14
: t1 ^: n2 V2 Q #define S24 20
+ Z; p& o l! K2 N #define S31 4
+ I. P7 {) \: h4 v8 U" v4 A6 s#define S32 11
8 H! n# z4 [$ b" [#define S33 16
8 |3 v) H$ p$ M y# F" J; b#define S34 23
3 g# i4 W: \4 E6 l {#define S41 6
& C8 d' o* M/ a2 ]3 C( z #define S42 10
& J& @. y# } O/ S) R #define S43 15
0 [3 ~- m% N& ~# F#define S44 21
4 {# I7 M8 Z9 @- R
( J2 I4 \0 Q2 u. p9 Mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 _2 J1 [- {1 h# d static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- M. \( E, F* Ystatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ C' Z7 j. b0 F) D3 ^0 z static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
( n& Y `& N' e, A. ostatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' f+ n9 A: ]0 v4 F" s- z
1 e7 ]( p9 l( D# q: I$ d; e$ A) Ustatic 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
2 e0 [* W8 q1 P" ` };
& d% F% E; M8 k% M
5 ]. v! c1 p& Q- e /* F, G, H and I are basic MD5 functions.
*/
7 ] F& D7 i& W% Y' X6 i1 Q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% T( L" a9 k9 F: q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) u, [6 |6 C" Y2 s; V" t) f' N#define H(x, y, z) ((x) ^ (y) ^ (z))
3 p: u) U6 g+ S- h3 @/ i #define I(x, y, z) ((y) ^ ((x) | (~z)))
+ o1 G! g: s" S
$ `- F( _' c2 ~% V; M /* ROTATE_LEFT rotates x left n bits.
*/
6 }6 ?# Q, ?" r0 [ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 s( R9 v- C* w6 X9 S, x
9 Q5 G3 p/ y. t! z* O: w- M /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 S) S2 J1 z0 w7 q$ _ Rotation is separate from addition to prevent recomputation.
*/
4 m- A# z! Y1 w4 I #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ @% k$ R2 L/ c' H #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 A6 g6 L8 w0 u #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) ]. }9 c m1 p/ w#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( q0 _9 l: ~$ H2 ^ D
: X1 K6 v# e2 ^' V) s7 { /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( _5 x& I* q' a( Mvoid MD5Init (context)
$ T0 E! b K. J% ]9 B' L( zMD5_CTX *context; /* context */
% x) ?- Q2 e1 Z ]% p6 P2 f{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 Y; s J# ]& B3 p2 K */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
" S/ Z7 u* d4 s$ M }
) m, R; r9 V& A g7 g+ T/ k$ {
8 c/ L j$ o8 z1 T* }& }5 P1 V+ f/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( _- \$ T6 x/ P7 B) \- o void MD5Update (context, input, inputLen)
8 Y* _+ @1 C' y' E& g# u* I6 GMD5_CTX *context; /* context */
# f: B( ?! V8 `) A% Z9 sunsigned char *input; /* input block */
4 J, }9 R0 ` m }% d t7 cunsigned int inputLen; /* length of input block */
6 p; o; o% Q1 d9 F$ d {
unsigned int i, index, partLen;
: z* Z2 K; ]) p, E6 O l
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 c) F- K" d X" T& T& v2 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++程序


( B6 B; t& o* V# Y3 L) u8 H. S
4 S; j; G' |2 l j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-23 06:03 , Processed in 0.065391 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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