中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 t% {0 \& a! c #include "md5.h"
9 `% I; v( U( z! i+ ^; ~- c
9 d/ {6 z @: G. _/ g" a/* Constants for MD5Transform routine.
*/
# u1 {" t- X2 z
% @* {+ N+ r) W; g3 i
: @7 I$ b/ T7 s* ?#define S11 7
& B' ?' |3 F7 p+ y. w" R#define S12 12
% q4 S- [* m, y# r3 m7 J#define S13 17
2 P7 P: u; ` A: _ s: G #define S14 22
+ a& C @4 w, `( [/ V7 D, b4 C# V#define S21 5
, y M* c/ {. N3 B# l#define S22 9
- r" r& n- [% O8 G#define S23 14
( F) Q4 M# B" u- t' g$ L3 }#define S24 20
; W* t" {* I( ~#define S31 4
$ a3 L$ y5 E+ B" x- c #define S32 11
* a6 Z* x; j f+ R. p5 X _5 h #define S33 16
6 O9 N: A# S9 e8 p #define S34 23
' J+ n, o* i4 u( T#define S41 6
4 a4 l) n0 G1 W3 \; c) R6 g #define S42 10
) I5 s$ I3 ~+ B: c/ m& q#define S43 15
0 x3 o& ?3 O! v" \' P, G/ T# u8 r#define S44 21
5 i6 R0 ^5 u m, C; b: H8 B7 y" x
6 A1 g6 W- O1 ?5 l static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 b9 S" i- c% e: b/ k1 T" sstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; o/ I; Q' R h( Q- t* Jstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 ^2 t5 P$ {( x' n$ ?9 a% Sstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 A) M9 L( }+ ^) z+ y static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. d4 b1 e- p+ K" {' E
4 \! A8 w4 ^6 K; { 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 \( [8 k! }! r7 B1 r9 L };
1 C+ x/ K4 \1 w: u3 e
9 ?( A4 l6 q) U/ h9 k& G/* F, G, H and I are basic MD5 functions.
*/
* e" |2 {% _/ L, ?. y0 V l #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' m/ H3 t; a" ?#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 B C4 x9 [! b: r, A) n( v; y. W, v #define H(x, y, z) ((x) ^ (y) ^ (z))
2 f7 \. d+ D2 J$ c#define I(x, y, z) ((y) ^ ((x) | (~z)))
/ \9 C4 o/ U, B4 a) I* E2 Y6 {( B4 F
+ D/ i& n! z: N. J/* ROTATE_LEFT rotates x left n bits.
*/
' u$ k1 ]% O6 e7 w #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( r$ l" k/ k: [8 S) W
3 y# ?) H- |! x$ @4 u2 _/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
, D. X: S# N3 w# S& FRotation is separate from addition to prevent recomputation.
*/
; h2 L" b6 R+ ~$ ^#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# V- R. t% d) C #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 ?* o2 V' r" n" 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); \
}
. l6 H& {, I: H) ]8 q6 o" X #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, ~- R, E2 D! l
* D+ G: [$ c" B7 N) @) B3 ~+ r. D /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& ?% c j( i3 ]: e0 E0 hvoid MD5Init (context)
, o6 B* \! Q q7 }- H$ J; H MD5_CTX *context; /* context */
8 b3 n/ u$ Q0 M' {& O+ F/ z1 P{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ y$ I1 z2 m. V z" K, N$ C9 z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 ?7 I% r: y: b1 x. h}
, L4 J9 Z+ |' ^/ \- q
+ _# g0 j9 }6 y# x /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' j2 c/ t, i; C% [+ w+ U4 Fvoid MD5Update (context, input, inputLen)
5 f: k, `2 r1 I. H5 P8 R. RMD5_CTX *context; /* context */
- ` W. k/ w$ [' I6 r* \: aunsigned char *input; /* input block */
; i) F8 g" c$ X- x9 cunsigned int inputLen; /* length of input block */
8 |/ G2 K. o+ ]2 d2 y- v! b: P8 C {
unsigned int i, index, partLen;
5 e7 @) ^9 o2 S9 W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 R# Y4 V" T# I9 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 `1 f* M+ w) m( t" K. S
1 c' [! i& ?0 R8 C y+ g1 j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-3 05:32 , Processed in 0.058406 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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