中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" A4 k3 W$ Z. H( Z" i#include "md5.h"
+ I; a1 i7 O4 K% M' P+ ]& x# }
, q' u1 T" V7 M1 G, d6 o/ ~% p/* Constants for MD5Transform routine.
*/
2 C8 u) z1 P2 w' l7 ?* }; ~
1 }7 l6 J9 @6 D" ~) ]1 h" G
9 q9 x! k0 L* x1 ~& { #define S11 7
1 Y+ f6 Y' F' w#define S12 12
2 E- v* z, _0 h. N6 J #define S13 17
& Z8 s4 e; R6 A# l6 ?1 l1 M5 ~2 M; e#define S14 22
* ?$ g) J9 @) Z3 s# x0 z1 Z #define S21 5
6 M+ t: I: L' v( a: U#define S22 9
# K* f6 A/ Y. s# l; { #define S23 14
- Z& t( G4 H$ S |% {6 Z* N7 B- g #define S24 20
: c R+ |, [3 a#define S31 4
. d! `4 S& P% B#define S32 11
# }* G( ?5 X: t#define S33 16
) L$ ~) g1 {" b#define S34 23
- N# Y# @9 D4 n#define S41 6
) e1 I; N! l8 _. J7 I( D$ V5 y" _ #define S42 10
/ v( x# H" s$ i) G0 ^8 M6 k#define S43 15
6 N9 k1 O. R& ` #define S44 21
% X$ x1 C4 e9 @0 M& G
& D; s5 b8 E# ~* _9 x2 _5 q- l static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: Y4 {' u) C j; u4 g. sstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, D: K/ n4 R# b* r' o6 e static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ D+ {3 M' k9 F* T" F0 d static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ a/ b; z. `+ Ystatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! [6 B& b% o+ `! _( M6 c
- R" g& {: ~ @4 j7 f" e* 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
7 i# H& v. l2 L1 u" w6 u };
( {) R# p! S O0 y9 D
, Q8 R2 B [3 u* k" I /* F, G, H and I are basic MD5 functions.
*/
- a9 E$ a5 d7 G1 W#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. t! o i' W: _8 ^! f8 \- { #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 }. `& f4 ~2 _- @& y( p #define H(x, y, z) ((x) ^ (y) ^ (z))
+ f% J# r g4 e' L3 R4 {9 a# r, d7 @#define I(x, y, z) ((y) ^ ((x) | (~z)))
3 O0 t: m$ x4 l! Q2 }1 [
m/ ?6 Y' ?7 x; C5 z9 A1 D" K1 j) x /* ROTATE_LEFT rotates x left n bits.
*/
. l- W2 K9 G, x: v% s4 ], x4 H! P#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 M+ Z9 {' m V0 R. D" f
+ D8 H' x: `5 n8 E& k+ D/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& Z7 ~9 C+ \' N: V" K$ U' D1 F/ I Rotation is separate from addition to prevent recomputation.
*/
" j% R5 ^; y- N5 z; |#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! S5 K0 j2 g# o* e#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" z+ c9 j* l5 u3 j ~: H, V #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& Q4 K1 q+ J) R- W, S3 ~#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, c3 v6 H; T( J7 D3 F
' ~+ A3 }4 q$ N) e- @/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' c& b. o, D; n: H- B void MD5Init (context)
0 {' Q7 W; E- ^; [3 w( ?1 I) gMD5_CTX *context; /* context */
8 P4 t5 c* l- H- ?% q P{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& F* g3 c, g. |4 M */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 @( ~. J( X2 N5 t }
9 D, Q9 X1 B$ Y" N
: _ _& I1 V" N- x /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
: K2 @* V' @* k; I+ @ U3 W, ^* a. d6 o void MD5Update (context, input, inputLen)
2 w& N! [- `* Z+ K7 b; BMD5_CTX *context; /* context */
: F4 ~5 V6 \4 \: L2 U( R unsigned char *input; /* input block */
3 I) l ~0 R# v3 P: ]unsigned int inputLen; /* length of input block */
4 `, A4 d n& @" \. d4 O+ ] {
unsigned int i, index, partLen;
0 [: x& m; J& J2 Q* e1 n4 Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 n! r. H" Z/ H6 i: W- J; 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++程序


; G& k" ~; ^8 h% V- S
) r' L( u/ j6 W% S% d& ^
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-26 16:13 , Processed in 0.121288 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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