中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ ?! S, M/ [; G3 X; h0 ~) ?0 J #include "md5.h"
+ O0 N1 N5 B% D9 w" F5 p+ Z! q. F, L
2 H$ v) k! Z1 S2 u" m5 k/ t8 T# o /* Constants for MD5Transform routine.
*/
/ s9 g1 i5 v; V' c+ i
3 Z; S7 t( M) b* r
$ D, G) ?4 G: k+ {- W5 f #define S11 7
& c d8 |- K, [7 p) Q; c1 Q5 d #define S12 12
& o& J4 T. k* o5 O J6 j; n #define S13 17
3 o$ ~1 R1 e9 | c' t#define S14 22
- O, S) g3 b: ?# g( x, h! n6 T" G$ k #define S21 5
9 D1 {, h/ }7 Q$ [#define S22 9
) a. h' Z# p. r, ?; t #define S23 14
4 L7 t+ F3 u2 \- ?! K #define S24 20
3 |- g# x# M; L- m L#define S31 4
. |: A* w. M) F/ A" M' Z1 B+ {#define S32 11
O2 }, E2 }: j/ ~#define S33 16
6 T4 L! d1 J" A! g3 N) h+ }#define S34 23
: O8 W+ M5 [3 a6 V8 a #define S41 6
6 J! E* {8 B, P$ e! \$ k1 B$ _- ]4 v#define S42 10
( R/ u( {9 O2 f7 @5 I8 q `& `8 z#define S43 15
6 J& J& ~1 C; |( P) Z#define S44 21
P0 y1 ^; u+ C! q0 a8 G h
. G" h: ]( j7 ?5 K) T' ?4 ~static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 d( c9 L9 b/ U* _+ ?" X# q; cstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ N" e- e# ?- O& V$ k static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 F% d' |0 g4 K, u' ^+ t( Hstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" p7 z* s: i) X) @- F6 m static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, q7 |. i1 A7 W: `- B( y
! K4 J" G7 o) Fstatic 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
4 Y* `) H. V5 u; r };
$ U1 Q9 s7 \+ c' D5 k
; c/ D R6 e6 @, `! m# D, B& l/* F, G, H and I are basic MD5 functions.
*/
; M6 z2 {9 Z5 `9 l" x, e #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, E& z- ]. K+ Z3 g) I6 m #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# n: h: ^3 X' ?( H: S% U #define H(x, y, z) ((x) ^ (y) ^ (z))
6 @6 I- b0 a0 m, u' r #define I(x, y, z) ((y) ^ ((x) | (~z)))
6 d, W( o) h! Y6 x6 f
* q V( E G$ }+ y+ |2 _ /* ROTATE_LEFT rotates x left n bits.
*/
; @* m. W% O( D+ k$ f0 A* a#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 u4 p" Y/ s- b6 O# b# e
3 T; ~1 R6 y* H( ~/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 M5 R9 D* }7 W8 i" g Rotation is separate from addition to prevent recomputation.
*/
1 p. a4 v: q1 }1 W #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 z: P. l4 t0 B' A" j #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* m' P4 ~8 ]# Y" ? |& h#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: {: P2 W2 A9 A* V) c4 r. O% s7 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); \
}
( C I. M$ s: B; Q
3 z' A' i: ~! K5 u! G" U# T2 ~3 s6 x/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 h! ~, H3 ^2 F void MD5Init (context)
3 g+ ~7 ~" ?: `+ ]3 |' @* xMD5_CTX *context; /* context */
- V2 H! \7 y2 d6 H& u( f- K6 [ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. \* ], x: q& @9 H% v1 F*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 D' M. p( W. Z4 R}
# I/ o x" g y' X
3 P. k1 o+ X! \* }5 s! M% L /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# ?. `8 [, [2 [" [2 \/ j void MD5Update (context, input, inputLen)
4 B/ _1 A2 i( x. K# a6 lMD5_CTX *context; /* context */
# U" `; ~# V! W& x6 u$ Z1 ]unsigned char *input; /* input block */
/ f- C o: C8 f* [unsigned int inputLen; /* length of input block */
5 S e- g& d3 `# G A& e{
unsigned int i, index, partLen;
. @7 k+ w! b" ^6 J, I: Q; g( k
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& h0 z# K4 U9 J% s4 n
/* 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 W+ ?! Q+ ?9 L) L: f: f
- x& W% b: z" g# ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-23 07:08 , Processed in 0.150373 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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