中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: ^) H% [6 k% x; H# b #include "md5.h"
% j2 w& R K' t' ^
$ G' B V+ e3 d/ c# {6 z( v /* Constants for MD5Transform routine.
*/
3 M( S. ?$ z* G8 F
! t: {' P: h) q. z0 P7 [5 C
3 z& ^5 h: q9 H( c6 d9 @8 l #define S11 7
$ T3 |6 s2 \( L3 D( U+ P #define S12 12
+ W9 T# K8 @2 @2 K' X! M" Q/ u #define S13 17
, J0 ], R5 P. s3 E' a1 X #define S14 22
' l4 s& N4 Y8 F$ f, x- `1 K/ r8 q#define S21 5
7 o* A6 F$ {2 y0 G8 }& w; u #define S22 9
% L5 @! q) _ e# j, C#define S23 14
& O |( o7 w: U! e# s# T #define S24 20
+ n1 C- J7 G% K8 y! @- r #define S31 4
# x0 @. Z) m2 v6 y#define S32 11
* z$ }: U! ]! w3 Z: e! g( a" [ #define S33 16
0 X( r/ p/ p4 F S) Z& N1 r #define S34 23
$ O0 S" O, x* u3 j #define S41 6
5 ~ t% Z) l Z* d#define S42 10
$ F% H- [5 ~& K( w3 c! u #define S43 15
; N1 o0 A2 [: Y, @#define S44 21
( i- v+ M0 W/ `$ O# A
, V O8 I% c1 F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, l* c, m: F$ E' _static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ C8 g/ v6 U Z ]: Wstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" K. q7 [) B2 x5 C* Lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 P8 F" f4 Y" Dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, `9 s- B+ z ^1 L; F4 V
+ }9 C8 ?1 t4 R% i1 C; {6 d/ ]& Q+ jstatic 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
# k& A, T& g& Q- } };
+ y/ L9 R3 d2 }0 D
$ X* q2 f8 I& C; H/* F, G, H and I are basic MD5 functions.
*/
1 A8 l" `0 T c5 ~- `# i2 B; V#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; Q9 t+ n( v$ i#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& I' `" w1 }7 H" P #define H(x, y, z) ((x) ^ (y) ^ (z))
' C6 R6 q5 ]$ ~% o #define I(x, y, z) ((y) ^ ((x) | (~z)))
2 _! o" x6 [! h3 _7 {
\( q D, x) O* v, ]# W0 Y /* ROTATE_LEFT rotates x left n bits.
*/
9 t) t+ n0 S. h% a$ @0 y# S% x #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 @: N7 S' c8 R$ f9 k4 @
( P; S# O3 r. r6 E7 t% s( m/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
9 N2 F7 z' S1 T8 s. ?: | Rotation is separate from addition to prevent recomputation.
*/
. ^3 e7 A% S" Y% m #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( g, G2 _1 \- ^+ v5 Q9 v#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 k: G0 W, H, ], P( L9 v- m: n, \#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 l1 W! h& ]1 s9 P" I, h7 q1 u/ b3 S#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 t: j& w8 a4 I* M4 Z
( H+ m- F+ a+ f a7 @* H# _ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 e1 { o2 O8 `. k void MD5Init (context)
& ] ? _, g$ p* p MD5_CTX *context; /* context */
* ~: |" j! P0 W( I: I/ C {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! R9 x$ h6 P$ S$ U* d" Q2 S" k- E*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 X1 V1 R% F4 n$ V {# L+ f}
/ ~; `3 N6 A* V$ L7 C
: M! F, E1 w! ?; a /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" Z' F) {5 z( `6 S4 \+ lvoid MD5Update (context, input, inputLen)
, ?7 S5 o5 ^3 KMD5_CTX *context; /* context */
0 D G6 i i1 k" zunsigned char *input; /* input block */
% e& b( i+ ^- @9 g unsigned int inputLen; /* length of input block */
% _8 l& O; ]! p {
unsigned int i, index, partLen;
r# c$ x5 P4 N2 {1 ~
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 o$ }- j& d( d+ \4 a; \
/* 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++程序


2 x' `. K* _0 O# c8 ?( B
& u$ L- Y6 R, r! v4 x
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-11 04:15 , Processed in 0.069181 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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