中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* X2 t. J3 p7 V1 K/ d0 L) b4 f #include "md5.h"
/ q8 V/ C3 _! o3 T, I
) G7 z9 e7 O* F5 U/* Constants for MD5Transform routine.
*/
& c4 Z$ H: }& Q8 \. ^7 [
, B# T/ l/ U) M0 J3 O
2 Q* g. W L6 D% E1 U#define S11 7
) b5 f! D5 n4 p7 \' H#define S12 12
+ K& V, R6 Z6 D; ~1 ~ #define S13 17
0 {1 w9 K2 Z0 g4 z f c% h#define S14 22
* m4 N: \) k- E #define S21 5
- D6 X. }6 f t0 a& x& J #define S22 9
" L6 x# ~5 Z- O$ W* P# h9 i6 H#define S23 14
) T( ^' _& P. Y* e- R #define S24 20
- x/ F: M" ]/ L- T+ U! F#define S31 4
2 F$ L) H V. m( i #define S32 11
* y% L+ `2 O+ r% F$ q#define S33 16
/ Z- w& Q4 g8 j! f#define S34 23
. _: P% y/ S! Q* j0 b4 f #define S41 6
/ c3 s! _. y) d- Q8 c#define S42 10
$ E: L6 t; R4 b3 Z G #define S43 15
) k/ S; ~) t9 P6 ? p$ a #define S44 21
& l: Y2 c) ^+ a; ^
' _6 M* v5 N0 V. n' E7 ? static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 a% z: j. ~$ Y1 G) t( jstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
1 o& P* e3 v1 o1 R2 @+ R0 ^- m static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 H& W) ]% f6 t$ ` static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' a. F3 S* z: ]7 h) p& r static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- m; h9 `& D: U1 D X C* W
! r1 [' e5 d3 y; `, `# {7 L7 qstatic 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
2 t* k! c G z) m0 t };
& M c6 A- t' E c* }* ]) t
- p& g& u1 J" Y9 l; P( x/* F, G, H and I are basic MD5 functions.
*/
5 @, ?7 G# b1 z$ i #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 d& d+ u' s6 p5 ? #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
- e/ l0 m' |9 ]% `+ i4 ?5 }. s1 R#define H(x, y, z) ((x) ^ (y) ^ (z))
# d% [5 |6 ~, i2 S X5 x( `8 ~! j/ ` #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 i3 l2 V5 V# k" n: T
) m x: `, a" I& a: ]! f0 J /* ROTATE_LEFT rotates x left n bits.
*/
) x# O& w$ x, b/ U" u6 \1 ^8 \ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" W* O" W+ |7 h& q( O
& e! P% A+ e9 |5 P9 Q5 H4 X4 D( K/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. } J* L. x2 e9 r y" k* U6 YRotation is separate from addition to prevent recomputation.
*/
* a7 l$ o8 Q2 _8 W#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ X; v; U u. |5 g! l. Z) j #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 e; A3 z' u" ?1 ]( 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); \
}
' ]/ F" t; @2 G2 m6 i ?#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 B, G. i, D8 _3 _& t+ a
4 \ O1 O! m4 g( Y( ? /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" b$ o d* q. E void MD5Init (context)
" |5 o( I- j; O MD5_CTX *context; /* context */
9 {5 F, s+ d- @2 N% B2 d{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, |% v: A; m ?/ ~*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# j' z, H) r3 c% x. b+ N; |}
0 ]- U' x8 G- j* S% | [! e
9 M# q1 r) u0 f9 w6 U4 G /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! }9 X8 B- r, L* Uvoid MD5Update (context, input, inputLen)
8 k6 x) H! y5 R* b" RMD5_CTX *context; /* context */
! W4 v* k& O1 C: n; N5 h5 p unsigned char *input; /* input block */
2 R5 S3 R8 f: I% |. Munsigned int inputLen; /* length of input block */
_4 R b& \0 f( |{
unsigned int i, index, partLen;
$ t5 b+ `; G; I+ W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( o {6 `# @3 G9 \, g
/* 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++程序


4 Q( C) e- W( Q& }9 s- h2 x
8 O! r$ S d* u+ A0 Y/ Q2 t+ F
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-20 18:55 , Processed in 0.058153 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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