中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 I' N( r: z) E: S6 Q8 A( \ #include "md5.h"
5 D' i0 i- Q# I) g, E
% _- X0 ?" y6 y/* Constants for MD5Transform routine.
*/
) F# n' N: P, p
6 z3 G X8 v7 \3 L X; ^0 f Z
& t+ \" J! W/ d- I #define S11 7
( N: h) G- i9 o, _6 D: H r1 C #define S12 12
2 ?0 @' z! o0 x: n6 | u#define S13 17
9 l0 V5 i6 z% \7 F #define S14 22
% {* V' j0 ?7 f# L #define S21 5
% ~- P9 u: I* T4 Q& x1 | #define S22 9
. w$ S, V( @. A' {/ m3 s- q% G7 K #define S23 14
% E, i! ~& l2 @( F1 [4 ? #define S24 20
* G: P! a/ m, t9 a#define S31 4
+ G) O( L8 u7 F: h#define S32 11
( f. h4 U9 T W& h' z; T- h #define S33 16
# w N1 P" i: Z# D#define S34 23
1 R Y+ ]2 g* K" u- j$ u [ #define S41 6
/ l+ |1 u$ b* |. t3 ~4 n#define S42 10
7 _& }" O6 e: E' c, r #define S43 15
5 G6 l6 t4 [6 f, c- a9 y2 n% z#define S44 21
) R! \& M; x/ M- }! `% c; s( G
* s6 N7 u7 d/ y2 M static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ B1 b4 c1 E; gstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! k0 o3 {2 J( F# W3 Y/ P: S+ G/ U0 fstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 ]/ Z6 l. a7 h r static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; g) ?! W5 O2 x/ g) X) A) B! j bstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 G; I6 C" `1 X' G3 c6 L Z t7 B5 y% l
" Q' Z8 O" F4 m# @9 B2 q7 hstatic 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 E; ~7 {* \. q" Y# E3 r};
3 c" a$ y$ Z5 Q% e
6 t: a7 e* m% s+ a/* F, G, H and I are basic MD5 functions.
*/
1 n* q3 H4 p. i, e #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" n! }8 B7 I( `7 S8 [( Z5 ?9 P* e #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; g% U9 |' j) i#define H(x, y, z) ((x) ^ (y) ^ (z))
1 u0 R$ P3 n8 x O8 c/ r' } #define I(x, y, z) ((y) ^ ((x) | (~z)))
6 [3 A% R* w! B6 Y/ \. O" q1 Y; e. X
) Q+ J+ P0 T, F, n /* ROTATE_LEFT rotates x left n bits.
*/
- ~1 w" x C9 A ~& n#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, r+ n' h; |* I" N
" _5 ?1 q( l& I" E5 t! @/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 i) ]- X( R3 d4 c1 D' fRotation is separate from addition to prevent recomputation.
*/
0 u' w6 U$ w6 I9 S' @0 g9 x1 |, B #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' E0 e* G+ A- T) p: l1 C8 I* 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); \
}
* D2 V$ e" O. \#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 p+ p! H- [1 Q7 I: c; j- K3 n& x#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 d" ?: T# q' m8 s5 j) \( T
1 K* o L! z9 \4 ?, S& A3 ^ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" u, j& L0 R* ]7 a& Yvoid MD5Init (context)
8 C1 W& Q1 _5 Q0 f9 u! gMD5_CTX *context; /* context */
/ U9 `" u" U0 ?- J4 n9 n{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
G( G' {$ Z4 H& p: y( J5 Q) k3 L9 V1 B* @*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ t" o7 [1 Z2 a2 Q7 X }
) ~( p8 T$ x: o6 S3 Z
) A/ c1 j- w# B. t4 W0 b /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 `9 m$ p5 I) o. O3 t void MD5Update (context, input, inputLen)
/ r/ b2 W$ W+ P* j; E/ _ MD5_CTX *context; /* context */
3 u, U5 r% S. Qunsigned char *input; /* input block */
% [: x# Q: s( }) z9 N2 w unsigned int inputLen; /* length of input block */
2 O" L1 R" R) g- J {
unsigned int i, index, partLen;
) w# {8 R2 `8 r3 J
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
4 ~$ M: f7 R; X& t
/* 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++程序


1 q$ H$ U; |9 k
h6 w% ~& n6 L8 w& u/ e4 q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-1 22:42 , Processed in 0.059263 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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