中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* \1 y2 q3 f( N, n2 ^ #include "md5.h"
8 N( x7 g7 B; h
9 l7 I, F2 k) |. j6 t/* Constants for MD5Transform routine.
*/
6 R! F# \) m9 _. |9 g# F
; m0 n/ t0 `3 `3 d$ Y5 A! j7 ^
7 w0 l: D, y3 G, ?% K: b) N; @3 }#define S11 7
p/ l7 w/ \. M6 E% ^( V#define S12 12
- v Z+ n* B9 ~; H! R3 s* O+ T9 |3 `#define S13 17
* Z8 J$ v E/ P% t4 Z#define S14 22
5 b+ P: j$ B# Q+ a3 h#define S21 5
# Y `' j3 P+ s' x& o#define S22 9
% l9 G) ~& I! {' X" ^! d9 }' m/ [& s #define S23 14
- a2 k4 w B: A% F# l0 ?#define S24 20
5 R4 E+ t$ P( ?/ P6 l: _1 H#define S31 4
3 l! b0 W# D3 n" T2 X) v% i #define S32 11
' q* a/ I, U/ L#define S33 16
; x4 h1 n } T( \/ R* ~ #define S34 23
" q- c, H9 \0 M. R #define S41 6
% I/ w& v9 E5 O5 d#define S42 10
, e% \1 A' d1 C) b; x- v #define S43 15
3 R( e4 \* ?. o #define S44 21
6 m+ t) I% y( Y' |
! T& H" W4 D( s* m( Z* z a8 } static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, D7 i1 s& s: u( w9 `! E static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! U0 ?9 D( d4 z x2 e, i static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ E( }7 `0 M- ]# s6 Gstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ U6 ^9 p# Q, V, K1 r* P static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 k% A# Q7 y, f8 J9 K
* f5 R3 W8 O0 W1 t# [7 m7 Z* `# S8 h 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
F7 O( o: s) i; F! E4 w+ k( |" N};
9 E9 Z8 l' G+ u+ C/ f; `
6 E. L( `& f% x0 F /* F, G, H and I are basic MD5 functions.
*/
# d8 ]7 Z( _+ f4 l1 R#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ z6 w- O4 e) B #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 h$ {, H2 L( t4 e0 F3 {3 x2 `#define H(x, y, z) ((x) ^ (y) ^ (z))
! v7 u1 \5 b$ ? y8 {" W6 P1 l; s6 d#define I(x, y, z) ((y) ^ ((x) | (~z)))
" `0 k. `9 N u8 h
0 ^' S: r0 Z- B% h2 G3 S" M /* ROTATE_LEFT rotates x left n bits.
*/
( j/ E: n+ ]0 c4 x* J2 H9 T5 Y #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
6 ~9 H; N' Q, `7 ^% g& S
) H# M6 O9 r' E/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* |; {8 P7 b- x/ @2 c Rotation is separate from addition to prevent recomputation.
*/
, A6 J$ T- Z- ` #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( [( K1 ~; d; n9 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); \
}
4 H: l1 F3 z$ c* C #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! M* t4 Q; |" n3 j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* ~% |7 b8 v. s2 c) C/ b
2 I0 j) z. b( G9 k: U t" y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 _5 l2 Z. H7 r2 I void MD5Init (context)
$ B7 P7 T6 y: X* t" m4 `8 y* u J& } MD5_CTX *context; /* context */
) ^1 J; q# r% U+ [2 Y3 P3 Q7 l{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' M) r5 \2 B, U: ]' l0 R b F* n*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 _& r9 x1 {* Q+ E- W) e, L }
+ o P8 @% A+ G' ~+ S8 ` D
h9 r+ Q1 v9 Y' U' Y' ?, \1 I, m/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ }0 X/ j' g/ p4 [1 c/ ~ void MD5Update (context, input, inputLen)
% {7 n$ o. w. a2 _& @/ P MD5_CTX *context; /* context */
' f& E* Z7 n1 `4 d" K unsigned char *input; /* input block */
+ M: J. d! A! \( d$ Y b: |unsigned int inputLen; /* length of input block */
! d% v5 G" U$ _. Z; X0 H) P, V{
unsigned int i, index, partLen;
: U4 G# ^, }9 G7 D$ V9 j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 T6 _1 Q; l; r9 P4 G/ 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++程序


; I7 q0 z: H& t& M0 t" r
5 Q1 o! N8 d% M w# s& G- d5 o$ g
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-10 09:25 , Processed in 0.058685 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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