中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# X) |8 V3 v9 |( J2 m0 i/ n #include "md5.h"
8 e0 T" }- L N0 ^ k$ R- d
, T# j8 d+ ^& t8 _- z) `* e: t& t7 Q /* Constants for MD5Transform routine.
*/
& S2 Q: U# g/ i4 W0 {
' h& k$ n# r( S8 ?) m
; X1 z& f1 i# M3 o#define S11 7
) `1 X* X3 j2 z4 v# @$ C3 Y #define S12 12
U6 g+ G1 ^' y# f#define S13 17
* h. ]' g1 m+ x x) a* ~1 D #define S14 22
' g0 Y7 [3 I8 R6 ~#define S21 5
3 ^% K$ ?1 `5 m' t* j% h#define S22 9
) m0 t) Y$ ~+ y# V #define S23 14
" h, P/ X, a, {1 h1 }4 S #define S24 20
2 @5 C' ^& z0 w K" y#define S31 4
& m) g, t- j* }- m M- p#define S32 11
( |" C6 t: h. W9 r #define S33 16
9 K$ i8 T" m) Q #define S34 23
$ V/ l: U" \, }5 @' j* G; n#define S41 6
6 C- g3 w. ^+ _* y0 `#define S42 10
) p0 z3 O* R/ b% R. u #define S43 15
" [5 j# k/ x8 Y9 F0 K #define S44 21
( @0 p2 R4 w& U! V! L
3 c- i' y$ S8 @5 R E$ c static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 n3 Y! o' o6 G& T! istatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( g/ X& X# ^: [0 ^1 S* [& C/ M: Q static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ p' F; H9 a: j: ?8 Wstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" k4 G6 W& ] b* n" m6 ^: N static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, m. S' N7 g4 s* J
; C* R" C- v. F* F% ^7 ` ~ 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 c- L2 V3 Y; T% e8 B: Q0 L};
2 p8 A- L* R) ?+ l5 ^
& J4 C9 B5 R+ {. _/* F, G, H and I are basic MD5 functions.
*/
' C& q4 _# {3 s #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. o' _8 p' m0 r7 t" ?9 U* I9 K& Y#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
8 L! }& m* P, u7 Q#define H(x, y, z) ((x) ^ (y) ^ (z))
% s- c2 I: n. Y' c; o: |1 t S #define I(x, y, z) ((y) ^ ((x) | (~z)))
+ X5 y$ l1 u7 Q0 x w4 P+ T
. J L8 w J A0 {/* ROTATE_LEFT rotates x left n bits.
*/
/ L# K7 Q) T5 E/ v$ ?0 h#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 T f/ G6 h( ~9 ]3 Z8 k( p! x
+ A# q! t1 @! j7 j$ H /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) w: D$ u/ F. N3 wRotation is separate from addition to prevent recomputation.
*/
" ?* X' L4 y- a! z. G. ~#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' Z: g' `! H3 L9 P6 r/ {! S #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( s1 D9 }* ^+ q9 A" |" X, D7 G #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
l P5 @, W. L! ~ f: O#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% f7 _' i, q9 N- H0 M' [+ w6 p
1 M' i! S5 G4 o, ] /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 R- J8 D+ D8 k3 Xvoid MD5Init (context)
4 X: |) q+ r0 P$ ~6 jMD5_CTX *context; /* context */
, M4 u9 o8 N+ x W$ U l{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! W" M! W ]. |+ J* U7 M! g */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 a. w# s+ s' L}
/ R+ L& ~8 }) L# r
0 u' N b1 Y$ c9 k! B2 ~! i /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
% r; g+ J* G& Vvoid MD5Update (context, input, inputLen)
( A2 _+ W Y0 @+ h' D: f, vMD5_CTX *context; /* context */
2 _% I. m# A. p4 q2 k/ j/ I unsigned char *input; /* input block */
& G1 F# O" M9 a$ Runsigned int inputLen; /* length of input block */
5 f4 X9 } i, I{
unsigned int i, index, partLen;
+ }0 Z) k: Z6 }% _: _, `0 y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ m: A; t0 ~8 u* z. K: v+ l
/* 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, B, x# o8 i
9 @: v3 {6 F5 Z( r0 I) k- f: z2 M
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-16 14:14 , Processed in 0.499646 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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