中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ g2 E; A6 ] v1 D #include "md5.h"
* r4 U1 j3 ~- m, [5 ~: n7 t" ^& m
/ Y' ~- y2 k s- u1 s! B. ? /* Constants for MD5Transform routine.
*/
) B1 O; i H) R/ h- t/ P$ P
+ N. {0 ^. O2 |& Y8 w* b: o5 G$ D
1 p/ Z1 K3 O5 F1 c#define S11 7
& J6 o4 Q+ L# \' M! r) H. J0 L #define S12 12
1 ]+ @, r; [, | F #define S13 17
5 _# C. ~1 C' w: g8 j2 K5 ?( } #define S14 22
8 \% L9 A, V$ |- ~ #define S21 5
# H7 L# n# u* @" b6 X1 t#define S22 9
* s4 p( l* p8 @9 H. W' i9 Q#define S23 14
, y; z- S( a/ [+ {" r3 p& k #define S24 20
1 | v: w+ a( C #define S31 4
6 q1 E: C$ c- C& D0 ^" c4 [& T* J #define S32 11
6 S: w; A# [3 k #define S33 16
, r. a- W4 s- ?4 O#define S34 23
4 _& x! D7 S1 ]6 X" Z- d) T4 G' p) S& v #define S41 6
: \* i4 D7 S, P( n#define S42 10
4 X9 S4 S% y% M4 h/ l# E#define S43 15
, _2 }9 J3 S. N#define S44 21
/ K! O& _0 \, {$ Z$ e5 X, H8 g# ^
8 s& o- @7 [+ a6 rstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 ?3 ]! V, [1 C- I' p7 s/ O/ qstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
0 ^. I n2 K4 f, Q' ^+ B static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: K/ x. I! C2 X! T; E8 \ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! j3 T# l7 D8 ^static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* a; X# V" s; Y$ A% U& R+ v* {. {& U
9 \* k7 c) [! [. r! h) L) }! \static 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
1 q/ O, h( D$ @6 P6 c};
9 D4 ?& n9 z% `
3 Y& D' d. i( z /* F, G, H and I are basic MD5 functions.
*/
: ?1 @! ]- q) Z8 u( i+ @1 @, K$ }#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 C0 v" `0 F& n #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 s7 S* z" W+ u( u* q' j+ y#define H(x, y, z) ((x) ^ (y) ^ (z))
2 ^. ^" N4 ~& S3 T* a& K% {#define I(x, y, z) ((y) ^ ((x) | (~z)))
( y8 [) R, \6 v4 [; \! g) z5 C
9 y4 G" p6 ~% m& J6 Q, y" V" O, c/* ROTATE_LEFT rotates x left n bits.
*/
7 a4 s" l7 W: c9 M7 `1 [ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! Z2 V7 L& ^- @9 A/ I3 M
& H) P) \5 X: J! \, ?" q1 X/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ y" ]6 f5 }8 ARotation is separate from addition to prevent recomputation.
*/
% r* X& I7 {' V4 X#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' O# f" q i6 V" m#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" f8 e/ b* S7 {9 h" b7 N& r #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 h% X% p- M# O #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 U! w2 _" x1 D# H% {2 V
4 E+ N5 F% W6 z% l- e9 P/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
0 ~0 C' u7 V" h! P; Xvoid MD5Init (context)
# C [" B9 D; v1 s MD5_CTX *context; /* context */
: G3 U4 I F( G+ I0 } {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ ~0 z" T( s# h' `" k */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 X0 _# @# T' r; D) y}
+ y" Z- ^ T5 J) n8 |+ Z8 p2 z
! |9 ]9 l5 _! R8 F /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
0 F/ C( }: q# ^! E7 ivoid MD5Update (context, input, inputLen)
1 S6 m, L: C% o5 R7 @5 GMD5_CTX *context; /* context */
+ t, \- u7 t3 O4 I# v3 Cunsigned char *input; /* input block */
4 `7 w$ B! r& F; {unsigned int inputLen; /* length of input block */
0 H5 x* R3 u4 i# f# h {
unsigned int i, index, partLen;
O$ q* }( ^& m0 Z1 P5 c9 [2 B
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, R6 Y; F w/ a% ?: p/ ]) g$ r, N9 P
/* 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++程序


8 ]/ g) C( ?$ i5 U% n2 M% h
7 P9 N8 g9 `/ s0 x. \
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-19 06:30 , Processed in 0.056013 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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