中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* L; O, A/ K6 C0 ]' ?+ M #include "md5.h"
( k9 O1 x5 T; H! i) o) K
+ R* s9 a& E: i /* Constants for MD5Transform routine.
*/
! |3 j7 h, B- k
$ x% ~3 z7 E% t1 N" C
9 n8 Y& f; Z. t q3 Y& o #define S11 7
! ?! \" |! v/ h3 ~6 J #define S12 12
+ d9 U' `1 k5 V6 s6 z #define S13 17
w7 T0 x7 h+ y5 {: H$ h #define S14 22
- y( n, j, [/ i) d# x/ N, d#define S21 5
2 N' N! u' w9 t! V3 M #define S22 9
% u2 R. p1 i( A: M# \ #define S23 14
6 h! p; @& G5 }3 f! O2 r: J #define S24 20
" b. F/ l. d' `( G# _2 `/ ~5 c m #define S31 4
4 J; t0 q; C: n/ k9 z#define S32 11
* \% [& q+ L5 {. Z w! B3 `: k #define S33 16
% x' F# \- ^ h8 d4 }% H#define S34 23
t& k0 F( R* m. q4 r: {#define S41 6
8 ]5 y, K! x7 d% N+ P#define S42 10
6 T5 ?( a: s4 x3 p2 u; a1 {#define S43 15
, g, S! M* ?8 Q1 s1 H: v& W#define S44 21
6 s2 a$ Z2 C; ^! {4 l
4 h6 N7 ]! I/ \# J- \+ C static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ @$ m9 r8 d9 e static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ J: f0 j9 O$ j% ~static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% n% z" |/ n8 Wstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
( ^! i( r, K! Zstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) s) Z9 P+ ^, [+ `; {) M" E9 ~
/ z' f3 A) O" h' e6 J [4 b 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
0 l5 i' F* x0 }! }7 n: ~ };
; S1 Y" a7 q/ e( } r/ D- W
3 I) A5 m) F) A /* F, G, H and I are basic MD5 functions.
*/
# J1 X6 `4 _$ K. F, W#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ s, B r% G0 m. i( N5 { n #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& `6 O6 A N( u7 E #define H(x, y, z) ((x) ^ (y) ^ (z))
' V( s [/ v5 H; h #define I(x, y, z) ((y) ^ ((x) | (~z)))
4 M6 `- \3 m& l$ P* r9 H# D
7 E1 }+ g# _8 p& h$ `# @1 @ /* ROTATE_LEFT rotates x left n bits.
*/
2 V+ ~0 F; h8 v0 ^( _ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ |5 ?* m ^0 U4 B% {1 n
, ]% B& i& z$ V4 P6 j /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- H; D( w. @+ Z+ ] Rotation is separate from addition to prevent recomputation.
*/
1 H. B$ X3 r5 I1 X$ W* V#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 M, `$ w# W* M* F #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* E% L/ J; P& `* X6 f& E #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ _% v- M* p% P" c( e4 i: C #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: C! s7 C% H3 U
3 J; q: x2 I4 r' \. p /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) G- t4 f; I1 i4 s- r9 ovoid MD5Init (context)
/ J& `5 Q" b; M5 M- ?% w" m& `( p MD5_CTX *context; /* context */
4 v) K7 Y7 m: t4 x, Y9 _ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 Z6 L9 j6 a6 x: i*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
: {8 W, ]+ K+ Z: _2 ~; I# E}
, S F5 u" \6 I6 R( d9 A) J9 n# B; ^
7 r& _# J" ] n- w' T) ?5 [ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
0 r6 i. k5 H6 N; xvoid MD5Update (context, input, inputLen)
% U- G9 H3 z* a( t MD5_CTX *context; /* context */
, ^" ~& C1 @1 l+ k# B _- Sunsigned char *input; /* input block */
* q' ^0 D. [6 ]$ ?3 P+ Xunsigned int inputLen; /* length of input block */
3 j! t! Y' l, U {
unsigned int i, index, partLen;
# _5 D+ a0 Q0 g/ m) J
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! V( I- z$ q; h" ~3 M2 g, L, E
/* 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++程序


: y: I3 @) O6 R
) w+ C+ B/ @5 V9 ?0 s. O9 G
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-12 08:52 , Processed in 0.069839 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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