中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 _& O) S' _' E #include "md5.h"
- c: W+ C9 _( g$ W! o6 B. G
9 r$ y2 b: Y0 B0 @0 o /* Constants for MD5Transform routine.
*/
* b$ P0 \- _- f/ |4 V0 d, C0 g; O
2 q" s& R# |, l1 H3 k' D
G" p/ Q: x9 v" B' k2 ] #define S11 7
' n$ d( {2 ]: Y) k1 M! f7 k( G #define S12 12
& D. e+ ^% O: ~1 r4 \7 f- b# } #define S13 17
& V% {) o& _: s6 \$ v#define S14 22
) k% x- L! u: s$ D3 C; M* X% t #define S21 5
4 |0 e! U$ Z. b, I5 ]' F% L#define S22 9
3 W Z) X2 S8 V5 m4 V9 z #define S23 14
, Y3 x1 F. X2 B+ B4 H3 Y #define S24 20
/ n, @* {* b( c! U) E: I#define S31 4
+ Y8 [& C7 Y1 Y2 F#define S32 11
+ O2 q$ y- u& k2 l#define S33 16
; s, a4 V' C: W+ l- a/ Z #define S34 23
2 N& y# J- A8 C6 S; ?2 G* D2 @! W#define S41 6
/ j: ~: X. \4 q' Q, B#define S42 10
& G5 i$ U- }+ j2 d#define S43 15
) L {' ~. k, F6 W9 U j: w% V#define S44 21
* A5 C) L c& m# i7 i5 S
/ l% A1 _5 Z! p) \0 N8 nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 c7 M$ D( T8 W* a1 J& Astatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% d$ ?* E6 Q0 O' o4 |9 N z: `3 K static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 R' H/ I. u# X6 p5 |; E static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; v7 h( Q$ s& O; Qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 S1 Q0 D5 ~. T' c- k, W# T
- i$ o. m! c( ]4 H3 X. E. a 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
7 d0 ^2 `8 D R! Y( B3 V; C {% f};
. C/ H/ B( f: Z/ Q
( R! K4 h- t C" [/* F, G, H and I are basic MD5 functions.
*/
. h8 P2 t' Q; | #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ S. t+ L% t% A3 X- A #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
j6 S% [: M& j( L+ P#define H(x, y, z) ((x) ^ (y) ^ (z))
0 |* ?; ?/ ?; _0 }" C! y- Y* E h: j #define I(x, y, z) ((y) ^ ((x) | (~z)))
; h1 u1 p! H# O$ s4 u4 v
. k, {# R1 L) T8 L. y/* ROTATE_LEFT rotates x left n bits.
*/
' J+ [7 w( I; O T7 A, Z: h2 n: R#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
n! d- f* a( D3 n; U2 X( c: s
+ L, u7 e2 [9 L; F/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) X4 L5 @" z: l Rotation is separate from addition to prevent recomputation.
*/
4 H* y$ j' ~# ^+ V9 u G, q #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 T7 R: G0 c, Q3 u% \: B#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
[8 g$ _6 @5 |( U( \, |) P' u4 } #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 j- O6 S% w% A2 G6 ]8 r#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 L- f$ X: E2 f1 }; V
# _ o! x* @4 w/ \( {/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- x e9 Z7 S, `3 \void MD5Init (context)
7 P: K! L$ K: L6 x* h) mMD5_CTX *context; /* context */
8 q2 J/ L6 t; P4 V{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
K+ Z4 |- K" `6 S3 o3 T */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, u. `) A: S- H. R' Y5 a }
; k% d8 c! V4 [ i3 [; h
( Y& x r. Z! P5 Y- \/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 s* i, z; I+ ovoid MD5Update (context, input, inputLen)
& V2 p9 ^+ N6 P MD5_CTX *context; /* context */
1 I% c8 u1 R. D# d% L/ \8 {unsigned char *input; /* input block */
6 k" ?; v1 D$ @ U o) k unsigned int inputLen; /* length of input block */
6 H( [1 P0 A! f. l( h {
unsigned int i, index, partLen;
Y$ r3 {8 F. \" U f8 V4 Q: P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& u( E( E9 k& S. Z- i
/* 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++程序


. `' X$ Y. c& S6 d( [2 e0 J
2 W! U. k2 n+ F x% v; X: r' ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-19 13:53 , Processed in 0.139734 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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