中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: ]& }2 c/ n$ i6 T& y% u1 y #include "md5.h"
4 p; l" Q' C. T* y M1 Q$ E- }
3 F: p3 A& w! g8 J# D6 O: e7 T/ K /* Constants for MD5Transform routine.
*/
; E" X# K+ Y2 W q' p
3 w( Y% y! z% k
# F' v" L: K+ s6 d7 U$ @ #define S11 7
: d! I" m0 n' o#define S12 12
$ m2 W, i+ H7 K! j4 C#define S13 17
j# \4 |8 }/ ^3 A/ h0 @ h #define S14 22
4 R% A5 H. }8 Y( l, T( m#define S21 5
m3 m& X; X+ U1 q! B #define S22 9
& ~9 Y- B1 J* A' B, o* s& T& o #define S23 14
r8 z# x' F+ h0 J9 M: g #define S24 20
" ?4 L9 v% i4 G E# d) h- x #define S31 4
- w3 \3 O' a/ G/ `# T #define S32 11
6 ?6 n, V" {1 o# G: k1 p5 ? #define S33 16
M! z/ k+ D3 J% G$ F #define S34 23
1 o5 c9 r% r, O6 P. z0 |#define S41 6
; j% y! H( ]% n; q #define S42 10
5 Q: C) U( Y% j#define S43 15
" d: e2 B3 a/ c, ]; L+ K# C N #define S44 21
2 }+ T! M; T/ z- X( k# A ^. C
; k3 f" e( B+ Z: m) a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- R O" h) R$ ^7 e1 wstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ e/ N) q0 e Q8 _2 ^& t! Estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
( x3 M3 [6 t, P z, W' E, s9 C! _ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 r# I! e) n' t# }static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( I+ O% d9 j5 m' c; {
) Z4 |1 o( C" `% O ~' j) a 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
. I+ U8 r1 S6 g, i};
& c. L, Z1 b" `$ ~, p i& q
; N5 {( k9 w5 n% \: p' W# I/* F, G, H and I are basic MD5 functions.
*/
m$ b" r5 p* @) V #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 o* u& v( X0 F5 y, {) E. }#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 V" _5 @1 F" T$ t i: m#define H(x, y, z) ((x) ^ (y) ^ (z))
2 Z6 x1 D& ? w# E/ K" d #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 Z9 ?3 M3 n: B9 u! n$ Q8 z
1 w* w% N6 Q/ ?- L0 A/* ROTATE_LEFT rotates x left n bits.
*/
3 e" S- o2 K, S1 x) ~8 L4 r. A #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: I; X" M8 D- Y( e* m. N4 b
# ~: B$ v* `* q5 g' x /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 Q2 }9 n% V8 }( P Rotation is separate from addition to prevent recomputation.
*/
9 n2 M6 }1 D9 b& p- U2 ^! f" V #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ A! c$ C2 g; t6 a- W" ]2 x#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% h* M5 e* I# b& F2 a8 b- h8 E #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
A* R k3 X8 V( p+ e ~6 O7 U #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; Z3 Q. b \. e' q) @
$ q3 |' y$ I2 u$ S! U/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
/ k9 ^4 Q: M3 a2 W I! x: g' b! c void MD5Init (context)
* u, ^6 T3 n% t/ j& L7 v- H/ tMD5_CTX *context; /* context */
! [& b- Y' D$ p$ c, i {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& v0 j: I$ s( _8 y A, q: I */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 q0 ]6 h$ k1 L) y4 [ }
5 E1 @- o3 G' R2 t2 v. N( h' b% W; f
) i5 J6 k( U" D2 w% F% ^8 U /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 @# J6 H( W! y! _( fvoid MD5Update (context, input, inputLen)
1 U8 r( b! z- r' ^ r5 _ MD5_CTX *context; /* context */
; f. E( y' R9 K- Ounsigned char *input; /* input block */
( R. N1 H/ R5 i5 X8 B unsigned int inputLen; /* length of input block */
m: q3 f' L( c4 p+ |; x+ `{
unsigned int i, index, partLen;
5 \1 x; E- z, n Z8 Q! Z3 ]2 W8 i
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
1 [9 V1 w& A6 g1 M( |' o: r F
/* 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++程序


+ B) O; m& N: F6 @7 I* T
2 ^6 U" ]* N) H) _8 p6 F& Y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-27 01:50 , Processed in 0.055690 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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