中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# {2 | F# z( A( x% G [ #include "md5.h"
% [! m6 @) a2 e" @
2 s* L* g" }: v, S/* Constants for MD5Transform routine.
*/
& l6 h# _8 Y$ B7 ~# M' I6 A0 b
# X# {8 t, a/ A, x
$ d, b0 S- m$ _: Y) ] ^#define S11 7
7 z0 B2 n; V5 ~5 c& Z #define S12 12
1 T3 p; ?7 P8 W. Y3 ]( u) e #define S13 17
4 \+ i" o* a( O m7 Q6 D #define S14 22
2 R5 c) W6 q! R" \; ? #define S21 5
) Q S* m2 }5 e4 }+ ]8 B: p3 L#define S22 9
3 K) s- e |% b6 I3 |2 h #define S23 14
8 N2 n5 ^5 W- Z8 U #define S24 20
9 ~, B2 f i7 P. V) Q #define S31 4
( P7 p! d: e* D0 z% F d #define S32 11
: c- I3 N0 ?% ]#define S33 16
: z( x5 W3 ]4 w v/ _" k9 z$ M#define S34 23
) Q) k& e! x, L$ D q- S( r* { #define S41 6
T) |( s, Q' Z2 E) V#define S42 10
. i: R5 G! K* v#define S43 15
) s1 b. ]" _" t* W- S" x" | #define S44 21
b( B& _5 L5 c( J
% `9 S: C+ L% ~$ d8 J, E& Qstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ @& V. z3 p; V$ Z5 sstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
L1 |3 A9 S4 V$ a static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& ~) i, P* Q) }6 S7 R1 i; A. M. F7 i2 Nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ ]- G. o) ^! @- J! |5 ] static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( k9 l8 Z. ?- g! [& r- `
) u l. `/ r( g/ p; I' U" T. hstatic 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
8 u3 S% c% o- x- u* L3 o" ?};
7 \6 A ?, k+ |1 V0 [
8 @+ @+ X3 ]( E, n3 u/* F, G, H and I are basic MD5 functions.
*/
0 N6 {. k- a8 _. A#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. G7 Y5 W- c6 y#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! c" y) m& {: q. }8 ` #define H(x, y, z) ((x) ^ (y) ^ (z))
) j F" l8 E8 j" s5 q }#define I(x, y, z) ((y) ^ ((x) | (~z)))
A2 r0 o% ?( X
8 o" ~2 Q# L, b/* ROTATE_LEFT rotates x left n bits.
*/
3 X; O7 _) @- }4 u #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
0 ], |1 l! B( b$ ~2 D
$ j$ d/ O( ?+ B6 b; p4 D; f /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; O9 l( U) Q! V- E2 z8 l Rotation is separate from addition to prevent recomputation.
*/
/ O' ]" U7 ` o$ ` #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- k \& o9 i8 u! x" H; W#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
W9 w7 E0 ?7 _) D2 w9 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); \
}
% Y y, Q A2 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); \
}
6 T- {8 K' e7 g
4 ?2 w/ t* s& |' {& u/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
$ h: F0 e2 U( ]& o6 tvoid MD5Init (context)
; b+ A, M$ c5 w# e3 b% D0 s# XMD5_CTX *context; /* context */
$ [/ Y3 |+ D* u4 q {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& v0 L2 e5 V7 j# r9 P*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
' z. U' m" {5 N: u5 w4 j}
/ J- U% C9 f' P9 S
4 q0 H+ j: w0 p, j0 G /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 U# t# z+ h. |+ W2 U1 B9 [9 P' J( f void MD5Update (context, input, inputLen)
% m9 z3 R7 q/ X, D5 K MD5_CTX *context; /* context */
/ i3 ]3 u" I \( F; s' w. i+ Lunsigned char *input; /* input block */
( r, y, v9 {' ^5 P" \unsigned int inputLen; /* length of input block */
7 w' {: _! [: I. l* k. v {
unsigned int i, index, partLen;
, w* l: X0 U$ e( c; Q; B! s: d
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+ f2 b: \7 s; A
/* 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++程序


; O+ ]" |; T" b& P
, {: @8 c* P% [! w8 F: T6 ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 17:45 , Processed in 0.058606 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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