中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. j# g* h( T# ^# J #include "md5.h"
$ q. H) U3 p, J" Z( O9 \1 Y x8 o0 L
6 A5 p; B: C2 a; U( A /* Constants for MD5Transform routine.
*/
: L3 F* B& P U: f: _& N4 u6 z
# [" f7 |: [. u- E
# G# a7 d+ E& d( E; d$ H/ D#define S11 7
9 f/ X" W' G% k8 | #define S12 12
. a; j# `' ]5 e. _$ m& E+ r* R#define S13 17
O. a1 [- A! i- F* f# D4 O7 W #define S14 22
- B2 j& u" v: M- z' n#define S21 5
( W0 r& ]( o$ g$ _; c% o4 P#define S22 9
7 Z! B! Y' E1 ^9 e#define S23 14
3 n' N" ~: {* M; T#define S24 20
F+ j# e% F: F7 o' V#define S31 4
, e; ?8 Y/ d7 s8 F1 b$ \6 K #define S32 11
8 p4 E! C2 S' e& O. b#define S33 16
' G' Z+ ^2 V1 f9 \9 R( E9 C#define S34 23
- @, p' ?; w* ? #define S41 6
, F5 J* M9 E1 o- _" }6 |! b5 f #define S42 10
) x, M/ b; U+ e$ D#define S43 15
+ p% V% N. _6 \#define S44 21
% T) `& u! x; H
( c |: G8 Y. w* Dstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ j$ a6 O( y, p. vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ q6 Y5 P" L8 v3 M8 xstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" [: v& s- z) t; m j8 F% l% qstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
F a# Z/ `+ r+ I: b% j5 O static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 U. k1 T. g8 b% q V& v
8 b4 w; @; U* G1 m2 Nstatic 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
d: K; J( ?5 {& n' N/ H% S};
7 ^7 Q! h$ ]/ S
, p: n9 B9 @) n" `/* F, G, H and I are basic MD5 functions.
*/
* y+ v+ c, e# \3 C1 A3 v) r! m, \' i#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
: A, L3 w/ O, u: d#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ Y6 K3 i7 m3 z #define H(x, y, z) ((x) ^ (y) ^ (z))
# C8 O1 A4 Q6 v. r% W#define I(x, y, z) ((y) ^ ((x) | (~z)))
* H8 N1 n% i, ^; ~0 E7 M) e- D0 L
6 i0 c' L* D- [) d4 Q/* ROTATE_LEFT rotates x left n bits.
*/
' F/ q' c- n5 x4 ~# K& A#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% K* C/ D( c( ] ]# r0 b# p
& V, p; b- R$ T+ | /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. ?1 k: M6 g1 ]3 l9 |7 c8 E5 Y Rotation is separate from addition to prevent recomputation.
*/
4 h0 {* u7 K0 `+ U' V' h, ~! k#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, h: w0 E' F- Q6 M9 w #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- F# O- t# ?* u% z( s#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" x: G( K% ?8 F& d#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 T' T! o) `# \) ?
. V0 k, S" M; k, q& _, e* `" i/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" K0 d4 b& S* P% s: Y& |4 H! xvoid MD5Init (context)
0 t7 }8 f# g' E3 {6 {$ T3 R9 Q MD5_CTX *context; /* context */
4 |& S; y/ S [% X/ A3 [" A# \0 `{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 I) {6 s6 n( T ?3 S4 C */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 _/ v" ]: B# X2 }8 k/ r5 n}
" V1 P- C7 q4 y6 V: M
- p, c# E' p$ Y5 ?. Z. |1 R/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; J5 {+ d' U+ R7 I void MD5Update (context, input, inputLen)
: P5 q6 u9 [/ M7 `7 R5 `# ?MD5_CTX *context; /* context */
" z4 t' Z* q5 e1 ~8 @ unsigned char *input; /* input block */
- T4 }; Y# v' x" e$ Sunsigned int inputLen; /* length of input block */
5 p: {5 t# ^$ {1 q6 E {
unsigned int i, index, partLen;
+ f- F/ }( ]) K& {9 r/ I P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' b( |2 `3 B; Z8 Z! y
/* 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++程序


' q6 H$ p; G, r$ X% M$ O5 M
% k: @5 {. L# k3 I
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-27 07:03 , Processed in 0.055500 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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