中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( t& P `5 G X #include "md5.h"
" T H, ^8 @. q9 m, K- Q3 U
% R3 [1 k/ a% d6 h0 n S5 ? /* Constants for MD5Transform routine.
*/
+ W: s$ _) i- G0 l! D4 w6 g
' ^7 c5 _$ i) b1 n# d! w
4 T+ z& [# d1 Z #define S11 7
: [* {% ^: u6 _1 H. [#define S12 12
* t) m" r. J9 t) d, `% b/ ^5 @#define S13 17
5 O' \0 } C8 {0 [4 W6 b( P% l& b/ X#define S14 22
9 q% U- f9 T; e#define S21 5
8 ^" q: f4 Z+ |% z! I: c9 s #define S22 9
# }" ~' j) q. ~ #define S23 14
; A6 \! b, F& Z$ W7 b K- a: F #define S24 20
# H1 y+ r0 p! t; Q#define S31 4
" q8 i! O3 W9 u+ k#define S32 11
! b0 t! B4 o) z) Y5 V#define S33 16
}# h. J4 Z" ~# w `#define S34 23
! w8 O8 e2 Z6 w5 ]#define S41 6
$ Q6 ?5 X9 A8 Y+ V #define S42 10
4 ?& ?& o7 v) V2 n2 k4 P) o. f #define S43 15
! }& ~& G% Y1 r7 L |1 F8 H #define S44 21
* E- E* k4 ]) }& e; n% s0 D" e
# N- g Q5 @# M2 K! i7 d* {+ [ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! f: g/ z* O5 s5 m0 E. [# N static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
5 w% ]" I6 V: U( q. H6 I: @5 q static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' X4 r/ c& q% K. {" U/ x gstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 ]. D, M/ L& |) L# Istatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 \: P* L! v; u4 a
4 W9 @$ O: ^2 c9 g I) J6 }0 t 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
; ^& P3 w" q+ ~. p; m: o. l };
3 ^7 Z7 U% f) _+ t* Z
: p3 p9 d7 m0 t: U" G# |/ h) c6 F6 k /* F, G, H and I are basic MD5 functions.
*/
1 H/ U2 `0 }" A5 l9 u! i0 l #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 a( Z3 X0 T y5 C6 H: ?#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, N4 i+ O: J6 i7 \' @: T$ Z #define H(x, y, z) ((x) ^ (y) ^ (z))
M: B# I: l3 v4 x#define I(x, y, z) ((y) ^ ((x) | (~z)))
+ W. z4 F8 y) ^! c& T; ~( K
* m& E! Z2 k# X6 X" i /* ROTATE_LEFT rotates x left n bits.
*/
- F z j" \, d#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
; h, x. R& R1 u0 v. n
' Z3 `/ G$ K# P2 H0 v5 I/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# V7 n+ p0 a7 W9 w: FRotation is separate from addition to prevent recomputation.
*/
* x0 K+ v t/ J* F #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; r/ L. l5 Q" y( c- X9 w/ n #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; \* K. w' A8 M0 w$ X #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
z6 w+ D; J& w+ B2 f #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, V6 g; a0 M: o6 @
) E' b1 l3 y1 A! [/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 B7 I: h- f/ E' @3 [, ? I void MD5Init (context)
% q% `3 W) a( [; d: j6 LMD5_CTX *context; /* context */
' `. r- ~2 @' \, c2 ~1 h( U# c{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 @, e1 D' B5 z+ \ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, p9 y& c- B" Z/ c# {, o9 K+ w }
+ y6 m- M2 a b) X
' w4 N! M) g" N; b6 W6 U/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; y. |; {- P( V& O0 Qvoid MD5Update (context, input, inputLen)
$ k/ K, W+ A) n. g1 l4 S4 @ MD5_CTX *context; /* context */
5 J8 E2 J$ Y; e$ o3 s3 h( G unsigned char *input; /* input block */
% a; S6 l1 a# J6 P& f5 N1 } unsigned int inputLen; /* length of input block */
+ d3 U- u) k, E% o1 I{
unsigned int i, index, partLen;
. Y3 r1 Q" S8 s2 E6 ~$ O# e
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* r8 `4 Q( T4 Z! U2 P) y3 x
/* 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++程序


0 v" M, c" j$ F6 x: h' ?4 f7 A
$ j1 |+ m* T) J* d( U3 M ^
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 17:56 , Processed in 0.263447 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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