中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" b0 p- A8 K2 p; {0 R #include "md5.h"
" e: s. j) K; ?4 E/ h
0 [3 v+ G/ ?' F: r/* Constants for MD5Transform routine.
*/
) _- H* U5 d* |+ x( ?
1 J6 }. H- h8 N8 I
1 R! [5 q% I! M I5 H6 d$ C #define S11 7
. v5 \) z5 ?; z#define S12 12
2 k" e+ y* S9 q0 f @% F#define S13 17
. D4 D4 _5 m9 D/ l #define S14 22
" B# o, p6 W9 } #define S21 5
2 J) B; s; i- C4 ] #define S22 9
( O, e( q1 D+ Q8 B/ U% ^' S7 E, U #define S23 14
$ e% p; m( d4 A! x#define S24 20
' r: S; E0 s6 ~( n #define S31 4
# a. u6 A, @/ x! D #define S32 11
$ F! @6 w" c+ K2 m" j' x#define S33 16
{$ G! d6 M- R( |) b' e- G #define S34 23
( h1 a7 J! ?. F$ Z #define S41 6
2 n( O( C$ }& j0 X+ u- P' b1 x #define S42 10
* ~- [( p3 l) k #define S43 15
3 \1 g: [9 J6 K% @$ n- {, ^1 B" l #define S44 21
- b; K; x* n4 B
: Y) t* b6 c: i. `- X3 p- f' F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! E. X. V4 A/ [7 f( Z5 ystatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
0 ~6 g8 O+ q& u @1 I static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
0 r3 Z' Y- X1 i" S; A* hstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 K i2 }9 r) \) f0 R. Q8 u static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* \& K- N) o. [: e6 ~$ t8 B
G0 ^7 k+ }! H# X& p4 Pstatic 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
2 M5 B: T5 A( x, C& J };
1 u: S$ d7 J+ c6 O; l
% G9 N# m& V5 {( h/ j/* F, G, H and I are basic MD5 functions.
*/
. ~# H6 Q9 h: I #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 H' _ P- P7 D' O8 k1 Y6 i#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 Y9 T4 }& b5 R) `3 @, C#define H(x, y, z) ((x) ^ (y) ^ (z))
" a8 S1 z( y' x. _& q3 }3 D#define I(x, y, z) ((y) ^ ((x) | (~z)))
( B- t. |' t8 e% @
/ {1 P! k( j9 H5 _2 S3 T; \0 I7 @/ P /* ROTATE_LEFT rotates x left n bits.
*/
1 u, ^$ q9 i! u3 \# z# d& }#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# H" ~. e* x/ h" N6 G# K4 Q6 Q
% ^- b1 q0 ]- l6 K( N0 [! D/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" F1 V9 Z5 l; r5 dRotation is separate from addition to prevent recomputation.
*/
' p9 a) b- K* I #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. V1 U! F- V* D #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 p/ n1 ^, m7 n9 L( u0 Q#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" }8 R6 F4 u" w #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 ]( p1 Y$ {( R4 ~( s
7 g0 J0 a1 Z _2 Y) D5 Y) S/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 h o; B; g4 N4 m, @, H$ j void MD5Init (context)
' T5 f$ N5 f! m* q% d6 s MD5_CTX *context; /* context */
" g8 `4 U/ C5 B% I- F{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ Q0 L% X& a1 i7 g& c- z*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! |) K5 g* C+ Y* f9 G' N0 ~* f }
/ ?. ~0 S5 n! g9 B
9 s( L/ U% U; W/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
_) W+ j6 `; i# g void MD5Update (context, input, inputLen)
; F2 C0 C0 F& u7 n- V2 qMD5_CTX *context; /* context */
% X5 n8 ?0 p% M1 i8 cunsigned char *input; /* input block */
2 w5 }+ M* i6 t zunsigned int inputLen; /* length of input block */
~2 D/ i+ `* V0 O) _6 J4 \{
unsigned int i, index, partLen;
1 S1 B) ~- S1 Z( C
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
4 R( o3 n3 ~! H, E; }. G% z; W
/* 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++程序


0 D! T+ A" X" q
; |0 W/ b" Y( [% E. `
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-12 12:15 , Processed in 0.310805 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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