中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 O7 R4 R$ T; N#include "md5.h"
) x. N& d8 T6 a( _- x( H8 A+ r
6 V/ y7 L+ T \% T u, Z8 A$ u4 J7 j/* Constants for MD5Transform routine.
*/
% z9 v; O* @6 U" ?$ D
/ D1 }2 c0 j6 G/ u
$ v1 u% A* F/ S3 w- p' L5 @" ^ #define S11 7
* q. \- i" ~+ D4 H8 J: u* X#define S12 12
7 @& Q# J8 l$ g3 Y: r6 U#define S13 17
, Z# ?( {) \ E* g: N! S#define S14 22
1 H" S9 p7 V* r. m$ ] #define S21 5
' f8 B7 Y4 k8 X+ F p5 `% [ e#define S22 9
! \# T+ k; b8 y2 O' T( T #define S23 14
: g! C+ H1 Q# T8 D #define S24 20
! I' g5 S' W# g #define S31 4
% J1 E1 \: b0 @8 @2 r #define S32 11
& L, E5 R# B- O# B# z F #define S33 16
6 f# v. U1 }+ j#define S34 23
/ I+ U$ r+ D; q) L/ l) e! X& s+ X8 Y$ i#define S41 6
* M7 G: f- l! H+ w. Y#define S42 10
$ G+ ~# R7 W& z, J#define S43 15
3 B( |6 p8 `$ o s+ s#define S44 21
8 A' N+ x! ~$ B3 d- u+ H" t
; U [2 |; C: W" l( n+ `# xstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ @% ]0 U- C2 r& g static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ ]; H- b1 o6 z. b0 d" V" wstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 ~6 z0 @4 L+ Y3 ^. f" } static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) Y- E5 i5 M4 P! l) a static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 R/ F: g2 C2 n1 L9 g" v( b5 r
+ I6 R5 N% ^$ {! B+ C1 ` 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
! n8 V( C- Q# G7 u" ?};
3 d: J! B8 k/ k7 m9 I; n
3 m9 F/ P; l& x! g) M% ^ /* F, G, H and I are basic MD5 functions.
*/
% T1 X- h" b. _- H#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! K) ]* F9 y4 [( s. i3 A1 q" l5 l#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 ~- g8 W; W' B' q1 D$ @#define H(x, y, z) ((x) ^ (y) ^ (z))
0 e6 A$ X( g, e$ C% v2 l0 Z#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 d: A, ^, }8 ^; |; |
( Z5 C- @1 F# o0 T8 D' L /* ROTATE_LEFT rotates x left n bits.
*/
" C9 l X, z* ~#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
$ l$ n" @2 y9 }% q; |6 I
- M% G; j9 b7 l& }4 b /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! ]& f/ r2 q, c9 F' {5 ~4 VRotation is separate from addition to prevent recomputation.
*/
6 h/ a, O! C, {- @5 G#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' x$ ~! g$ R. f$ N) j3 m9 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); \
}
8 q4 b( Q2 N' w+ L #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' a+ ]8 v5 @& d3 | #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 m+ S1 N/ F4 z" r2 k! @) {; G
/ M) h) e6 e" u' C; ~6 ~( J$ T /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) J$ g6 R* c" i: p' Uvoid MD5Init (context)
* P& ]5 p3 Q, l8 T. z* P2 T MD5_CTX *context; /* context */
& v3 o( x# r. _, C/ v/ n{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 W4 n0 ^3 s* }2 b1 O# ?1 F6 G*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
: d0 I9 `# L* ^$ |: q6 a* H. G }
0 G f2 \1 Q% b* L
: T) {0 [3 Y/ \. p" F3 w/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 t- ?: }, m. I: Wvoid MD5Update (context, input, inputLen)
) ^ O9 V/ v5 R+ y) AMD5_CTX *context; /* context */
' E+ Y; P6 O5 K1 Punsigned char *input; /* input block */
5 P* o# t; b5 y' b) p3 D9 bunsigned int inputLen; /* length of input block */
: ]- v4 R$ H: d, ? T: U' u5 k{
unsigned int i, index, partLen;
# o$ a3 K! p; ^6 Z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% ]! M1 V, l6 l. U6 D 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++程序


- w: }3 ^9 ^6 {5 p1 d1 A# a0 }6 W
/ x1 }6 }( H" Y7 Y/ l
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-29 12:37 , Processed in 0.099379 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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