中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* n5 E$ y7 c; i#include "md5.h"
, H4 d9 z. N6 r$ k5 n4 I
: V( g6 M/ }3 s2 }5 i+ w. d+ c3 K/* Constants for MD5Transform routine.
*/
: D$ D% Y6 P7 V1 O
" L, h; X Z( F1 G% p5 q
4 n" m% O! w% V#define S11 7
1 A& o$ _+ b. e. n9 Y! r7 @ #define S12 12
4 L- E5 I# z; f& V #define S13 17
9 j+ I$ W' t0 b+ @ #define S14 22
6 J5 [' S+ ^8 J9 r7 g( b' w" _ #define S21 5
) k* |+ u6 w* f/ }5 w9 P/ q* g #define S22 9
. x7 P" i. t8 h. Q0 ^( b3 _' o#define S23 14
8 i5 v' `' }6 q+ F6 ]) q0 T( q, N #define S24 20
; a8 x$ R$ n o( w6 K# f( l#define S31 4
( Z) G( _& \, q' Y# ]+ p #define S32 11
( c8 R7 A \; S5 R #define S33 16
6 P9 w8 H' X9 Q3 s/ w6 _#define S34 23
* I- H2 \6 p" {) p+ g6 u3 ?#define S41 6
8 U2 ~) R5 y: u+ M% \ #define S42 10
: w" b& \9 u0 H; |8 u #define S43 15
# ~1 P T9 h+ O# | I- ~" E- _ #define S44 21
3 L9 Y: q4 M& l
8 k9 h* {* |# H% P& Hstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, p- g( n; R6 m+ ~6 \ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' {! x* J, c$ |. v2 Gstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 I( y0 i" _) R' }static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" h! x+ \: R& G: s" q3 o6 q) W static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 q6 ~; Q$ t; T! X; z8 K5 m
/ y6 @7 }4 J" D 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
6 U6 R P! Q6 D2 q3 k& ?" _) n* k* R};
c1 p' X1 E# C4 f2 ~
) l: Q. Q+ {/ p, E5 [& `/* F, G, H and I are basic MD5 functions.
*/
6 @) y6 \, w. V7 a* `+ x% }+ l#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ a! m. P' Y$ z" M5 p1 g#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
a& f. T& d$ J4 C+ {3 U; J #define H(x, y, z) ((x) ^ (y) ^ (z))
% ?( D" n# G) q0 J' R#define I(x, y, z) ((y) ^ ((x) | (~z)))
& K' W# R6 q1 I6 w$ R7 x& J0 g7 Q
% Q' ]" \( o9 W" @% R0 Z/* ROTATE_LEFT rotates x left n bits.
*/
/ g/ R) L5 ^: a/ V2 [! @- \#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
9 O- Z' n' A" Z# d
& b: ^ j% T* [1 p( y- v9 B/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% W4 {/ y' C: U# a2 w+ V' J Rotation is separate from addition to prevent recomputation.
*/
h6 p- u: B# 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); \
}
2 W- Y7 d, [) M9 `. Z0 D #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 P) B) S0 c% P$ w j#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& |: G" Y- ^' n' [! e5 d #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 h* c0 ~' P1 `/ ~/ k
`9 f4 X+ r/ S/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 ^ D% E( n) b/ Wvoid MD5Init (context)
/ ? ~. R/ C1 a P9 W! z7 G MD5_CTX *context; /* context */
4 M! _9 L f6 q5 [, s e0 T {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) {1 W7 J! |+ V% ~ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
& W i/ y3 N% R% F}
! t, l& `! A! A' |" h( Z4 i
$ R, r& s0 R5 K7 z y0 G/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; n) |. f0 U* V+ _& d void MD5Update (context, input, inputLen)
$ ?/ b/ w+ N7 w, H; X. | MD5_CTX *context; /* context */
7 L7 }# H0 v% {9 _1 `4 u$ d. N9 V: [unsigned char *input; /* input block */
. E/ n" f2 G0 f! o5 E# D+ i Z unsigned int inputLen; /* length of input block */
% k, ~' ?8 r; @ {
unsigned int i, index, partLen;
1 y+ u! _$ z! M4 j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( t% g, K. c! V9 ?% P9 J
/* 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++程序


5 X; i: f" V3 K
- R% @+ o$ A3 u9 r, M" F D
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-1 19:10 , Processed in 0.057855 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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