中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, D( |! m2 l5 X& t& Y+ P #include "md5.h"
5 ]8 {* W) s, M& O* ^
5 h/ u3 G7 x0 l }$ T7 o1 s /* Constants for MD5Transform routine.
*/
% s/ X1 ? }9 T( B' J0 U
& f% b7 P6 T* d, T
+ Z* w1 l. @- r$ W/ X" K#define S11 7
5 @/ ^& _" b: m# `% z#define S12 12
8 c4 g/ j* @4 X* K8 q #define S13 17
" m/ m y' y3 ?3 h' Q' d #define S14 22
& h- {0 g; J8 _#define S21 5
6 { l4 E t- u, h: | #define S22 9
4 B2 W+ o3 V7 ]/ X$ A #define S23 14
* N0 N5 A/ [- H) d #define S24 20
( `- e5 z1 w% q1 s5 L#define S31 4
6 Y4 S5 {; X* E' G#define S32 11
; ?6 K5 e* x6 O: Q3 V #define S33 16
9 G4 w! e# x+ D: V0 t #define S34 23
( G4 w, U4 f6 o8 X9 i #define S41 6
O( x; r. Z5 x# d6 E #define S42 10
) ]. G) _* @/ M0 j) H) o- A#define S43 15
4 I7 d3 s7 P$ `#define S44 21
. e* J% @; f; ^- E8 J4 M
@$ Z$ h3 M( W8 {# a6 O) y) i" d& a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: a/ k- e2 A6 j1 \8 E6 @ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) M* z: E/ u5 }3 jstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" P* ]) B! ^5 ~( C* N, ^& Estatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 J7 [( X+ A" C2 l" R static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 V- a! W1 f/ q" D
) U: x+ k" z' x8 w, ~9 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
0 V8 A F0 a+ o; g6 A$ w };
: n Z% o3 t1 b2 ~2 S
0 m9 f+ Q$ F/ g( `% Q7 } /* F, G, H and I are basic MD5 functions.
*/
, U/ F3 a J% }: l#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# c# t% j/ c0 m$ a- Q#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 G/ G- C! Y3 `3 p( M#define H(x, y, z) ((x) ^ (y) ^ (z))
! y" b; _& V/ [( p( S, A#define I(x, y, z) ((y) ^ ((x) | (~z)))
: z' `1 m' }% X$ \! W$ `; J
& x/ M A3 C0 d: }/* ROTATE_LEFT rotates x left n bits.
*/
) {5 X9 d$ K! I1 Z+ k$ t* V+ J #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ Z! V) ?3 L5 O8 j- c( H) j
: r+ L {6 J# [: ? /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: i) R5 @0 A$ U) } O! s$ h3 \7 wRotation is separate from addition to prevent recomputation.
*/
* `+ v: ^) m, e8 g3 [% x#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 h: H6 t6 l1 B+ U/ ?$ m/ s4 ? #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) S1 Y) k( t, }5 l0 Z; C #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 v( T' \$ c! ~- i4 O" P #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 Z3 C+ R5 _5 ^3 d- l4 H
, X! m9 p7 ?" r$ c /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 _% P3 d' o8 A( x" o$ [1 W8 Ovoid MD5Init (context)
2 F7 g, ~! t3 j# ?6 r: zMD5_CTX *context; /* context */
7 Q0 d! P8 Z+ o/ K4 {( i6 J {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# r$ U9 R( q9 Q( H9 C*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ K; e5 h7 M, ^4 ? }
1 h4 h: {7 f5 y( q0 o& m; Y
7 S. s' k( g0 S5 \; b+ ] /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. i. t& U& _/ N( x void MD5Update (context, input, inputLen)
8 f1 e3 t7 A. S1 C& ~/ _MD5_CTX *context; /* context */
. }: j* t2 S& c0 junsigned char *input; /* input block */
; _# `% {. F3 K+ k% e" |' A. W- s unsigned int inputLen; /* length of input block */
% Q; E% G+ O( t; b n- P2 w {
unsigned int i, index, partLen;
/ z5 h( \9 E* h7 h9 w' d' ^3 o
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% L; J2 w G4 l F
/* 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++程序


* r5 q0 w% g9 a7 O" R
% X$ v( } E2 f' c7 O3 e
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-28 06:35 , Processed in 0.060524 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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