中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 o6 B0 l0 u( k2 H) Q2 I3 B #include "md5.h"
! a; p+ @( W3 [. ~: G7 \2 T
. S1 y3 `3 D7 m8 X ^/ _% s /* Constants for MD5Transform routine.
*/
& R7 k- ]) W0 J& W q3 m: u4 N3 V
* h F& m* G+ ^9 W* g- b3 D
6 B) l3 U c* d' n #define S11 7
4 c/ Y; h# n0 s: h, J c#define S12 12
3 l( k/ H7 ~9 P! a$ D#define S13 17
7 w' C, X: V0 }" @1 O3 d! r1 e#define S14 22
% ?; u: q: [) }#define S21 5
" p7 j( _! @1 u% P #define S22 9
( I' X8 K$ S0 J% M" @+ S#define S23 14
5 B) ]( _, K/ [5 h$ Y3 S#define S24 20
* I% |8 }* Y4 i% b! n( j8 ]! d0 ]' u#define S31 4
( k- _, c" k" H9 ^/ E6 v& ^ #define S32 11
1 r( x4 f9 B# Y# y u y #define S33 16
2 G8 r2 _. a: d: D* d+ o; ]#define S34 23
5 c: r" a( \) j& I0 F" i#define S41 6
6 U0 r% v3 |" g: p$ O) g9 z #define S42 10
' F; B: ^* n( s" c" D8 B6 v) I#define S43 15
: }( V, c! J6 N3 d2 B' X #define S44 21
( i* X0 X' S# \$ V
6 P: U3 B5 U! j* ?# P: {3 a' bstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ Z+ F: U. u @ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# b+ X0 ^+ P, W* w. {1 m( b+ g$ w3 H$ lstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 c& o7 H6 A- @9 }6 ?0 mstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 _8 i5 C; r1 N) y8 h9 zstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# C- Z$ j$ {. P6 ^% ^( Q0 H" L
# w7 s) ~, S" @; `/ w: b5 D8 E 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
% m, k9 Y2 q8 I };
8 `5 ^3 _+ ~" i( E; l' N* ]
8 |# b+ v9 x# W5 i3 V" g" ~$ M" ^/* F, G, H and I are basic MD5 functions.
*/
# k' j; X( Y, q/ R#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, q- g1 |6 f4 {, O1 C* H) u5 L& h #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
* E) `1 f! d# ]& J r6 |#define H(x, y, z) ((x) ^ (y) ^ (z))
- \4 b" Q% a$ W/ \9 O; h#define I(x, y, z) ((y) ^ ((x) | (~z)))
! H2 `: _0 v8 ]! N! i9 j
% T* ^2 M: d5 r6 h5 D- D/* ROTATE_LEFT rotates x left n bits.
*/
7 _5 `6 w9 J9 x) Y, h, B #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, L2 p$ @* t$ v1 c
# c2 y8 K4 M, T; v, H4 Y/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) W( q. T* T8 b) J+ p Rotation is separate from addition to prevent recomputation.
*/
( X0 \0 t+ F$ f$ D e, W$ R0 U" k#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) j9 T9 D* X/ G& 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); \
}
* D* i! P. D. O$ ~- g3 J, X #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, x3 P0 E. w+ g# P! E- O5 @#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' E1 j+ ^: D) S( P) @$ J
) l7 x' V+ u1 u* I8 T F5 b& M/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) ?( N: w" ?% e! `( Kvoid MD5Init (context)
$ m/ Y4 c% S! K MD5_CTX *context; /* context */
6 ^% Q% |# N) }# r {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 c, y- k+ v% z( T */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 m! _" C: w6 |/ z! e" ?}
, d! x+ E3 t; `: _6 W8 g
0 k7 }# B" O, `. B! `$ m, x: a- I4 K /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* c7 @" m+ q* W& e W9 [void MD5Update (context, input, inputLen)
$ [/ L; s* ^# `5 \) L3 ~$ SMD5_CTX *context; /* context */
' [. v# X4 v; ?9 p6 ?# uunsigned char *input; /* input block */
: \ C, L2 D$ q8 y+ Z! Y+ Q' Yunsigned int inputLen; /* length of input block */
3 U& Q5 }" K; ~2 L{
unsigned int i, index, partLen;
" z: N. G b0 K' ?6 ]7 u+ S, j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 q. H) K2 f- Z g& a: f' 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++程序


; }, R- F' Q, i$ O
( b" O3 W h0 X4 h) b3 L, w6 }
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-1 09:31 , Processed in 0.099329 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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