中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# y' e, Q; n6 h! s#include "md5.h"
; r1 M7 ~2 v8 k3 `
( l4 j. @/ G4 ?1 v/* Constants for MD5Transform routine.
*/
1 l4 a/ Q3 ]1 |( V7 t, r5 d8 {3 q- N
, N3 \# K! T6 r4 f$ c; |
! D8 c* t; P! r- g2 D: j+ b #define S11 7
8 |/ G. `5 F5 d3 d; t9 C( ^ #define S12 12
' A5 m0 T* A& u1 g& J+ ?( y- E #define S13 17
# c2 u8 c- N; g8 E2 m) }! z#define S14 22
( L# f/ h7 o& a: E) E- ~, K+ [#define S21 5
& F; E; k4 a& L- B& ]0 k" Q #define S22 9
7 R v- G- ]) K# ] #define S23 14
( u( t' Z, F' e( X" d3 \4 ~; q& G#define S24 20
8 S1 S$ H8 m. Y D #define S31 4
8 h; v) I+ {. J) K( m #define S32 11
s& ?& M& V- ~#define S33 16
3 k3 E# F2 s( O* @ #define S34 23
( P' r0 l6 D7 i9 t$ h: @#define S41 6
6 c7 }6 @) z( X, L$ g% B; H #define S42 10
8 `1 _, j: C+ ~! z* v* i#define S43 15
# N+ T9 b3 ~! y8 E% O! w: j #define S44 21
: W$ ^9 s9 ]1 Q9 @+ V% m- Q
+ R# B0 U+ O' L, c" lstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 M) q$ p! K3 R1 U static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
" k. g# k$ u3 w6 A& ]+ x4 r% W static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& ^6 r9 w; s8 n) p# N0 D# n9 pstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ {, p' f: C; I/ a$ B$ X static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 e# ^: `; P1 s; E
2 E' R7 G- Y- W6 x1 dstatic 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
) m( I9 @5 ]+ M& O9 E };
! h$ c2 F3 I6 o3 ?" U7 K8 d
) E3 y7 C0 Q. M0 L8 w /* F, G, H and I are basic MD5 functions.
*/
% p4 k. |4 V+ ]2 A. ?8 Q" Y #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 f+ F, H4 ]9 f3 L #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
. Q' s n7 G$ y6 w. _ #define H(x, y, z) ((x) ^ (y) ^ (z))
2 x: n; D& y, m5 w #define I(x, y, z) ((y) ^ ((x) | (~z)))
( e( [- c- |3 F, U9 x4 Q! _
4 f9 v% J0 ^! c0 Y, m /* ROTATE_LEFT rotates x left n bits.
*/
- `# W1 |% \. Z" s#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 ?% s |8 ?* I- L1 Y4 t J/ n
* O3 d$ T3 W1 F2 J3 T /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ s) c4 y" F; F8 L( K. i Rotation is separate from addition to prevent recomputation.
*/
: i0 C" a, z* a8 O, [' l#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 u% L. W! {5 D9 `! i# Q, j #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ h9 a9 Q% X- ~6 I& K2 B" q& ]#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# u6 ^( ~. \+ @# 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); \
}
4 } q" ~0 ~+ G* r' R
/ `; y6 Z+ m; F C! p/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ I( V8 C; E3 e: ?0 C' W8 l void MD5Init (context)
, F+ o1 b2 g! q# Z. x5 D& a MD5_CTX *context; /* context */
+ v N+ x2 z; _$ s p/ T- `{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
7 X9 y6 A# Z$ q */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 R' o& d2 P4 {; T" V; _}
: Y9 M" v% }& X& ?. K. y @2 Z: Y
E- q. b) p/ C. ?4 d /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
% c) F$ P) m7 o' X$ |* E2 Yvoid MD5Update (context, input, inputLen)
& e! S! G2 }, I. L. n4 I9 AMD5_CTX *context; /* context */
' ^! l& z; M/ j3 }' \unsigned char *input; /* input block */
( j! l5 S; J& }% H9 k4 v2 o* ?unsigned int inputLen; /* length of input block */
, d( c! U/ p6 ^+ ]6 g4 K {
unsigned int i, index, partLen;
! L$ w* P: G& h, r4 s. Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, ?7 o% c0 k8 P: m
/* 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++程序


+ X1 V- _8 r4 \
9 K3 N$ l2 ~" @, D/ ?4 W+ P& |
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-21 22:06 , Processed in 0.089295 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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