中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; Y: l6 }$ {' s7 j; l2 o #include "md5.h"
) O# u: h# U- o$ Y8 e# R2 A8 b
4 G0 V0 i8 _) |; `7 @' v /* Constants for MD5Transform routine.
*/
4 F' r% @ o& |5 C% B# C: G, o
9 |2 l2 v5 i$ x; l6 ?* X- q
1 O. ~0 z T( J#define S11 7
, t/ G6 i+ l6 A$ ?( Y7 T #define S12 12
* v( A; [8 E/ J #define S13 17
, |, j" ]" ]+ D1 W5 d8 N#define S14 22
5 E. @2 X- V. N! u4 X9 Z" c#define S21 5
# G9 h6 k: n* C8 R3 J4 y6 k5 ? #define S22 9
0 i+ R. e5 a5 `$ C8 c #define S23 14
; p$ P0 s# b- c4 y. [9 G#define S24 20
- b' @# K, t' k' b#define S31 4
5 G4 I- D/ Z7 u: @6 i8 S* Y+ l5 Z) Q#define S32 11
: \& M }. F0 A" E #define S33 16
f/ t0 [8 z2 ~) T, x% C) x& E #define S34 23
* Q$ m5 j. o+ ] #define S41 6
6 @ s! I q( A" q: I+ I #define S42 10
* f, u2 q# _7 K v3 B#define S43 15
) C0 }. t" A! W( k #define S44 21
/ v: H+ I3 H Q# v2 X. j
& K$ e8 G# d7 x7 ~' X: I, \$ m( D7 Astatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ U1 Q n; t6 H: Kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 J" c* |, m0 Ostatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 c/ f) q# T5 `static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* c' z& }/ C' @, Ostatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' @, U% ` }, U2 U# I' x" c: d
& i* m9 g' @2 H9 B1 P" Y2 P 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
. F* w6 H1 d+ Q };
7 S( r. G, p' f, j! I$ w4 V- p7 x
/ y$ P; M. B+ M& J4 i9 C1 Y /* F, G, H and I are basic MD5 functions.
*/
" ?0 X# f- @9 Z, N% ] #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. g U; J, N2 L0 j$ C( _- T, f#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& z) {8 r1 M% O" n; h+ g1 ?8 ^0 i5 a- j #define H(x, y, z) ((x) ^ (y) ^ (z))
0 R0 s& ]8 {1 s* V, b/ F8 B #define I(x, y, z) ((y) ^ ((x) | (~z)))
2 v0 c) E% M! H! q) O6 m: V
! M/ D8 ]: V1 M( `6 K/ [ /* ROTATE_LEFT rotates x left n bits.
*/
; \1 _5 z( r" L( o: ]0 P' W: T#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& U& v; N/ L2 P* B, \# Z1 X
1 e8 _3 |; N- W7 U$ o1 Y/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 r4 y& e& j D+ t, {7 TRotation is separate from addition to prevent recomputation.
*/
) _& w5 E) D0 Z. A" t #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ Q" g0 g6 _; d #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% m3 D+ m; i0 Y0 D# r1 K' i1 [#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% b) F2 [4 `* a. O% z#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 ]% p! N4 ^, F i- }
; z7 v: J9 Z% n% H/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 A0 b% A- N, O# ]" Bvoid MD5Init (context)
+ c# A) [7 v( T MD5_CTX *context; /* context */
# k) q. ]5 w7 X: F7 f/ B& Y4 N {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 i- L% w# f" v/ O% }*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- }$ v$ e/ d# h( [3 R3 P4 f# H }
N+ R( u6 N5 C$ A) |' ^8 v$ V- V3 j
/ d: }! Y' F( E6 J /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 A+ l) E9 }9 u7 v- I8 T* ]void MD5Update (context, input, inputLen)
0 I, R" B4 [0 |, ~* zMD5_CTX *context; /* context */
8 ~' B# V; @' v- ~7 ~ unsigned char *input; /* input block */
2 t& g. m7 q5 J0 K( N4 Y& t' R unsigned int inputLen; /* length of input block */
' ?' B0 I; _" q9 d{
unsigned int i, index, partLen;
7 [2 ?/ S! F- k, _$ {6 ?
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) n5 L$ T5 J: x3 z4 H# S
/* 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++程序


$ M0 x" B' Q( h7 B8 G4 X
( I; r5 N" ]) D$ y( G+ J5 i
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-7 08:33 , Processed in 0.214814 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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