中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& @! k! F7 _8 L9 Z3 S" v #include "md5.h"
( z; J* k6 M+ X5 l- W7 Y
& u3 W: P6 h% |. u /* Constants for MD5Transform routine.
*/
) W8 T; {( v! [3 x j( h
: h( Y. ?1 ?7 d
h2 `9 ]& f3 p- h' J, \6 T9 \ #define S11 7
2 u, r1 \4 v6 h) X u( W# j, Y #define S12 12
- y8 M& z" ^4 p* N3 a#define S13 17
6 H: }- m8 l* P2 f+ L#define S14 22
; v& Y5 |. R) Q. d9 ~ #define S21 5
* T+ r# Q) |. ^2 f5 u: z- U #define S22 9
( u+ A1 O* r5 b, _' N! r4 O- F #define S23 14
" I6 o/ J4 x' m; Y5 ~$ k. a% T8 S3 k#define S24 20
) t, a6 |8 K9 t$ Z3 \! E9 C; U#define S31 4
G4 g( x j6 b" `! @7 P$ Z+ z #define S32 11
* E% d! h6 S: [3 F$ u#define S33 16
2 x" u4 v! u% C$ m7 _2 T& J5 i #define S34 23
; O: ~5 j4 `0 D% a* s( z4 w* c#define S41 6
4 [& Y% t- S' Z6 x* H$ q #define S42 10
% O, R3 B6 I; W; { }1 K X1 V5 n3 e- x #define S43 15
, R& i& Z, q0 p3 t3 a$ Q #define S44 21
0 D P) x7 o+ J/ P9 l' F3 e
& r0 ?! y9 E' @9 c" W. ustatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 B, r5 f1 Z2 Y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ w. v4 F5 m: s- K0 rstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% B6 `* u8 `6 ^5 f9 ^, gstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* ?5 T( S& m/ X9 ^0 [) Gstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' ~9 z+ ~, R, v
) }/ W4 i. m: n% Q 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
7 T5 h1 W; J& x% q' c};
1 b: j# j' K. _6 l1 I( }
0 v) h6 b& F. u /* F, G, H and I are basic MD5 functions.
*/
0 N- S2 ?" b5 s" E#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& K& E( y% U& n7 o #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
0 }% X# F$ H/ u- q8 I6 A6 g#define H(x, y, z) ((x) ^ (y) ^ (z))
! E/ F' n+ b* g, r#define I(x, y, z) ((y) ^ ((x) | (~z)))
- X; [; u$ ]6 W+ q h- _
$ S7 W5 B8 h' h# f0 }. T1 C/* ROTATE_LEFT rotates x left n bits.
*/
& o! o3 Z/ Q+ v, R& p/ a( x1 I #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# X, I. _2 T. F, q( H
) z; G$ O; Y$ Q$ H4 y /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) J6 O c* H4 Q+ B/ ZRotation is separate from addition to prevent recomputation.
*/
8 y) r4 m% h# @$ W9 u* E& J, {! O #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 _' G8 U. K9 u6 ^8 s+ R* |0 K #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: T: h, D e% w* |) \ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, j0 F8 v# B3 d9 U. k#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 l4 n: E# k, G8 T( e: u4 D5 k0 W8 v
! n6 `- a* G3 Y- R* O/ o- A1 d3 O! O+ [/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, [* p) g( f7 G2 D6 B void MD5Init (context)
2 p0 a% q# L4 ~( [" T! o MD5_CTX *context; /* context */
5 \. K7 F0 Y" K6 U# D. ]9 m' e9 N{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 `( o, k9 r) T2 ]! j */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ e# p4 \2 y/ m+ X}
7 X! t+ O% s% ?& h9 w3 v
( J* u: y. p8 o: | /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 r0 Z+ |* T2 _; o" L void MD5Update (context, input, inputLen)
4 x; m" c% @* Y4 o2 A3 l! F MD5_CTX *context; /* context */
# J" ?1 Q0 A8 T5 Q/ v L: Z unsigned char *input; /* input block */
% c' G* x1 A1 Q0 N" bunsigned int inputLen; /* length of input block */
! G8 j c$ J, f: i {
unsigned int i, index, partLen;
' |. @' S( z. k
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
. T3 _; Z, f* S, B; l. b7 d+ e! [
/* 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++程序


+ p k* c5 e! U) x( J9 S! k, O) t: S- f
3 r! Z6 {5 E! T& ~+ b* |, E
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-22 22:48 , Processed in 0.061753 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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