中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' z7 o& X/ v" x* e7 Z" r+ ]1 P4 t #include "md5.h"
1 g9 x* `; B. r8 l7 F# W3 y4 |
2 P2 B% F4 Y* Y+ S! r# ~8 c /* Constants for MD5Transform routine.
*/
3 F7 n$ o( a2 i; D
+ \( Z( j- L5 T5 Y/ p8 l
' f' i( d: ]; Q/ I8 g0 Z! M, u #define S11 7
( u$ }! Q. Z& w! y+ d% F #define S12 12
5 ?& s1 t& z1 G; p# w- C2 Y3 B# U" { #define S13 17
; h7 R( {8 v1 O% P) t. ] #define S14 22
% c& I4 `0 _& C. I' i& E6 _ #define S21 5
U! [% P7 ^- N$ X" d" } #define S22 9
- ^; |; p6 w _" d #define S23 14
6 A) K0 d% \1 b3 Z5 a; r, W( j" a) ?#define S24 20
6 J. i7 G+ p5 m9 L- ] #define S31 4
2 K- O. f2 @5 v/ I0 }5 f#define S32 11
$ G/ [: U1 V# s7 |: s#define S33 16
4 S, Y+ W* L% \# @. [/ q #define S34 23
+ A$ ^7 J X% P) u, z" B0 x6 ` #define S41 6
$ [% x1 P3 \: b) r: W, g( Y) s, J#define S42 10
! `3 g! D$ h6 Z: u#define S43 15
6 D- C9 `% {: B: K+ m) t) ?7 @ #define S44 21
s. R n* r/ f
7 L$ ~" _2 a4 H0 o2 n" k: o# _ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) M! H8 k4 M W8 m! kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
Z# b' c$ ~. W; j6 P' A static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' _- u, x& {$ F static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" V4 K+ |7 O- E& s8 Wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) y8 {+ [* j$ s7 P0 Y+ i9 }
; \ v% n+ o1 L& y" G& |1 O 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
9 ? u1 i- z* ?" u( G: }};
1 ?) o. A, Q, U/ U% v9 L
/ b) T5 h) U: {7 r/ u/* F, G, H and I are basic MD5 functions.
*/
, j$ v3 ~3 e8 h#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* m1 a9 z9 N* f' U! e2 U #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 N9 ?( L7 L$ M \5 x+ G" ? #define H(x, y, z) ((x) ^ (y) ^ (z))
5 O C' ] q- W: W' \. d #define I(x, y, z) ((y) ^ ((x) | (~z)))
. n4 i+ Q3 J p/ p' B+ x
8 I/ H/ p3 Y5 E5 |; P* ^1 Y4 ? /* ROTATE_LEFT rotates x left n bits.
*/
7 b/ ]! u" ~2 d( X8 s# |( ? #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ ~# G9 C/ C G. J: \$ \: A. ?' C
# X% z+ Q2 f% V' B; P /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 ~9 N/ v3 q7 M8 S% ^) ] Rotation is separate from addition to prevent recomputation.
*/
) x% D+ B6 a2 s" [1 S4 ` }5 Q( v #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 n6 x4 ]# m( K- L/ S6 b#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 E$ ~' K6 J8 U #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! g j# o: ^* X0 h; D& k( c* q#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ Z2 I6 K3 {3 \5 n, C, a6 \
) B. f8 E l9 P6 ` /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 e9 ^! V2 F( t3 Q o+ M: q2 B void MD5Init (context)
# V0 r' q3 P" | MD5_CTX *context; /* context */
/ f+ q0 b3 K# k, h% c {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ o% D1 }# ?8 n8 {1 u( U/ i*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ E1 u' p) v+ C5 j% D}
6 Z& B# X- w3 N! p& \. j2 D6 c
3 s* E# Q: L/ M7 b/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" c I" [: q6 D" J+ ^2 pvoid MD5Update (context, input, inputLen)
7 J' K$ `2 m7 C2 f MD5_CTX *context; /* context */
Z6 h4 A& k6 @9 V' n6 `. V unsigned char *input; /* input block */
5 U5 B+ f0 q7 ^- m7 ]* C4 [ unsigned int inputLen; /* length of input block */
" J- k* C( g; d+ D {
unsigned int i, index, partLen;
4 W* J1 q. N% x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: s- T9 h+ ?* M2 I. w3 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++程序


9 h4 \3 D8 Q. A, a- g8 G) f
% ~/ e2 {, C! h/ V% ?8 J
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-23 12:50 , Processed in 0.057692 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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