中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. [ A& L% i2 \+ s. S+ e #include "md5.h"
. l! U9 t; C0 v% j5 P
1 `+ b3 [" t6 L( `( Z7 n/* Constants for MD5Transform routine.
*/
4 d- i5 ^- E- k0 m
* K8 ^- q. E! D r4 ^0 A
! R0 Q9 T: q3 e# h#define S11 7
! H- ], A- {7 s6 o$ w#define S12 12
" ?5 Q! b& i" f! d9 h6 a #define S13 17
G% O' G4 n; h/ |$ G% } #define S14 22
! l& r8 e) G& d #define S21 5
: j! a. Q3 _, s/ H2 Z7 f2 g e Y #define S22 9
# I% m9 o0 r6 ]6 R4 x0 V) s #define S23 14
: L* Y8 P0 I& h' z" h& b' y #define S24 20
/ P% R& D9 P/ }# a$ Z3 b. j #define S31 4
: Z' j q$ @" H, S, A #define S32 11
& F$ K3 E5 c; ]! I# q #define S33 16
) ?& i7 o; Z$ F# H2 W! Z/ Y( R% c #define S34 23
. L2 ?; Z$ X B7 r1 o7 S: } #define S41 6
1 J1 m5 j. Z; Z6 K#define S42 10
) \1 t/ N+ B, q1 P. A0 W#define S43 15
" |8 }8 u2 o6 C #define S44 21
t: a, `; M- T+ [( P
/ y5 q( t+ L" }0 Mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ Q6 [% s, m8 r- A P static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( b$ L( x5 u0 Rstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 U+ S# M2 O7 E6 _: u' T# b" w% N3 o static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* Y, ^* z3 _" t8 `' m static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* ?/ s B9 v. M, ^8 p
8 v1 X/ H1 l% p# z9 ] 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
3 S! ^1 e; R$ x0 C- [8 J };
s1 m3 U3 S/ J
7 \& `* @4 f" ~1 R+ Q7 @( ^5 A /* F, G, H and I are basic MD5 functions.
*/
( u# }# w' Z( ?- W0 x5 |/ S #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& b9 f& x" M' k8 _8 u7 F! U #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 J* r2 o7 n/ B+ n/ b3 `( L #define H(x, y, z) ((x) ^ (y) ^ (z))
9 `. N: P, D7 a8 @$ k#define I(x, y, z) ((y) ^ ((x) | (~z)))
+ e! e" w5 [* l; R
' ~* @( U2 c7 N5 b% L/* ROTATE_LEFT rotates x left n bits.
*/
( @: B p- Z- E5 I) r9 S* S#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# X8 s# h" x" O& ^- c& ?( C
# a; g: L c. r# U5 Q" T /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" I/ L2 L) z A/ H f8 U Rotation is separate from addition to prevent recomputation.
*/
' p9 {* N0 ~& P4 {, }, C #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 o% Z) l% e; k' O0 z- N#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) x/ _9 r/ Y5 J, A8 @( 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); \
}
! @) E+ c4 U1 ~1 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); \
}
h, ~' |& A! i5 w! x
2 F: W; g& `' L: l- }4 @/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 a/ b7 _+ a% r. jvoid MD5Init (context)
/ P1 s; |% J2 d4 a3 lMD5_CTX *context; /* context */
# I; d9 |6 l% T+ W" G- n& z{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ M' Z0 I7 d1 k# k: w' c- \*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( h5 t1 b% S3 w e( e}
3 r9 A- K/ r! A9 M4 |
. X+ ^0 ]& F6 o$ _+ H$ | /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& V9 T7 U4 |( ~8 E void MD5Update (context, input, inputLen)
- S& b7 R3 q0 Z } MD5_CTX *context; /* context */
0 g, V% n2 k& E6 H* B8 l+ t unsigned char *input; /* input block */
5 |$ |0 `* j$ N/ A unsigned int inputLen; /* length of input block */
) w/ H, @5 i% I/ P A @+ E* T4 H{
unsigned int i, index, partLen;
0 N* o" R+ L1 x; [ m P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" H1 q* l, a- r: w3 a- v1 X
/* 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++程序


" y8 j) y0 P2 i2 T
" J0 c3 v5 w5 X3 H1 t
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-17 15:42 , Processed in 0.209694 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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