中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& Z/ t9 b3 X! i1 K3 r #include "md5.h"
* l3 H; m+ a' E+ C, {$ ~5 U
# `" \! P, L2 \: i3 a7 Y1 o1 Y/ t /* Constants for MD5Transform routine.
*/
^; i( F* T! [1 N! S
& m2 \/ `% s3 g7 p
( k8 C4 ]" d9 C3 Z6 j4 E7 e: A5 p#define S11 7
" J. C' i, p8 @#define S12 12
$ k; p) i# y8 l# B i8 C- f#define S13 17
5 @' w# t* m& @7 X9 t3 T#define S14 22
* H5 ]7 s, _/ @& W5 W#define S21 5
* s7 s- ]# @" h' I7 t$ k#define S22 9
: j4 _- z+ v6 u+ {" k, S #define S23 14
+ l2 A! q' ^* H/ t#define S24 20
$ H- f. K9 Q3 a& T4 m #define S31 4
/ M5 c) n" E4 V! f#define S32 11
& f+ Q0 `' {. D#define S33 16
9 v: w( E6 U2 r9 z8 n, C0 W4 ^#define S34 23
; O! a& Z+ Q y0 t* f0 ]+ L#define S41 6
% a; w# p0 u! Y0 U" U5 R #define S42 10
; K) ]7 E! _3 a# m2 H #define S43 15
, [: {4 i' g% Z+ ^8 v#define S44 21
" {! d0 @( h* m5 x o% w1 v; w4 ?
6 w L( A& J) _+ W; k6 xstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: n- u8 J& w$ O8 K5 ~static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 n' S& L; T" A+ A static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" ^. ]; h2 K; Jstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ @: l3 a' Z( m3 {static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' {. Y& h5 G: m! X! d$ ]6 z
# |7 L/ a$ z" |6 c7 N 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
6 {$ w- ?. k) S3 M};
, E2 Z. c5 H2 t- t0 T
! @+ {5 f0 ~: E; j9 |/* F, G, H and I are basic MD5 functions.
*/
1 y b. I, Z+ ~& U" [; ], m; q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 E. n5 E6 Y3 ^( K#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 j/ N7 X4 W( `! j3 D9 S #define H(x, y, z) ((x) ^ (y) ^ (z))
/ o3 i" g% ~* _! X#define I(x, y, z) ((y) ^ ((x) | (~z)))
, U. }: T5 T6 L# r2 N Q
: e* b+ z. R, A) V /* ROTATE_LEFT rotates x left n bits.
*/
- @+ O4 v% R! c' Z `8 L! K #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
* E6 Y: j' U, N: ]# }6 r/ F
9 F2 Y( l" J0 H4 ]/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 Q2 g% @9 \5 d# g Rotation is separate from addition to prevent recomputation.
*/
% w }& d- M# F( L- ?% V* u1 D#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* J2 T- x; S2 l! v: F2 O#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 `: h* R4 S a% n& p7 G #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 F. e m# a7 F( y0 f$ Z; H6 L7 O #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) P; [# H6 w, g6 f
- c( L. u2 R0 E$ }* J5 a" e /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
$ d- r; W0 ` ]void MD5Init (context)
$ Q5 F- ]! D9 O( X$ N8 C! ]- xMD5_CTX *context; /* context */
/ J- S* z, c. g{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ n9 P3 g- h9 a( S- D c# u: }*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* a! O9 V4 h% `! V/ o, _+ d& d}
{( w. `: P7 J! Y. t4 q6 Y- n
' c: B6 r' ]5 B9 G3 s1 l /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
V% ~: H3 ` M/ n: @ void MD5Update (context, input, inputLen)
4 E1 S1 r) y8 d1 X9 z2 JMD5_CTX *context; /* context */
" h# r" W; n" I; Zunsigned char *input; /* input block */
& P( n2 S6 c8 o7 \ unsigned int inputLen; /* length of input block */
$ k( A9 u$ R- A( @5 z7 M{
unsigned int i, index, partLen;
4 M. L0 _6 r) [; W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# Q+ x. {- j0 p# O, k( c8 _& i& W
/* 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++程序


8 l2 g3 {. F. k: t. R8 u6 [
" _8 t( c0 V5 T! P% r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-28 18:22 , Processed in 0.078652 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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