中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) e) z+ o0 |/ ]4 ~- ^, D& r6 ], c7 v#include "md5.h"
9 m- d) _7 h7 v& B) G! n
$ h% S9 m5 N3 m, v/ L$ Q/* Constants for MD5Transform routine.
*/
\7 b) D3 k7 [7 Z$ f' a
A7 p* m- Y# f$ k! ]/ F
( ^8 U4 |; `! @: ~#define S11 7
8 r5 q3 e' i' f. r2 f6 G #define S12 12
* y& ]: U1 Z1 Q, }: c: l #define S13 17
' D! b9 I( Z" f#define S14 22
, Q3 { F( T! b! j( z* ?#define S21 5
9 p. U" j) ~( F- {5 j+ Z# L6 N#define S22 9
9 d( L5 F9 R+ Y2 a* p% @/ x; p#define S23 14
$ X* Z, x! m, r/ ]% P5 T1 m9 N #define S24 20
8 N& A/ q n: ?6 @. B #define S31 4
$ ]7 D5 `7 `+ Q# s#define S32 11
" d8 V% V4 L( W: z& P$ L4 m4 P5 f% w #define S33 16
/ J0 g/ V$ t7 e#define S34 23
) T0 u7 x: E6 b#define S41 6
7 T% S8 e5 B" Z3 M #define S42 10
1 F- e, q% |* C& B* B3 {9 g! D- H #define S43 15
4 h: r# E2 B+ m8 P/ q* c; _( O9 I#define S44 21
/ _! K A0 k) L
2 o+ F+ @( o& q: N7 ?static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 w" K% |1 h$ h2 T ]# Rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; i7 v5 b0 {; }' _- s1 Hstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 z* A; z& M# `' J9 a1 b7 U$ v. ]static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" [' r% w, U# ~+ }9 d! f; A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. ?2 R9 N+ F7 w# V* N. P# f
7 h- ]' j1 J) a, v5 ^ 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
+ i s& j+ r X8 [/ `9 t6 n& ?};
8 ?8 C5 Y: K1 k- L
J7 I, Z4 n6 \) ]% e/* F, G, H and I are basic MD5 functions.
*/
1 N; \# K& {1 B2 j/ R' y$ W #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 t8 B4 n: e$ S8 B9 ~#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 ?. j o8 j2 f2 A& `2 I0 G #define H(x, y, z) ((x) ^ (y) ^ (z))
7 q* t* |& c q+ f- c$ A#define I(x, y, z) ((y) ^ ((x) | (~z)))
9 Q- l5 p5 p2 y
+ B6 B! ^, a$ j% F7 E, f. Z& _ /* ROTATE_LEFT rotates x left n bits.
*/
n2 k9 J; o: X3 [" G/ m9 A K#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) R3 `$ ?1 T( Q- `3 r
+ F" M# g9 E. c2 W: K4 ] /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 t( E1 i7 I% K! N2 dRotation is separate from addition to prevent recomputation.
*/
* _6 _ ^( |6 Y) K4 ~ #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 S6 J( {& z7 a; R- u9 S#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 v" v5 q" B4 o9 M* S& M: 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); \
}
* ?1 s2 I' u* 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); \
}
8 R4 F- X( F, o: Z9 A
4 y; o( I& R2 i+ ]/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
0 N. _0 r( ?- @4 X( F4 p8 Wvoid MD5Init (context)
: x* z4 e7 a) n/ uMD5_CTX *context; /* context */
" T" K; i" W0 M& h- e, o {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. T$ @4 m% Z6 n Q- g' C*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 F; r9 L3 ^7 p( g1 z3 ` V0 @ }
( V+ ]/ m$ b# K) i
2 g( n* ]( U. x, Q% ^' z; x5 x /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ P g" g1 K* L; z5 ~void MD5Update (context, input, inputLen)
/ C6 U0 \6 n5 y MD5_CTX *context; /* context */
" B9 F& z% l; Q' P3 B unsigned char *input; /* input block */
5 C9 v1 A/ J) V& I" sunsigned int inputLen; /* length of input block */
5 M- s8 B4 K. I {
unsigned int i, index, partLen;
; n$ Q& O: T% i' C" m$ u1 v
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% C( r) z5 R& D/ c/ t* 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++程序


: m, Y. X1 J/ ^+ n9 K7 N* ?4 R7 G
9 ^$ Z* ~, t) d! ^
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-22 12:42 , Processed in 0.256055 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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