中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ f7 j; O9 o- w5 e8 l0 o" \, H#include "md5.h"
1 q# b( a# i1 I6 z( ]1 c
+ E$ \( m' L/ A" h8 q- @2 Y( U+ O) R /* Constants for MD5Transform routine.
*/
; R$ V! }: N A9 Y1 S
; ^$ {$ o* j9 ^6 I$ p
" l0 q/ j+ v5 V8 q0 ]/ x% H w#define S11 7
8 d* F, G. y$ p! I% \4 x#define S12 12
( U& v5 j0 w, v( E. P #define S13 17
F8 _9 w* ]* H; D2 K' T/ `7 l#define S14 22
! h; l/ r" E( N' E1 U( c #define S21 5
' m, g% @$ p1 `#define S22 9
5 k4 [. ?' [! a; b#define S23 14
j( p) w+ g: l- r #define S24 20
3 i' |- ^9 \ F8 q #define S31 4
- [% w9 f$ w* `5 g2 R #define S32 11
/ J/ L) H7 ?* K" E #define S33 16
8 y: [( F% J2 ?' }8 Z #define S34 23
0 P* B2 P9 }4 T$ K#define S41 6
, A) i/ \2 k- m5 C7 J- Q+ p& E #define S42 10
9 f6 i6 S2 i9 I% y' Z% J#define S43 15
3 g+ e9 i4 ]( G" i. P+ o0 w#define S44 21
& y% w1 V* T4 Y
% Z7 E3 b0 }6 a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 K; V8 p2 B4 d static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& \' n/ q M; a2 y% h% s static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 ]7 ]4 x$ E$ rstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 ?# B* x6 I: o4 G z static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 S; z; W. J# m4 z0 P) Q- n
' ^# @$ P, C- R% y 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
8 L5 |8 e8 h! |% J};
% J) y9 v3 D0 c2 ]
0 O! J' w! R( X5 |, C/* F, G, H and I are basic MD5 functions.
*/
5 c$ V$ U6 i5 W4 N. Q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
g* `5 ~9 j$ E. C6 ?* Y4 k #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# N3 {5 i: G# G/ B0 |- I#define H(x, y, z) ((x) ^ (y) ^ (z))
8 u; \) m; n- b* l7 |#define I(x, y, z) ((y) ^ ((x) | (~z)))
" t$ K: m* o: k
# q/ H$ |& m) W6 V( E/* ROTATE_LEFT rotates x left n bits.
*/
: F. Z8 @& j8 G% ^ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, k7 K" t L& o( B
4 D% J- n: I' |$ X8 X, L /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3 S- e/ T" A$ m1 z6 x* cRotation is separate from addition to prevent recomputation.
*/
! }& P4 y1 e4 F$ O* S 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); \
}
- ^0 e( ~1 g0 T+ G; {, 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); \
}
/ {2 T7 M0 I1 B#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# U3 B0 e+ |( N#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) Q- E- k( `/ p6 n
3 v4 e% K+ h1 T' o: Q. s/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# @. A! a: e3 ~% Q void MD5Init (context)
3 Z. V P1 ~3 }9 o2 SMD5_CTX *context; /* context */
5 y: z1 u( W: b+ G U7 ] {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) a3 L2 X, ~. v0 W/ s! K, o4 E*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 P' K; U3 g- ? }
, c+ ~* e' q, [
" s$ {6 w4 @5 o /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" j. i" W+ K' Z& _7 b. S' Zvoid MD5Update (context, input, inputLen)
& I e2 {* K. E$ r! w7 t4 p MD5_CTX *context; /* context */
6 r ]: P* t* Junsigned char *input; /* input block */
$ S1 Z: t5 t& yunsigned int inputLen; /* length of input block */
# }3 A9 ^7 L5 l. F3 \1 e{
unsigned int i, index, partLen;
# `+ q5 h0 ], }
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 x. x2 c) l. F; r4 H
/* 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++程序


+ G5 K' X# V. I$ Y, V
4 K4 w+ N! {- R) b- D6 F7 V) U7 u1 c
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-18 04:15 , Processed in 0.058343 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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