中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" z2 f; \* @( G* x* N4 X3 m #include "md5.h"
9 h0 ?& q8 Z% e, H i9 ?
( ?2 a; n& d5 E$ S2 a0 j g /* Constants for MD5Transform routine.
*/
! @1 ~) @1 u, D. B/ d. J. D$ J
# f- {# H" R3 r/ \5 N9 E& g& z
0 o9 b4 P. Z% N1 t9 b #define S11 7
! L2 B: A" W8 ?3 i+ |0 _" ` #define S12 12
. h* w) Z, d# p9 Z3 u, k* U* s #define S13 17
9 ^, ]& ?8 q" x- c #define S14 22
( D6 `6 k% b: a7 ^# G#define S21 5
* L3 R6 W' c9 I" Z# j) ]#define S22 9
+ Y! H) h; ~9 x. j! O#define S23 14
8 d9 V! s, Y' P' x0 P#define S24 20
0 w9 m% u* \- ` #define S31 4
" ]0 }- H: F7 p7 n: p7 e' U #define S32 11
" J U5 @3 B% b+ u* r+ k, g #define S33 16
$ a7 ?4 C3 ]/ l+ m0 A+ J& z7 G! C. O#define S34 23
* {; G- }4 Z/ Q' L#define S41 6
6 x6 y; X8 I& ~8 z- Y#define S42 10
( N. z/ i6 G$ o* u2 D#define S43 15
; K0 O" u4 V0 ], n: r0 F #define S44 21
) W! k! |4 g$ Z. s# H; j, ]
0 Z" w$ F$ g% M, \, F/ ?4 xstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, _8 L6 ^7 B3 Z0 y2 Cstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ e! M0 N8 E- W' R M) p static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 D( }. q, N- m7 m- ?4 y: Mstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 f. J! y: B3 x& ? x. ?6 Q+ Cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 J8 u& ]+ m- H9 `' P/ s1 N$ n" N
6 x5 a& G% W* L5 B1 d( T! H) S 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
' B; t" C, Q: m. u+ v# ? };
8 k- `' Z, r9 n, g( h9 |
" i; | w* X( _( \1 y) N /* F, G, H and I are basic MD5 functions.
*/
D, u% c$ j2 n" }% z. F#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" z @$ V: R1 u) T" k _, H #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" @; S) b4 h8 n; U& Z; n#define H(x, y, z) ((x) ^ (y) ^ (z))
- r$ E1 ]) `" P N#define I(x, y, z) ((y) ^ ((x) | (~z)))
3 |. J! @0 A1 v0 o) g9 M3 o7 U5 x
9 ^6 i* J1 I+ }0 ~/ l4 E- ^ /* ROTATE_LEFT rotates x left n bits.
*/
+ ~9 h+ U3 L0 a4 M#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) o% @6 P3 R& z& S$ Z: X7 `
5 n( m2 i, d( }9 A K! ^& o/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( g/ s m: X- C% X2 [6 ]7 G Rotation is separate from addition to prevent recomputation.
*/
9 W5 A5 B, M5 ?1 T7 M# V' K, Y#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) l7 V/ ]' P& |$ 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); \
}
" A$ W1 l) e$ K, i7 {. J6 E#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 v% u+ x* b6 z; y9 C#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; K3 q1 k7 _, ]9 W
F7 k! ]" [$ i! j( I: w0 P @/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 [: t0 F H" X Y7 avoid MD5Init (context)
/ E% L8 S' J& H- UMD5_CTX *context; /* context */
6 w" `/ d! I9 @( n8 c _{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
; i$ D, ]" C/ x( s, ]9 Q*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* {- H7 z3 m9 |, x! r' } }
7 h( c9 r9 R1 ]( U' ]) b# R
. a \$ S% S: p" K /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 p! z, Y; B7 D1 Fvoid MD5Update (context, input, inputLen)
7 [9 U+ y6 y: t: u# r9 uMD5_CTX *context; /* context */
/ A( h! G2 a( J+ J6 Q$ l {unsigned char *input; /* input block */
4 A9 H' }1 J9 X K4 n4 T& munsigned int inputLen; /* length of input block */
' y1 K; L$ \4 V* V: S$ t{
unsigned int i, index, partLen;
! b6 S$ s/ O$ h9 h7 O( \9 g
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 g& O' E$ W( L0 z
/* 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++程序


v4 p8 z3 t9 n( B7 H6 V
$ z7 ?! c) D2 _* e3 I' K
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-23 00:43 , Processed in 0.055048 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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