中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: z5 j0 {5 ` E0 l4 Q#include "md5.h"
; O( }2 L. O2 l. V
* t0 L% n2 ~5 t/* Constants for MD5Transform routine.
*/
( e$ @6 u- |) \% K
1 V# W& D8 ]( u; E
7 h! c. a% |2 a5 J6 K/ b#define S11 7
( {, |8 Z9 X) P" F4 x& @4 }#define S12 12
1 E( Y* \2 V+ b& B1 E9 }2 S/ ^* u" x #define S13 17
0 H, {- g v: g. \% o- Q. g#define S14 22
1 ]1 N+ j4 U9 ^+ c: F#define S21 5
+ G) S1 v- b. p. V, v' O #define S22 9
: [' `% B3 }, G c' p6 K#define S23 14
0 E! b1 i& ]4 W: y4 D. o1 R, A #define S24 20
- \' k) t+ t' b/ A* x #define S31 4
& q) E; R5 T2 P# T/ k+ x& H: K #define S32 11
# z- F7 _5 O) i2 ?; v6 F #define S33 16
# K* ^ ]$ s. o1 H1 Z* d. B #define S34 23
$ T6 @$ y1 Y! ~7 y$ z#define S41 6
; o& x. [1 X0 j. V- c#define S42 10
, T: \/ Y, G' n) w1 g #define S43 15
* }9 d6 K9 @) @ #define S44 21
' Q, d0 v& S+ Y0 @) H
) @& O$ L9 l) p: X# i* mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ e! D9 b E: I! w. h, ~" d" o6 kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 E# e+ _% i7 m$ v8 ], W% ~8 M+ a static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' ^ o) I! [% b7 q% \static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 |6 Y1 P8 `) N0 O# q X static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) ^( n8 E3 U: N$ H
8 s* z$ B8 L1 R+ ^( v, Pstatic 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
( D. s# {+ F) t5 w};
- r R! Y" N, O
% d$ T% S9 m8 p# E. v' x /* F, G, H and I are basic MD5 functions.
*/
, W0 C8 ^; g) S6 ^) A" c#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ |6 c H* Z! E0 `1 C" S. w# Q#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" i4 i8 i9 n4 T& f #define H(x, y, z) ((x) ^ (y) ^ (z))
; `# |' I t4 B+ I( L' {4 P G #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ b7 B- w( r" v3 B7 P
. H. b: _# \, ~/ b# m- d/* ROTATE_LEFT rotates x left n bits.
*/
4 P4 j$ J+ E$ o/ x1 Z9 D" G4 I #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
G6 D1 y0 Z( v" Y! E9 j3 e
f9 V+ c$ T3 ~+ x3 U/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- h& V! o1 O# E, K2 M% _ Rotation is separate from addition to prevent recomputation.
*/
h! ~8 Z, H6 t#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 z7 f4 b) O3 I) ~7 j! ^* Y1 [ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: S/ q: h! b/ f$ ? y% s#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 E0 @( Q W9 a4 _, f3 O h# ~! d #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 h6 E4 u! G6 K4 K& ?
) f0 ]: v) w; h: _" g$ F/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- i( ~2 ~: D7 Z, m; T+ Hvoid MD5Init (context)
. q& N1 s" C( ]" b- x2 [ IMD5_CTX *context; /* context */
$ Y% s% y4 `+ n: }{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
( c* U& I/ F6 R5 N*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; v7 W9 W; H* ^: b+ q}
% @, F7 O$ w: a# s! e
/ w9 ^; c! }: ?* Q/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 y( C8 Z! y' R, e: t+ f5 Z6 F void MD5Update (context, input, inputLen)
" _- s$ H/ h: P3 `8 ]MD5_CTX *context; /* context */
+ v+ p( W4 x6 B+ T" T/ p2 | unsigned char *input; /* input block */
! j9 q2 k0 O% j8 l8 z* J. F. vunsigned int inputLen; /* length of input block */
# P) h5 p4 r$ K9 v0 H% s, I1 x {
unsigned int i, index, partLen;
. Z- w e' _; V+ o/ [+ D5 f! E, Z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 O M2 c" e% H! R
/* 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++程序


* w T3 ?2 s/ }3 C
3 V0 }# H; n6 c, f y h
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-10 05:37 , Processed in 0.088860 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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