中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( l+ U6 u2 q% T$ o8 f F #include "md5.h"
, F1 r& S1 S, R0 U% e# q8 Z
. l j9 o$ R1 L3 y' T. D0 \ }/* Constants for MD5Transform routine.
*/
6 [! {( E/ z) p: M
& ~1 `2 R7 T: l! L. B O$ m
+ F8 q; q" S! m/ K# s9 b1 v0 n5 e#define S11 7
. Y2 y% i9 ~! ^ #define S12 12
7 i a7 A) ~+ g& t #define S13 17
( v9 M# B0 J/ a$ X/ }+ W# D' M#define S14 22
2 y0 G, h+ a0 d+ a2 C3 Y' E6 x/ J4 i1 S#define S21 5
: N& ]4 T% y0 y- @6 E" I7 L#define S22 9
) }' d! [# h: L1 ]+ f #define S23 14
) Y( i: x) O$ N0 n#define S24 20
9 f6 R: ^, f: D- i. c& }, r1 }% d#define S31 4
2 A' r8 a) r, y! x% n #define S32 11
+ Y! T7 h4 u _! A #define S33 16
2 Y& o* O; k- L3 v+ e6 @ #define S34 23
- h& i4 d, }! S; k#define S41 6
8 @/ G4 D6 |4 O; c5 K #define S42 10
3 J7 r5 ^# d; `, W3 `7 F #define S43 15
, D+ d" m+ V: ~% M #define S44 21
; g# H# {- ]# [! y: |! k
8 z/ G* t1 j/ ]- G: s# mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; M7 t# y% x6 M' r2 d- b) J+ Zstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& B% _& N6 j* f6 F1 o static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! ~( F2 Z8 u% t6 {& ~static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% k# b1 w W8 b1 S0 A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. f' Y/ r0 l7 A! H/ C
/ w" m- Q; C5 Z 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
/ B- Q: a) b2 Y0 }- G, e};
0 p8 \# F2 I* }, G$ [( e4 G1 v
1 w5 Q" L4 Q; L& _8 x9 @* N9 E /* F, G, H and I are basic MD5 functions.
*/
4 }' W) x- Z+ M( D#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 i3 u; I# @2 k! H#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" u+ [ E6 l4 @/ l#define H(x, y, z) ((x) ^ (y) ^ (z))
4 z y) z$ n+ B+ P8 D#define I(x, y, z) ((y) ^ ((x) | (~z)))
1 W& P& H/ s! z3 i" B6 n2 e! s
) ?7 t4 W5 X: ], f- } /* ROTATE_LEFT rotates x left n bits.
*/
7 i7 t8 _$ u9 I% ?#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( ]4 L, R1 [ @7 I7 A. W
: r* Z1 P% C9 M& u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& J4 Q! y0 n; N5 z7 }2 i' E% D Rotation is separate from addition to prevent recomputation.
*/
7 z, j9 Z) B7 i2 M( k; L. d/ R( w8 W #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 u; {4 B3 k4 d; X#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, s9 R8 \% R6 d+ w7 E! Z. j$ | #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
Z o0 n8 d+ `: l( G* 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); \
}
( R! `* h; o9 g& G# E8 M; X, z" S
4 _# L2 E7 Q' C7 d: _ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 H: c# p& p8 I, v$ @ void MD5Init (context)
" b# ~- a' M! K% } MD5_CTX *context; /* context */
3 p8 R4 C! F# H% [9 Z7 C. d a {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. H: s4 t) t$ B) j9 j6 n */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
[& a/ ?9 }- Q2 X2 g/ [2 Z }
1 r5 A( l2 n9 A: g# b0 t3 B' U
$ @1 b" x' m& d7 F- e /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 ]8 T( c. y X( ^void MD5Update (context, input, inputLen)
9 E2 v U# N [MD5_CTX *context; /* context */
i$ ]) g8 ]# w1 i4 m6 i unsigned char *input; /* input block */
3 [4 [- N7 T X0 t0 b/ Gunsigned int inputLen; /* length of input block */
! x9 o+ e# J1 @/ h9 t {
unsigned int i, index, partLen;
0 _% h( L. h5 w) U% e- g4 f, A, i
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
; f7 `& [) D+ t; ~ u& z
/* 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++程序


) Y& m# ^! i+ q( c8 u; E
5 F: N Z+ f1 o2 `( U. T& _) }
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-24 07:55 , Processed in 0.106426 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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