中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 I- |# H- u4 a0 G# a, O7 k U #include "md5.h"
, a( G9 L# P1 ^) p0 m3 o
L; }) X1 @1 K$ S6 s8 K- b# V B1 O/* Constants for MD5Transform routine.
*/
[2 n* [( X/ V3 v0 P" Y
6 A7 T, `+ i: S+ w) w# p" F
6 U2 ]8 u p0 L% \2 { #define S11 7
% j& N' p- A- Q6 m( N: c8 @#define S12 12
. h- |1 C' b6 O) U( ^ #define S13 17
x$ |( ^/ ]" k #define S14 22
- ~& {0 b! M9 } B#define S21 5
5 L. T# i3 s# ?. D1 R! h #define S22 9
8 T4 Q$ z3 }* l+ @/ I#define S23 14
+ e* }. A9 j, p0 P #define S24 20
7 w7 R/ q, ~1 e/ c' `5 ]#define S31 4
0 I6 x( s' S) U b" y" b#define S32 11
0 Q3 O% {1 I' W) c# H+ }4 m#define S33 16
) z' f4 T o4 Z2 o, e: a( f#define S34 23
( W5 h% U" Q* s: J4 t* q; h#define S41 6
/ }! i$ ]% P! |) S( H7 E" j #define S42 10
6 Q8 A& H/ c* G" h) d" Z: t* K #define S43 15
1 q" R' f9 Z Y0 A6 ~" X #define S44 21
) T+ t( {8 |& e5 I# c6 L
4 g" ]3 z% b5 ^ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 u+ }: F `" }( x) [$ f static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 |1 t; n2 r$ Z1 t/ W static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' z9 p' F1 i( E0 ]6 s9 x" m static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* p6 C: H ~3 ~9 nstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ L, F9 u G6 H7 q) J
0 N/ n9 D0 q5 y* O: A; h/ h: P istatic 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
5 T% }5 L( F0 d% x( j };
6 ?2 h8 \1 m9 B4 f
1 t. p' B% t$ m9 E1 N3 U) X/* F, G, H and I are basic MD5 functions.
*/
2 G, G# u( G7 U5 |- N. y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
7 O; r6 i. r4 f+ |* t/ X* j #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% t6 U& X; z8 ?/ H! t#define H(x, y, z) ((x) ^ (y) ^ (z))
6 y: k. W4 z6 J, O#define I(x, y, z) ((y) ^ ((x) | (~z)))
7 U* b7 V/ F) U5 ` G( u/ Q T
; u1 R/ Y9 u. `" A6 d: V /* ROTATE_LEFT rotates x left n bits.
*/
, V: n& S( @ i4 C0 P6 b' E #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 A! a' R) a* A' u5 J( @ F
7 C0 F& [: [: y* V N /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 [6 d" H% C z7 y/ Z Rotation is separate from addition to prevent recomputation.
*/
: x5 q# R5 N8 p" o #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ `& A# h, l& w1 C% B9 o #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: a$ q" D! v& E4 u: D+ k3 H#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 @# r/ Z! I+ h #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) k+ x5 ]: b, m% }& h: s' u- x
7 O9 y6 h% H$ N4 h+ R) p. {7 Y /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
C l6 m2 N( O% }. S0 R8 D$ f8 ivoid MD5Init (context)
% z5 e* D& C; V' o: JMD5_CTX *context; /* context */
" a8 o( x7 O$ V# z9 [{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ C4 z$ d; A/ u' B! }*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; g3 ]. K0 V5 w* V. d8 m" x1 a5 y+ y. c2 M}
1 ]+ T7 } T3 y, F$ C$ L
1 y8 z! J& r( M8 Z; u /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 }* @3 o% y' A9 k void MD5Update (context, input, inputLen)
2 s' I6 c3 v* CMD5_CTX *context; /* context */
- e9 g. S3 g, v( c0 e( e* i: m( Yunsigned char *input; /* input block */
4 g. D0 p# C, v- } unsigned int inputLen; /* length of input block */
! P( g# G$ Q; A! x4 S" M+ g: c{
unsigned int i, index, partLen;
# r4 y6 s; ?- F4 w
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
I3 G1 N; K" |# n R
/* 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++程序


7 s% c/ s% a1 C9 A6 q
; L6 A l" `; b2 f. T9 |/ q* L
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-14 05:17 , Processed in 0.071495 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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