中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& E/ o) R" U& [& s" p#include "md5.h"
1 l6 w- P8 j$ w) x# u s
( E6 O) m2 E1 L$ c. C+ r /* Constants for MD5Transform routine.
*/
+ t! r4 o: g. }7 I6 s0 S1 H
. z% ]+ o: A3 u T6 Q1 {7 ^. Q }8 i
3 s3 J" G2 Q& _+ Z6 O; Q! ? #define S11 7
& L! T8 F: I. ?2 u, Z: C/ l& N#define S12 12
% e6 n8 U8 w( y6 ~ #define S13 17
6 G& F5 i+ r0 a( \#define S14 22
6 m* V4 E5 b+ G: X( v9 d+ N! n #define S21 5
8 G( }' _4 A& u9 U i! L( I/ ` #define S22 9
: e3 v0 f8 K, i$ {7 A #define S23 14
9 `5 ^( p. |) g( r# |#define S24 20
, F- h' e! @$ J2 g' m3 \% k #define S31 4
4 i2 B) t! l# i3 K% d #define S32 11
2 W7 C* e; e5 F8 i' ]& f& ~#define S33 16
( s$ v) G$ N# n5 c#define S34 23
6 [. ]' f# F" ?- q7 K' O$ k #define S41 6
4 @) y1 D' u$ }* q0 A$ }: b#define S42 10
( O9 g8 {/ G3 W O3 o6 X' P#define S43 15
' u& i% V7 W2 `0 p( f0 z#define S44 21
2 j) T0 t$ H$ X
! o! w" R8 z2 y9 Hstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
( _& Y+ F* I c7 Z static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
k! m: S# a3 l( n) [ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
; O0 @1 O+ O( Z8 W static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 i- z1 @2 y7 _+ A$ J# O& K. i: i3 ]static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
v9 ?* f# A, Z! o& E
- ^+ ?9 }% B; S- J$ P9 Rstatic 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
7 H1 L" |: ]- P* B4 V1 f };
5 X- z. n* V$ S, u
6 i$ a4 C- m0 u; g /* F, G, H and I are basic MD5 functions.
*/
) e+ f. y2 O) C( f#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- c1 l( F( K5 W' ^. S#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 o4 ^4 g, j, n #define H(x, y, z) ((x) ^ (y) ^ (z))
+ m: i% }9 k+ X0 W7 ?, W#define I(x, y, z) ((y) ^ ((x) | (~z)))
5 z5 L" T& U* G" {( n1 A' |
) f2 j) l/ z+ H+ f7 T w /* ROTATE_LEFT rotates x left n bits.
*/
, F4 [& a8 \" b% E1 x, c #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% i* B9 l8 W9 }6 H
- q7 U# f. o$ n3 e5 Q- M/ b6 l6 D, e /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& W% j" X; q! i5 j' B3 {3 d. ERotation is separate from addition to prevent recomputation.
*/
8 C- |3 z& N* x, b/ i#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 K9 }7 L- H. @- w. l K' H2 Q2 F#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" O. i% y" R( T/ @. t4 |1 p4 n #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
E: w' F M) ]( ^8 X6 Y% b #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ A% ^# j2 Q2 E7 N! B4 S# o
1 ~- l6 c) M' P5 J' A0 X+ Z/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( r5 e/ d8 V2 e, U. P( ?void MD5Init (context)
Z6 `8 b# N' P5 h; G) j$ X& sMD5_CTX *context; /* context */
k6 m' D% L6 y& d, l8 R{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. H! f9 x1 D3 I& @* B J" m4 I# ~) [*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 x9 z; e3 D4 q M& [" \ }
- F" [5 N0 M. ]; C! a" o
. b4 z6 T4 e# O( C /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
9 C/ ]! n7 q$ g8 ]( ?void MD5Update (context, input, inputLen)
) Q" a0 n5 ~8 r3 I: |MD5_CTX *context; /* context */
. q5 V+ C7 ^$ f+ V4 X, h9 K unsigned char *input; /* input block */
- h7 r* O$ }* P$ ]" g: i8 q- G4 A unsigned int inputLen; /* length of input block */
0 y7 `1 w! m6 _{
unsigned int i, index, partLen;
7 L! \) B; i# T3 k- b) [' X0 z# [7 S
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ h' t. ^$ U) q3 s5 ~+ p3 _
/* 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++程序


. @; ?( V3 p" Z9 C1 j( Y
& C% U3 p$ t& ^# b9 c! x" @3 j5 i
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-8 10:21 , Processed in 0.085842 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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