中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 p! q: H8 T* w5 A#include "md5.h"
7 M. Y4 L- [# Y4 p) R5 `
# }( T3 X. m# l: ?1 L+ i3 F% ~/* Constants for MD5Transform routine.
*/
+ p. U, v' ~6 _, v' }6 O
% N* e% U5 z& U# W# ^8 T$ ~* T6 b5 W! [8 P
+ z# V" n; q* z& L( z. i5 k' @3 Y#define S11 7
/ D8 z. }. F/ Z. a) Z; H#define S12 12
* ?; e4 ^7 P# Y9 s2 G1 W2 h #define S13 17
h2 m3 j* X4 s- H #define S14 22
% C* T) G" d: z. g#define S21 5
# u! W! L' M% _3 T5 r, Y; O #define S22 9
: }9 H- z2 b* h$ s #define S23 14
3 Q5 j+ G+ \% Z; B#define S24 20
. i# n! ]/ _& {#define S31 4
& l4 b( U" Y n. q2 E X6 e #define S32 11
# a4 l7 @8 ^. T! N9 u# j6 E0 P #define S33 16
) t/ b n: i% e$ o; I#define S34 23
4 }3 f/ c1 `/ X5 O #define S41 6
& [( O- Z. U( n- h#define S42 10
7 ?9 ]0 x! ` C( p#define S43 15
1 }4 Z# c$ A( D0 M+ ^; m3 I #define S44 21
" m. z/ X" k' z ~4 ^
j3 X6 z# @' q0 w7 n5 O static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 e0 J1 N5 p. ~* |: U( b% T+ p. ^2 X% Pstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' c5 `& i+ ?4 |& { static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 m9 T( \/ J# B6 p$ } ?static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 t% t* ]+ U$ `% W! P+ F7 _static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& @+ A/ U+ N3 b6 { ?6 Y) a
1 l+ Y, ^* i9 E o4 `2 U7 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
/ t$ X$ D' o( ^! i1 g9 g, i};
! j) x E" L- J- ~
0 }) Z5 F, p2 r; q5 [ /* F, G, H and I are basic MD5 functions.
*/
% _; {9 d' T. b9 J, M* r#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" P7 z) \) t1 |! r #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 N2 ?* |+ x1 S9 [5 R0 y+ _ #define H(x, y, z) ((x) ^ (y) ^ (z))
) F8 v: z s$ w/ O o#define I(x, y, z) ((y) ^ ((x) | (~z)))
% P3 R" O0 E/ ^* d. b9 h+ T; A2 q
n7 H$ R; _ x+ |+ v/* ROTATE_LEFT rotates x left n bits.
*/
7 ~/ n0 \+ C; q& I! \& i#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ _6 J S6 d( ?1 }# j0 P
$ p; b/ m4 E4 o; T" ^ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
2 l: Z: r* `% f! u/ f Rotation is separate from addition to prevent recomputation.
*/
3 P! E, F$ 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); \
}
E1 P2 l9 d% U3 @; t9 i; n#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ H6 x0 e6 @5 \/ Z6 {8 x# ^- D #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 ~8 Z% R* L/ V1 }6 g+ X#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 p9 g. ~6 P7 ]+ ~9 o
5 _+ |1 ]2 C" T+ m; ?! R! W; `/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: A1 t1 o. G, `9 q/ S* H void MD5Init (context)
0 u3 l. |* O4 k+ j* j4 jMD5_CTX *context; /* context */
& d7 K( P3 G1 I3 F: t{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 _. T( e/ n. h) ?( U7 c- l: @8 r */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
E' M1 C' E1 w+ H, _}
/ `* T" `& j; w# i# ], I3 Y! Y/ Y
: h. y. Q/ c# Y% |/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& Y- ~9 g6 s4 r- ~2 p9 n# x i( Q void MD5Update (context, input, inputLen)
) E! q. t5 G* d. M# b5 h2 j/ S, fMD5_CTX *context; /* context */
9 |* M7 }. C# d) a/ B3 W/ \4 _unsigned char *input; /* input block */
7 w/ B1 x; e$ s% N; V unsigned int inputLen; /* length of input block */
' }% \+ N/ B9 ~& ]; } {
unsigned int i, index, partLen;
; \) ^. }( e; K; f1 z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& t9 M" ]) L0 v9 ^, `* V
/* 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++程序


7 j; |0 z- V. ~( H: X6 W
, s3 Z9 @$ V: M9 q7 J3 E( h3 i
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-25 17:11 , Processed in 0.116353 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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