中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% F" Z+ B! k2 q1 p+ A2 d, S9 k: Q #include "md5.h"
$ `/ h" ~6 P9 |; k# W; Y0 T1 W
7 i1 p, Y& E9 _" { /* Constants for MD5Transform routine.
*/
, M+ D3 G* L/ K
; d$ ]# n. W- j
: e+ f2 v: E" s7 { b8 l #define S11 7
% p( m3 L- `, c& W! _- X3 z; B" [#define S12 12
4 x: A7 J; N# M, m- W#define S13 17
$ Q; [& \5 g8 [: s- t) ]0 l6 I) W#define S14 22
1 n+ B; Y' \8 P' r2 x b#define S21 5
# ~! w d6 j. X #define S22 9
1 U6 `2 [: U2 W( ~% d/ p, T' j( J #define S23 14
* W7 J9 E. {6 O3 R5 E2 O$ g #define S24 20
" s$ {/ n# U, R/ d* N#define S31 4
4 d9 G- |; v6 c+ s #define S32 11
2 w! P, x$ V0 O% s' P9 |8 T #define S33 16
, o5 G! d$ J5 q #define S34 23
- B, I3 _' P# z2 f#define S41 6
1 N" S; ^( y5 Q) D$ F#define S42 10
# }- s& B5 Q& X #define S43 15
4 R, E$ [# C$ N' C6 p #define S44 21
/ }6 ^& H D& D+ C m W. W3 h& h
5 h) ~6 g: t! K* m- q2 M8 u static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
( _+ x% i9 @- O( B6 e static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 c& B9 X+ Y5 |: i- d9 bstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
( }2 ] }8 M9 G/ n2 p$ Z& estatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# b7 c- u. O; wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! ^+ d1 {( _( ]$ K6 d
4 B% e: w# E& w0 L5 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
. ~/ \1 d7 X; C. i};
; v( Y) }% _1 X2 A
3 P6 _4 l N6 \& E6 u% |/* F, G, H and I are basic MD5 functions.
*/
; E; P/ l. [0 r4 C% ~ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
: N' D1 H5 `" Q) N, `# f& _' `#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; z7 T2 ]! y2 R! e/ J1 U: a#define H(x, y, z) ((x) ^ (y) ^ (z))
- `. ^ y1 V' g3 i #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 n- |. L. e; T2 e' {. _ Z% K$ E( y
. i8 i! i2 V# r8 O9 N$ ?/* ROTATE_LEFT rotates x left n bits.
*/
9 s& [" A K3 W6 o# E5 n3 b' g #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, B R1 ^9 V. X9 D# T; {5 J) S8 y" U
6 H* J: x9 S, W! X9 V! |/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ @- e9 S3 s" I& A/ ]Rotation is separate from addition to prevent recomputation.
*/
% M) z0 Q6 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); \
}
+ k' K- g2 E* I& G, ]#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, U: X: [4 w( x. ^' j6 F6 P; ]#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 S4 Y' T8 Q: Y, v" J2 A% ^ #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 ^, e# k8 e4 J3 p' O
$ G$ g! X2 f5 Q! a /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 S$ g) R2 [; ]1 hvoid MD5Init (context)
. W2 u$ w' c( b0 h; ?+ g! JMD5_CTX *context; /* context */
' d# H! ?$ R0 t [, a' D {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 C5 G* L& i- L$ k! [7 x( \ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# I" r8 h( a3 U5 K' Q" n8 \ }
2 w: i% a" j [3 p& M, D- S
4 Z& @/ s0 j% Y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! _% R1 A! T" M+ k/ c0 a0 J6 lvoid MD5Update (context, input, inputLen)
$ {/ ~1 ~7 |/ G/ Q6 U MD5_CTX *context; /* context */
0 o/ Z3 ]( n" n" Uunsigned char *input; /* input block */
4 M- m/ X) \. @1 Ounsigned int inputLen; /* length of input block */
) P. x n' V0 @{
unsigned int i, index, partLen;
+ t, ?( i C6 d# m9 \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 x. W+ k4 E- A
/* 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++程序


$ c5 h! W& @7 D+ o
& Y0 q( R- N9 S `
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-22 13:45 , Processed in 0.081332 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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