中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 N" r0 Q! U* e( p; Q3 J+ P#include "md5.h"
1 ?3 ]% e/ T6 N% C
& K4 Y- r; v0 \7 R+ o1 l4 ?! Z7 _/* Constants for MD5Transform routine.
*/
( J6 t4 J Z& t3 k% ^7 P' z
9 k7 a! u2 v. J9 f/ q% n4 a& }) P
- a8 Y0 @ W% W" p #define S11 7
. o3 e. @: e7 m0 w5 U, y0 N#define S12 12
& a+ b5 Y( v" z4 ?: v, a#define S13 17
6 r$ B" {+ q3 [8 S #define S14 22
, r7 P& F( ^1 C7 V" H. X( u1 J$ A#define S21 5
8 o! L" @* \$ M, R0 ]" F#define S22 9
# V9 A5 C* v0 u1 L #define S23 14
9 c2 v8 V0 o1 t3 O( r: s4 H$ E#define S24 20
4 J, u0 h8 l3 f* O, ^( ~: U #define S31 4
/ v) c* t( l. G; [( G( W8 a#define S32 11
+ d& x# A; U5 c, c& j" w #define S33 16
- q8 @& v. m2 v#define S34 23
" A. y: [4 ~, {6 ?5 g. H, D' T0 a! Y#define S41 6
2 e0 u3 Q' k# Z5 l3 Q #define S42 10
# Y, T3 D! r. q8 s& [% d#define S43 15
% j9 e9 L, V& E#define S44 21
: U. I6 S! d0 v: }9 T' {
* _$ H% p# ^: e% R+ n! ?static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 @+ t8 H3 ^2 C3 }% k& p5 \1 Dstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 d7 o$ b" A' J$ J, \ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% I, G3 O; X+ f static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 ~' m- k" _% q# J1 h static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
; S2 {2 P9 e9 z, G2 A* |) ~4 i
* U6 Q$ J1 i0 B! ]" N0 J 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
) z$ f- T" Q5 J5 g/ a* G };
' m$ O; a! h5 ` }
2 y8 d( [+ w! d! {; n0 } /* F, G, H and I are basic MD5 functions.
*/
* |3 |8 B: J6 r9 S9 C, Q/ A: V. i#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! Z. i$ }5 y2 n" b#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( ?3 F: A0 \! u, p: {#define H(x, y, z) ((x) ^ (y) ^ (z))
& e$ X- w! p+ L2 b8 l+ E5 f; L$ S#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 j# {( C5 K9 d! l! p
% y; d V+ y; ]6 J! M) `3 E; m( T /* ROTATE_LEFT rotates x left n bits.
*/
! |4 j$ Y$ }7 e: H0 s#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" X7 e5 R3 q# P4 e
8 C6 B. C# D) e/ s7 m6 A1 n% F/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ S& z7 C; G& nRotation is separate from addition to prevent recomputation.
*/
$ ?5 ^6 K; l* v, Y6 K$ M/ G #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# k; B T) _9 M0 q" h& w2 I. 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); \
}
E2 F- A6 a9 q e, E# j% d# a#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* p O* u" b& O#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) H7 ^5 {# V8 @
' _3 x/ p: n5 A* n /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( O$ M( ~. p1 c; g. F9 r void MD5Init (context)
0 y4 I( g8 d2 s1 IMD5_CTX *context; /* context */
8 x3 D! ^( {: Y) H$ }' z# ]. n{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 o0 _2 m5 |) Q5 D7 Y */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, X2 W- J, y/ `" @: m- ^ ]4 F}
( k' f: v! q9 Z ]: e4 I$ P
( r. o& D; t' W/ U/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. ]8 E" f1 h7 X" _& ` void MD5Update (context, input, inputLen)
! N9 h! @% ]3 F1 l& C& |MD5_CTX *context; /* context */
. g% Q+ a0 V) S8 x H; F; |7 punsigned char *input; /* input block */
. Q# c! l4 l! J1 F& v unsigned int inputLen; /* length of input block */
$ \3 ~4 v) B/ |{
unsigned int i, index, partLen;
% E! ~7 a# s) t! \6 y7 N
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( ~+ j5 I( k& Z+ p
/* 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++程序


" e7 `2 w/ y8 A
5 S z: w+ p' V1 n
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 05:12 , Processed in 0.066746 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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