中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! t$ j% C; X( p- p* F% s#include "md5.h"
1 o; B( `6 X+ T
6 i3 k% e1 s; b3 M" ^7 z' E /* Constants for MD5Transform routine.
*/
' j" }. M+ {% a5 o- i& k0 D8 v
2 n' t& d2 @ N% p5 G4 y
! v6 j9 l Q2 b1 I4 b2 Q- G! f #define S11 7
' j1 n4 y- l# p/ T: G1 t' D #define S12 12
7 o6 J3 Z3 e; {#define S13 17
1 H7 x; l; D, j #define S14 22
0 k' a( }" j3 { V6 d3 z#define S21 5
$ g8 W" t% ~+ q, C, o' K/ Z4 D #define S22 9
R! C5 q8 @/ T! N1 `0 D1 F0 ]% ^2 A #define S23 14
! I: q5 T* J! \#define S24 20
. S2 N. U* i' |. l #define S31 4
' V, Z- i% s1 [#define S32 11
0 ]8 d* e- Q9 [. m7 m #define S33 16
1 S2 V7 \' x7 h4 k3 S* k #define S34 23
+ R8 J; a& C( w4 e#define S41 6
; m( l8 S9 ]0 F) f #define S42 10
$ S' I4 z4 ^: T/ T! l }6 U9 C4 `$ O8 A #define S43 15
4 Z- N- `( Q0 f1 N7 w #define S44 21
1 |2 _1 k$ [+ q( l: x) w* @
% [4 \2 W9 \/ j% Ustatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! Z; `/ y% `7 g' Xstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( x( j L$ l( i! W' l+ |8 o3 f! dstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) w) Q# s G# L# U3 ?static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& p3 w3 \* Q, R; W9 O- ?1 \$ s static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, X3 K7 y. n) K! _4 _$ t
$ e3 k) p2 M& H7 c8 G% w1 M( vstatic 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 Q, `! z7 ~/ w( {. N& \* x};
7 U3 \/ A" r0 A v8 D
' F% o% ]* _2 Q /* F, G, H and I are basic MD5 functions.
*/
" p3 e' ]- v' S! O4 L+ x# ` #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
5 e" a: z& l0 K #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& O( T4 ]; x" _2 k#define H(x, y, z) ((x) ^ (y) ^ (z))
8 c& Z# O5 V. q2 P- T+ P* ^" Z #define I(x, y, z) ((y) ^ ((x) | (~z)))
: _% f4 z( f8 ]) B
; e3 s0 j r$ { x( o8 G /* ROTATE_LEFT rotates x left n bits.
*/
6 \8 q. b! q' \#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 _& {+ }* J* }
$ d4 [+ ^9 o" m4 T0 M /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: l3 Y5 k- o- {* g4 M Rotation is separate from addition to prevent recomputation.
*/
0 q* K r$ n8 ]! a/ n #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 v9 H) U" J C; I# `#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% m5 ~$ o, j5 [" y4 @#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) \* m5 M' y$ O, k3 n) [+ k4 C#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, l- X+ v! O4 w
* S8 L) x/ t' C( Y7 s /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 B) H. Q! z0 x; z4 Tvoid MD5Init (context)
2 s! U! F+ L0 W. u( z8 ^) n MD5_CTX *context; /* context */
$ \# s7 t" N# j+ S `{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 h3 Z; k+ \3 Q z5 t( _*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 {7 S/ R$ ?# x0 L) w |! y& W( `}
' E8 e. S" W7 O0 }8 p! F" y
( H# h& |7 B1 X( [ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 v3 P7 M/ c1 y/ m2 Y void MD5Update (context, input, inputLen)
* l+ o; }1 y( H* W& Q! f! \MD5_CTX *context; /* context */
3 s1 @) P9 f; g; D% W, eunsigned char *input; /* input block */
* t4 g' A x, m) R) c2 c1 O unsigned int inputLen; /* length of input block */
4 z. C& A$ K- T0 n. Z% ?$ r {
unsigned int i, index, partLen;
6 ], C/ l1 T5 N2 v
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 h) [3 R9 G) J
/* 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++程序


1 n8 T* w& d2 ~* j$ f- m! ~; N6 B
: j( H: R) i" k
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-22 12:05 , Processed in 0.106424 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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