中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" G. k- }$ I+ S+ ?8 l' @& N8 A- u#include "md5.h"
8 r" s9 ?( R/ O- c) s
1 a r+ |0 h$ [! E* ~ /* Constants for MD5Transform routine.
*/
7 m" G+ ]( w# ~. V1 ?0 @
: U: }0 ]4 _5 g0 n" V
" x" ?4 R2 p+ W: W) I #define S11 7
1 m. v5 y8 ~6 O) m( R #define S12 12
+ G& G9 ?% r G" F P: Z$ G #define S13 17
, W, x2 ]# H3 d% b+ u; u4 R4 x #define S14 22
6 _ w& j/ m8 F/ E4 W0 u0 n #define S21 5
$ I4 K0 |- q% `5 z. X" L#define S22 9
* S5 G& z4 E" h7 O' k: x# e#define S23 14
3 u" D& v/ b- \+ |( m. c/ P #define S24 20
) `+ w/ C# C8 r A! \#define S31 4
) i) J( a! O# r& j4 @9 t #define S32 11
% ?0 D. A- E" y d& f+ u1 M#define S33 16
; p# W- o* b' A#define S34 23
& D1 G# D3 U0 c: ]' P #define S41 6
& U2 v) i5 y3 k/ t: ~ #define S42 10
; @$ {8 H' a' I+ A; w#define S43 15
/ V7 o6 I. t) P7 u#define S44 21
! y+ i: R2 S3 s& F
' _; ?8 x; U( m! R, e D# u% Z1 \static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 f+ K# H3 E' J: M8 |# Istatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 ^9 s6 V/ }# ^. ~8 X static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 e& \' `9 d+ n- ^/ P& P! A0 n static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! f! u, g( e% nstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' c- b A3 o% }
3 {5 {/ \4 n: `5 `3 \5 Vstatic 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
, P% h8 P$ K5 L( [! o' H/ a' T* d};
. U+ u) \. _; ]. r
, E$ C2 B! ?% N# G: b /* F, G, H and I are basic MD5 functions.
*/
2 L( S _4 `% H% ^. K9 X#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% B; E: Z3 }- l Y9 G" Y #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# H5 m" _5 P* _; `7 _ #define H(x, y, z) ((x) ^ (y) ^ (z))
' d! l* l. }3 N+ ` #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ a* J# V( O0 d; w6 v: z- Y
% { Q4 a- {8 S* T, T! A: U /* ROTATE_LEFT rotates x left n bits.
*/
+ N5 R- L9 J( {+ }5 f% } #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& }, G2 A( Y8 m$ m6 k3 q
% c0 ]1 S# u: n5 s /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ h% b9 U3 H4 O5 M1 K& ? Rotation is separate from addition to prevent recomputation.
*/
5 `) l5 d8 e1 f$ T. F u #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. m* D1 ]3 @ R#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 d% y0 f/ J" B3 i5 `! z#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! y' [0 _5 l2 X. v7 K0 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); \
}
& l' [" |$ j* u* }, q! k
5 E' g' \" X6 p) X4 A /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. L3 [4 [5 z+ R void MD5Init (context)
0 K+ i% H9 Q i1 o MD5_CTX *context; /* context */
- e8 t" f: p$ X- a9 I. n# P {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 P U1 l4 L: C: t" ]- Y6 _*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 |# |6 w3 C! j' A3 z}
; v' G8 Y. ^5 `/ U: j4 K
" F4 O' J3 W q" ^ F4 M /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) w& g$ u0 q: j! s void MD5Update (context, input, inputLen)
9 l; D. L2 w2 [* N! t- |- AMD5_CTX *context; /* context */
( J' g1 z6 G; q3 Uunsigned char *input; /* input block */
+ g1 q" A8 Y) ?/ E$ B$ { unsigned int inputLen; /* length of input block */
% S3 ?( Q9 C( H- h' C {
unsigned int i, index, partLen;
, a2 {- a- m- Z$ f9 n } H1 Z- z) A
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& R( w( X1 H. u9 C1 d J' H: _3 k
/* 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++程序


" E5 T+ t1 `7 E$ h# n2 ?
- s1 @* R0 r I, @ ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-7 21:53 , Processed in 0.060296 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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