中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* j, n+ O; T+ a+ n6 }% q* K #include "md5.h"
* R( F5 H! w8 _' p' z
' k! c( Z( ]- ~5 v) V /* Constants for MD5Transform routine.
*/
C/ p2 `, Q+ `5 k" Q4 y& j
0 X% i5 \6 _7 g; q: t# l8 c3 b
( H. N" n6 {; V( V) @6 a# z#define S11 7
0 O: J% T+ [, X3 [3 p+ d5 M, m #define S12 12
# a8 u, W; a6 H3 |1 p F#define S13 17
; L& J' E$ e* y #define S14 22
4 |/ W! a8 T, g/ t4 g) t! |#define S21 5
' N D) T4 s# x7 o6 u! w #define S22 9
# _5 L g2 ?+ x9 x5 G, }#define S23 14
, O$ _ E) f3 o8 C a#define S24 20
4 ^7 r7 ~- d' C3 O6 j, M" e3 s" R/ w #define S31 4
/ g3 ^( B4 X9 W& ~0 N( \' f #define S32 11
1 W Z) O& I+ U( h" X #define S33 16
2 _" H' e) c- r! X. s4 z4 o #define S34 23
! W2 J0 L4 E( E; Z#define S41 6
8 M1 d8 ?# H- A% ]#define S42 10
2 q# @8 ]) a @: a- Y8 i; T" B: { ? #define S43 15
' @6 k( X/ N V0 L/ x+ R, O3 f #define S44 21
: A% C0 |3 \8 f
5 \( s+ f" `8 H8 m* F4 _ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; ~4 x' d9 D! D static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& O4 }5 B- F, ]! K8 @# k static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
d& T( }' ^# @+ n! ]: a' l* p static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ \1 f* D1 r$ [ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
/ F$ P- i: n" D) P( Q
$ N+ }" p, ]! @ L+ H5 H 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
& v: s* c( i: w7 Y};
. Z9 L4 M, `% r5 i# _' C+ J
# k3 o! [( T+ n. [, c1 a/* F, G, H and I are basic MD5 functions.
*/
8 r) P% e2 j, l) x' r- e4 j #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
7 S- l c8 |* j4 s; Z #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 s8 A6 [2 M: \8 x5 {$ o: z% @ #define H(x, y, z) ((x) ^ (y) ^ (z))
/ X6 n0 k/ c0 J8 c8 j4 H% j& n #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 e# D. _. w6 b& \. h g1 r/ `/ g$ I
9 Y9 D1 w2 Y6 T. V1 S! R/* ROTATE_LEFT rotates x left n bits.
*/
# X, O' D4 P, @8 {; H #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# T4 `8 T2 z+ s
7 `; l! ^( O2 Z! \ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 U+ b0 a5 ^8 ?9 U5 vRotation is separate from addition to prevent recomputation.
*/
! K& R- o6 P Z #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 \2 r. ~! u* q' i5 E #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' y4 F, I1 }9 }* r% d #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* M7 p. ]2 B+ L, Z+ B7 p7 _, o/ R9 T #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 B% |. I3 N. n. ?
4 C, \9 r. Q; Y1 {: f! s! P m/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 W, C1 I- l/ Fvoid MD5Init (context)
6 p& W: t+ S/ u& bMD5_CTX *context; /* context */
# c5 G! a1 N* ?5 h: c/ [; D% l {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 o. v% y5 U! P$ p2 ] */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 N$ T; o5 J3 {. S" D- c}
" E+ G- g, I7 b
2 @% b; J7 g2 K /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# g3 L n6 ^2 Q- x% d+ C void MD5Update (context, input, inputLen)
* V5 N6 W& v/ v, ^1 _MD5_CTX *context; /* context */
0 K5 n; O( B% P6 |$ K$ V, N2 y9 C$ dunsigned char *input; /* input block */
- k4 f3 e i- L% {+ f/ Z9 N unsigned int inputLen; /* length of input block */
+ i% u1 Y" P; ~: C9 W {
unsigned int i, index, partLen;
' y1 k4 Y5 M G9 X) K
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# N2 N4 _% N" I; Y* B8 ]7 j9 z0 \
/* 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 c- ~. g) w% o- s
& f% S" l5 @' A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-6 03:34 , Processed in 0.057040 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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