中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 e& E# k1 E7 c #include "md5.h"
4 J8 W1 S1 E; c, u m
6 z( y4 ?1 @/ X" f. K+ _ /* Constants for MD5Transform routine.
*/
# }6 { y9 p, s f! Q& I+ f( x0 q8 U
9 k; [/ X) b+ b& S1 j0 s5 @+ A
8 u/ Y; ]9 n j2 y% I! b #define S11 7
1 B) a* W% w( C0 z #define S12 12
* Q3 t. r; e1 A+ v/ K$ ~! B#define S13 17
E, j4 ?0 F3 ~# d1 J #define S14 22
+ u7 _) Y3 a4 S; K, C% k2 g8 v3 S #define S21 5
& ^( i7 x1 \: p6 ?3 A9 ]4 C#define S22 9
" E8 s' Y- X# b6 A' j #define S23 14
& K9 F( b4 e) V2 x0 Y#define S24 20
+ ? T* S8 Q3 q! [#define S31 4
! x7 U# A1 x! V% b' [ #define S32 11
5 w" R( t* X& u* m7 h* u #define S33 16
9 o- _& K( _! Z- q1 _ #define S34 23
* T/ h6 R4 J. x' |. I #define S41 6
+ S$ P8 A9 p, J" Y1 ~' [6 h#define S42 10
& |( D: J0 g, |; e. X9 B/ C#define S43 15
* |4 N7 W! C/ S# U#define S44 21
0 z4 o+ a; D! i& H' g i
' b9 Y* O. b# S" c5 O3 Zstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* L; ~" j" a! c# w" B) _' mstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' s; H' o) g7 @ {2 F' i static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 P; R1 l& N- L2 m) Z1 lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% j1 ?' L$ m0 W6 T& sstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) x! x% p5 y- J7 J Z
& y; E. J+ h( o0 a; o H( cstatic 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 a4 F- b9 y, Y5 `+ ~3 k};
8 S, v% ?5 [7 w" e* ?6 l
7 z% {/ S) J5 L1 E5 K /* F, G, H and I are basic MD5 functions.
*/
: _) w: L7 H9 q0 k0 W" N: {; ] #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% ~' L# a$ e3 I9 t8 v* D#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; x% ^: b- v [ #define H(x, y, z) ((x) ^ (y) ^ (z))
& h4 O! o2 I8 R Y3 a#define I(x, y, z) ((y) ^ ((x) | (~z)))
5 x3 z! E$ I0 ~ ^
, C5 r) N! O6 e9 ~8 V/* ROTATE_LEFT rotates x left n bits.
*/
& m+ u6 \. O+ B) W: \0 ?3 t #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. y2 p6 k5 g" U9 p1 _
0 g. Q/ J2 n" K: b/ {& c /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ p; j! ^* f, BRotation is separate from addition to prevent recomputation.
*/
Y4 p' F3 i; _# N1 m#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 G q, M8 B# H$ E* I/ e#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; u8 t8 A1 Z. i) z #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 c r; m) n- O0 | #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
c* Y& E( |9 k& ?1 S
0 w7 m, w1 B2 Y' M/ a- ^) O% C% H* _/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) D5 c; C4 q, Uvoid MD5Init (context)
7 t0 D$ P t* k MD5_CTX *context; /* context */
5 L: q7 A# d8 F1 Q8 J, m {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& d2 o& |# }5 u* Y { */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 w4 k9 @5 H9 B9 ^* ^' c1 B( v}
+ O' w0 Y {# @' W; ]3 u
3 t3 W0 P- F) q# t' I% z/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 l% y; }9 `/ u2 l/ j1 Y% J7 k3 k void MD5Update (context, input, inputLen)
) B. i+ Y m$ O3 c5 L2 }) l9 G" h0 bMD5_CTX *context; /* context */
; h+ x0 d; M; x I5 E" Wunsigned char *input; /* input block */
2 T1 {- p; E% ~" n5 Bunsigned int inputLen; /* length of input block */
# _( T' A4 I5 o* T, k& w1 u {
unsigned int i, index, partLen;
; `) h6 g0 v" @- {! x% d8 E
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, @0 L$ T0 [. C5 N8 x
/* 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++程序


0 J m S7 C; Y0 n! W7 Q( F+ J S; l
e) d/ Q& r! f2 t! s
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-5 13:26 , Processed in 0.221568 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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