中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& N( j. E2 s+ I7 B/ G' u#include "md5.h"
. L9 T1 t- @, z% e' S3 [7 P4 c6 u% Y
0 T# U Y8 {+ a. R% A2 ^- P* D3 z/* Constants for MD5Transform routine.
*/
: c. `5 E9 I! |' \! T' B
+ @- }" t* ~4 M% a. f \
+ C3 t+ S! e+ q#define S11 7
) i1 o4 b. E7 ~! U #define S12 12
2 P4 c% A, {& g #define S13 17
$ P* k! Q& \: N7 G( v- m#define S14 22
! r3 j$ z* q0 s( \) i% }2 j2 l#define S21 5
) ^9 G& P" L; p& Z#define S22 9
Q( D* o3 o: ~1 i. w #define S23 14
$ i3 ?7 ~$ p9 j#define S24 20
7 U# G& t: p- v1 |1 x2 p #define S31 4
1 B1 q1 E8 f8 ?+ V- z#define S32 11
. h" Y8 d, T1 [! s; Q* M- o) C z#define S33 16
A, t" g' u' V! [2 G0 Q2 e#define S34 23
9 z2 ^7 h6 K0 v6 E0 o0 l' ` #define S41 6
7 q1 Y a: e% a- o5 k#define S42 10
) s- ~1 q: {* x( _* o0 o6 A#define S43 15
4 f$ E; |' T, h$ V( w% |7 V #define S44 21
1 l& e `8 m5 g& s$ P# Q9 I+ V, T: K
9 O. z3 m! G/ z. d9 A/ nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 {- v! q1 z8 N! q! bstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 k! T, T# ^, S- P$ ?. _4 ]9 Wstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: e9 s$ {9 q! d1 u8 ^% r- M# S static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
1 @) V( j2 g+ C* T. F; Xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% Y& N: o; G% T+ R7 d! P" S- ?# z
! k9 a: d3 |* e- V* ~1 h3 ~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
" \: ^. |2 h$ f! U! n };
; a$ {% x+ R4 N! E8 _+ R W
, u1 u# y+ g; L0 M/* F, G, H and I are basic MD5 functions.
*/
|" `" r4 j2 b% ?! n" t0 X #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 l9 q$ Q* [* Z7 R0 ^8 @( \ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( r9 ~& p5 O, L& A& n#define H(x, y, z) ((x) ^ (y) ^ (z))
7 F/ g1 ]! O! D) l#define I(x, y, z) ((y) ^ ((x) | (~z)))
# j& C/ I8 O* [. {
' G& X6 G: O: y% \! E( [/* ROTATE_LEFT rotates x left n bits.
*/
4 K) t" _5 l6 `7 c4 h8 {3 E ~' ] #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 w" I: r* D0 M9 n% }3 U
8 w* u% S6 b3 x8 s /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
b5 `7 s& c/ b% ^1 u# c: k8 V. c Rotation is separate from addition to prevent recomputation.
*/
8 A% U0 f9 r% n$ ~* b! I' D#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 v1 D$ g/ I6 M, A#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 d9 Y( {' d- C" p4 K7 { #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 _5 h' V' g% Y& Y#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 w: v% T; F; N6 `( |3 N0 G
! `8 y; B! s# i% e) a; l /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" T0 y( r; G0 Nvoid MD5Init (context)
4 N8 s- C" a& \1 G/ \" C MD5_CTX *context; /* context */
8 E( p4 N, O7 d2 I- w{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 g# j" }7 @; I*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* K* B* q0 s5 P7 z; J& F }
" f" }" W, m" b- y* G2 I9 y+ f
" H( a* T) \- L6 P) Q/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- `+ s2 S" F9 V0 y void MD5Update (context, input, inputLen)
# |5 Q/ D- ^3 ~% z# s5 q5 G MD5_CTX *context; /* context */
* D- d) s3 ^$ m& Aunsigned char *input; /* input block */
" O0 X8 M/ |. Y6 G unsigned int inputLen; /* length of input block */
5 F: Q; ]8 D7 u{
unsigned int i, index, partLen;
4 w7 K0 P4 ? _( E3 L; L
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) G1 D& D' b; X7 x7 u
/* 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++程序


/ n) r" B1 k( H( R; x0 e8 ]
8 x- M7 G N* k$ Z& m
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-25 09:42 , Processed in 0.086392 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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