中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
6 v7 c! k& b, r, {( s$ c" \- q# }, w#include "md5.h"
4 _, C4 z6 _$ C4 q8 I9 c6 T
0 P4 [- B* ?) |, c, V- D0 l/* Constants for MD5Transform routine.
*/
. {# a% V3 l0 F. [' h1 l/ d
$ y$ E$ x: p) q k# N Y. r- Q
, I# u* ^% p2 J8 W+ i: m1 J#define S11 7
6 W+ E q: t4 B T) A/ n4 V#define S12 12
# ^1 H1 a+ U& ]! ~+ H9 s#define S13 17
: l% ~; g9 ~( Q( \' C8 z U& I#define S14 22
; I* U1 I6 w; _; u, M9 g: t f#define S21 5
@$ T }8 [7 l! N' _* f #define S22 9
8 H# Y) G2 s% _& ?0 z- A ~ #define S23 14
- {, D X: V1 ~. Z0 }6 \ #define S24 20
% Y/ A$ t% t3 x" i#define S31 4
+ }: D h8 N& K8 C" q5 k#define S32 11
8 |* u: `/ ^0 s9 q. F #define S33 16
& u) p4 O4 A3 [: c4 B#define S34 23
# X' Z6 U X3 Q( y/ {' B% ~+ | #define S41 6
5 Z u+ z5 ]4 Z; g8 I3 A #define S42 10
+ p" `# |6 p$ G; W+ J$ y4 w# {' K #define S43 15
: _( L9 F( `) i #define S44 21
$ \% J2 f( u9 \: q/ \; i+ X' [
9 c* f( V+ Y$ }, j static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 I" d0 P+ |: k7 J static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& D) s- \! G w, M0 A/ W: Y static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& o3 n0 J7 {8 A) S# H( b0 jstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* c. u0 T& P% ~7 U# W# d static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 Q3 W- U# F9 i4 M# N
% {; G% L6 ~9 X5 pstatic 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
) M4 S6 K) z2 H4 `/ L9 m5 @ };
/ i2 o5 L2 h* |; n
8 V7 y8 Q" X& E4 G# Q/* F, G, H and I are basic MD5 functions.
*/
2 n) s1 Z8 m" ^#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% i4 H) E- K, |5 I#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" S8 l7 P0 h4 l# b1 F #define H(x, y, z) ((x) ^ (y) ^ (z))
/ n; w* `/ h+ n5 v5 f4 p9 n. ` #define I(x, y, z) ((y) ^ ((x) | (~z)))
) k& F U7 Y; x/ w4 ?
' w& N6 Q- K! ^, Q4 U/* ROTATE_LEFT rotates x left n bits.
*/
; H' ~$ e* d( v$ }" E. C0 y3 U" ?$ c#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- S" P( B( F6 `1 N9 D0 ^0 [; e2 O
& x2 ^ E; ^3 L0 O) N+ r+ A ^/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; ?" [( V, U5 [' B" iRotation is separate from addition to prevent recomputation.
*/
2 a7 d9 b; S1 i( Y* C& B) j A#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( f7 K' P: y, p+ r1 E& 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); \
}
% i; @# N+ T0 U; O#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
w( H& [7 l3 k: j3 d: f- V#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ H$ Q- ]2 i- `: W
7 S* r' V7 L) P) _" u! t /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" c& ^/ C( [/ o) P/ n* c' svoid MD5Init (context)
2 p; {/ z9 X+ G9 r& \' K MD5_CTX *context; /* context */
) t; T: Z8 \3 K8 t: M! j4 s* j {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, i: m X+ W4 X8 A& I3 { */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 f6 G8 p5 v1 U$ W7 T3 s}
5 `- j! ?4 o, d4 ?& u
4 r/ Y9 p) D% x; v/ N /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 z {, a% [' d void MD5Update (context, input, inputLen)
7 e8 [% n) a6 N0 e IMD5_CTX *context; /* context */
8 h, m" }2 ?. y2 [1 Y2 Punsigned char *input; /* input block */
5 |8 e' x0 q; s3 \5 Y9 |unsigned int inputLen; /* length of input block */
: t k" `. t9 y& i. u {
unsigned int i, index, partLen;
: o$ U; O) U8 |$ J1 |, J4 V8 Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ d+ g0 t8 w+ @7 z6 K* T; h m
/* 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++程序


% ^6 o2 T0 x) |% @9 `* ^" m
" n2 {8 C# P- ^4 I
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-5 02:30 , Processed in 0.056280 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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