中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# Q R: j! ~) X; f8 g#include "md5.h"
& w( }% _/ U+ ~9 W$ l4 H4 J
7 o! i, ~6 b9 I, ?- Z# b: [6 z /* Constants for MD5Transform routine.
*/
) L5 H1 I: Y" ?+ ~- U6 N- M
, K" J; g5 z6 ]5 ^* P
0 K! q/ o% V3 q( J2 H, O #define S11 7
6 j; I+ I- {; b4 o#define S12 12
! _" @$ t0 f- N7 k4 j4 W6 z6 n5 J#define S13 17
, h% F4 k2 r! E* A# w. n #define S14 22
. `4 P# z% z7 r2 q5 k8 J; v4 w#define S21 5
: E" v6 z/ h3 D4 Z' |#define S22 9
7 g) y0 l% c5 L% l- Y #define S23 14
$ ^ A' }/ d! a& R! ]7 E! b- g#define S24 20
# ^1 J8 \3 R! B' m2 {2 L #define S31 4
$ ^4 K! W; \) ]- v+ r% w8 S' Z #define S32 11
) F8 y3 v) `7 Y#define S33 16
* q2 ~6 r! @; ]5 g1 u( B#define S34 23
9 n) I' Y$ F9 j$ U( J F: F/ h #define S41 6
1 I" ~% C; ~! U5 I7 ]#define S42 10
! N. d8 f9 j t$ f& `8 Y#define S43 15
1 k6 z, u: k$ a7 } L+ a) H7 }/ h) U#define S44 21
9 i. @6 {) C# |# o' y
( V) L* O6 T: Q, f: rstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 L4 J% l# o# I8 Tstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ A8 u* x" x Q; V5 g( Rstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& Z; i6 h- x: V& s static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, |1 e1 q; o+ U: [: V- A( Q5 U static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* J3 v1 m3 X. h* h# C3 K% Q0 ~3 _
( {9 M, ^/ C8 q) W/ b 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
! ~( d2 ]' h q* c3 e" E};
6 e& U' f9 P5 _5 @& A. Q+ W+ r
C/ w, j5 `% {! G /* F, G, H and I are basic MD5 functions.
*/
# a5 a! N9 S2 k( X/ K7 x #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* R+ P! O( j5 d9 w. B #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! o# j! N$ Q8 i8 \ }4 f #define H(x, y, z) ((x) ^ (y) ^ (z))
) _0 k: d8 B* K+ r/ }% T #define I(x, y, z) ((y) ^ ((x) | (~z)))
0 I8 ]2 c( U; k3 ]/ L& F9 J
" X5 O- Y6 [! z4 w9 l8 A/* ROTATE_LEFT rotates x left n bits.
*/
+ S( J$ m, c g! \4 Q8 X& o7 O' S #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, |3 A- |: w7 `/ Z" `
* d# @" J& B0 j/ a/ w* M) i$ P /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& u; P E5 E6 } Rotation is separate from addition to prevent recomputation.
*/
- q- N- v# i( e ~$ v' J#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 |+ h) W0 A! M4 Q& H6 m#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ D5 }6 {7 E# ~0 t5 Q8 ~ #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 J/ F: E: F/ z3 j) R#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% e$ k" E! J( I0 E
! ]* {2 I7 C9 t8 |/ p- w/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# d0 [) z% P% e" C% R8 G4 Hvoid MD5Init (context)
- }" c( X# c0 o! a8 F9 m3 i# g% @4 {MD5_CTX *context; /* context */
6 j) |& Y' J* U$ F- x{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, F: m% f8 i6 y# z, W3 H: Q8 ]*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ u' D+ T9 a5 l. {/ Q' f/ L' e! F }
: R# `9 Z7 U; t. i+ F0 [; i
E. X8 |9 N7 d9 Y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 s& q, M; o' v! @! P. h: rvoid MD5Update (context, input, inputLen)
, S3 v. v7 G+ v1 Q. N% v( \7 M MD5_CTX *context; /* context */
+ ~7 U; t! }% O3 t0 D2 @4 |( \ unsigned char *input; /* input block */
; i5 X& w! E# D9 ounsigned int inputLen; /* length of input block */
! W- o, r! X9 n& ?& G4 U4 R9 H {
unsigned int i, index, partLen;
( W1 D# M+ x0 D
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 H9 W2 C, [1 ?
/* 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++程序


% U4 v) R/ E$ i$ U3 W
' r3 j5 `* i. n8 h
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-28 21:54 , Processed in 0.141880 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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