中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ P" K$ D% ?% m2 F# X+ ]- b; Y* ]5 l0 Q #include "md5.h"
) q4 V$ n, `5 d# {2 ~* \# R+ d
- Q7 o$ o6 e, N; G% n6 G /* Constants for MD5Transform routine.
*/
/ ^- O7 p" G( H0 G
, f( L( A+ D3 C' g
8 `1 h# `! s) B$ Q7 `6 i7 }#define S11 7
% p* n% v4 W2 P1 \2 B9 S#define S12 12
$ m2 [: l* K# ?" r #define S13 17
: R- Q8 h/ d, }0 n) P#define S14 22
- ^$ K m$ I: d4 N. x6 S( f5 a6 K#define S21 5
' N2 c% @4 G. G* q #define S22 9
4 F1 K0 U: X0 {- i7 X#define S23 14
- d1 ~1 Q; h9 {) \- G! ~#define S24 20
& ^5 V" S3 F7 j" e+ o) }2 t% A#define S31 4
1 U- |1 X+ @) t3 q0 @ #define S32 11
/ D+ k& |2 R5 ~6 l$ M- T #define S33 16
% N5 X! E/ M: i& b2 `: P- ~ #define S34 23
7 S/ ]3 ?8 I* e#define S41 6
+ B: P* ^8 x9 L B! T#define S42 10
1 I5 A* X- X0 {1 e( D* x1 |# m#define S43 15
8 |; i; a+ @4 ^" S, S" j# r#define S44 21
6 S) f+ y, [% I% a s
# d1 K+ i8 n; D6 t5 Estatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 X& J- g6 k8 U' E2 S( `6 Qstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 S% z8 f" ?! y: Vstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
0 v& ^6 W% {* C static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% w( i; Q9 ?. X) [/ ]+ A; L& s+ @ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
: \8 Q& f0 D+ I$ z
1 B. f2 Q" ^& Z3 U2 z0 xstatic 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
1 ?! E4 k7 {+ A; {0 A% q };
6 q n6 E: Z( g3 d2 `
8 z+ S, j! R& O/ ^, O" H+ ~ /* F, G, H and I are basic MD5 functions.
*/
: K; W# o7 i( j, d2 H K. z4 S #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. _9 g% {$ n3 q, W S4 b! l#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ N1 {7 n+ |* S, k1 [% t* V7 u#define H(x, y, z) ((x) ^ (y) ^ (z))
: x5 d# u' V8 w' b#define I(x, y, z) ((y) ^ ((x) | (~z)))
/ h7 v2 m9 |5 W% Y+ j* V$ ~
4 M7 Z0 `. r7 E( j7 Z- J /* ROTATE_LEFT rotates x left n bits.
*/
1 d8 S2 @% ^7 Y% h#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 Q: T) v& B: ]
0 m, a* _; y4 v' w /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. |+ ~* g: N2 O$ WRotation is separate from addition to prevent recomputation.
*/
- A' O' S* P# [0 d$ k; Y9 Q#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 m7 X' \% d# s' f4 ~& k #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 `6 t! l a S4 Q#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 s- j5 t3 z" J3 i2 z- [ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ b! _6 ~- t5 o( G& u' S$ J( H
" O; I y" ?/ B& H /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: B& d, f7 P/ }) Rvoid MD5Init (context)
# v$ g/ D. q; J8 i2 y6 R. X' [MD5_CTX *context; /* context */
; L2 u o2 _: l/ J {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
I2 z4 f* Y9 y! E, g7 v. `, e*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 Z# S( U$ Y. q; a4 G) d! O3 S8 j+ s }
9 s3 ` |: \3 {# Y H' v
* f% o6 ^2 s1 y9 n /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
0 F3 a& b& r, G; Z3 g7 ?2 x0 Hvoid MD5Update (context, input, inputLen)
8 k, t- v3 ?0 l- MMD5_CTX *context; /* context */
& W' y5 B+ N) }; i6 r8 }& ]unsigned char *input; /* input block */
p' v4 P% `) ~: Junsigned int inputLen; /* length of input block */
. G# ]* f, Z; ~' R {
unsigned int i, index, partLen;
6 W0 \. D* R4 ^7 D/ V% n
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: y) o0 D% }+ R) A+ }- e
/* 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++程序


; w* m( `8 b) T4 [$ W7 L
8 _0 K: c0 r) O2 v, ~0 e
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-10 04:25 , Processed in 0.062856 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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