中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 S) R' P- k+ I #include "md5.h"
9 Y* C+ {! f" e
: f% m& Y @6 [! b+ g/* Constants for MD5Transform routine.
*/
) p# S( e" a; N
, u+ W- F! W5 q" ~0 Q
& M5 c" K. p# d: d- v #define S11 7
1 \5 _. O: j8 ^ #define S12 12
8 Z7 T4 Q3 Z* Q: w. |7 |0 X6 J7 e1 ] #define S13 17
* x% O( [3 I3 \$ z #define S14 22
6 J7 U6 S! [! t1 O& N: r) \5 }#define S21 5
4 Y+ N. e# W/ E: ~' ]: E #define S22 9
9 \2 B7 R3 r8 h8 H, \$ }. V#define S23 14
/ V1 G" a7 V' ~5 l# w; b#define S24 20
7 _" B; z2 M+ I* K2 B, P) b#define S31 4
- H ~: D5 r* B( S# A6 _% a#define S32 11
5 O! n! l/ X1 a #define S33 16
' C& L" C: d4 V: O #define S34 23
1 E% r& A+ j. F! \- c3 P' q #define S41 6
! b8 U, N& G9 P' A% P2 r#define S42 10
8 e( v- b. D' r4 Q2 ^6 l' O#define S43 15
7 y+ k; V1 G4 B8 O3 P/ W #define S44 21
" J2 r! t3 H' X6 X& P" K. _
+ M& ~6 S7 V9 `) R" Q. y! O static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! x8 [- J6 ], C. V0 b1 O9 Y5 r static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 r$ Q/ p$ B$ s static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) [: d$ [2 w! K/ Y. i/ B static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 E+ Q! k9 Q4 r static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% J p5 n7 ^* b
: ~6 i7 S) C$ S6 d3 r 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
: z7 a5 m2 h- X) |+ T};
8 }8 C8 e. a3 p+ O2 B& H4 U
r ]+ g' C2 S/ i9 q- @0 N1 Q7 h /* F, G, H and I are basic MD5 functions.
*/
' f3 H( d5 g) D6 @ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& ^3 A" L6 ^3 M* W#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 q5 I: b8 V, Q5 r9 e$ t' R: u #define H(x, y, z) ((x) ^ (y) ^ (z))
! w& ~% S& O4 K7 r #define I(x, y, z) ((y) ^ ((x) | (~z)))
0 u+ ^! o) w9 O5 ~
% U% _6 v$ p: F9 s+ v8 K /* ROTATE_LEFT rotates x left n bits.
*/
3 O8 ?5 ~* H6 d #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, B# k- }: c, A/ ~9 n
% l1 n3 f+ H0 W( l, p6 Z/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& _6 p. D# w+ L R5 D Rotation is separate from addition to prevent recomputation.
*/
% e" f+ k' F- M4 T% q#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
G% w; k' e* r5 W' s#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' Q. v: e4 F. F% Q& _' 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); \
}
7 o. G: n: n. Z% h `# j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( t6 U5 t0 R4 A, o- _) F7 d2 o8 g/ C
' z7 f% {7 \1 _( R3 F/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 f/ r$ i. F. i, ]: A' U void MD5Init (context)
2 G& e1 |" Z* ]MD5_CTX *context; /* context */
! O* [1 a: H4 J$ |1 k4 a/ Z8 x& D( }{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
Q! H3 j, Q7 x- e6 w( p# B*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 S7 U6 }3 m9 l: F3 x5 k }
2 p+ G( _( k9 K: x) g. L
. o- E( s0 C/ @5 L; n& R3 r/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 E5 z! C' I4 Z. E void MD5Update (context, input, inputLen)
6 w1 [: G# k3 w; g/ _MD5_CTX *context; /* context */
& s( E! @4 i j4 c6 g unsigned char *input; /* input block */
+ x' y7 x. u7 l8 L. m" Qunsigned int inputLen; /* length of input block */
( y* T7 S" L. o0 D6 I4 ?{
unsigned int i, index, partLen;
0 v# v `8 t8 \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 M; t5 O& c6 `
/* 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++程序


' t, {5 I. A% j% J
1 B/ Z3 z! B# o, j( w* T6 U$ q1 A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-25 14:36 , Processed in 0.079586 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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