中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. Z5 ~, g: _* d7 d+ x0 L# m& O% Q #include "md5.h"
; Y" I' B" o0 r; P
$ \8 o: V' v( v2 I4 s5 r; V /* Constants for MD5Transform routine.
*/
* e- h( O6 e+ d) }" ]
4 s+ I/ f' v4 Y2 Y
1 {3 A% i: T, G#define S11 7
! x0 Y$ x; q5 o' p#define S12 12
" M" r2 B; _0 ^/ b5 r#define S13 17
6 I% V7 O8 x6 e/ Q4 z: s' V#define S14 22
9 l7 @9 b' Z6 N+ |& r#define S21 5
1 f$ N i, m2 R#define S22 9
/ J, i" T+ v. L& @; |5 e#define S23 14
4 E( p* e' G! q- Z: Y5 d: b #define S24 20
6 j$ D0 e/ i3 x8 Q& \ #define S31 4
3 F: C _$ u- J+ x6 _3 D d#define S32 11
7 g- r1 P# M' }- e. i #define S33 16
( @2 d6 ~; O. p* E' k #define S34 23
4 y+ v( a7 R+ g/ s$ D) y #define S41 6
2 o& u/ h! M7 ` [* a6 E #define S42 10
, U1 S1 K9 Y, m) Q0 ] #define S43 15
* d: g% k% a; [7 \#define S44 21
, b5 E5 l* u y- ~8 Q
/ p9 M4 @ @1 r" N3 I static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 U+ e8 y1 w t2 Y7 Gstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 H: R/ Z+ v, a* R1 h; b9 e3 _static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. k# l, q7 u3 d# y+ V. M {) { static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# |* u2 N" T+ {" e* k2 cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) i5 e8 N3 j5 m3 m: |) L+ ]
6 c N' d' I% d9 Y4 T6 A! `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
# N! d$ [8 U- n1 s/ `) t: R3 @};
2 M; d! l5 I# N! R" N% y5 [
, E* S8 I% g* p# k9 J/* F, G, H and I are basic MD5 functions.
*/
) _% @" o& J2 o7 j' P" k! b#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ r" t+ U& D6 {#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 K; c2 k7 f0 \ p. ?! e#define H(x, y, z) ((x) ^ (y) ^ (z))
8 y% m a! C! s4 i% u( k0 I#define I(x, y, z) ((y) ^ ((x) | (~z)))
% ^" d6 m, a2 l5 `4 w# Q1 A
2 z- j! d: Z: `# J, R/* ROTATE_LEFT rotates x left n bits.
*/
$ X1 Z3 g5 S( N- N* @1 j5 Q* h #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
9 \$ R; u' p3 N/ s
$ _0 x V! c; I- ]1 E /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ X H0 T6 g) n$ {0 E Rotation is separate from addition to prevent recomputation.
*/
# `5 ` P9 i5 O# Y3 t* }#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- g9 A* J6 g. D- b #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% P" M7 v& b Q( ~4 p3 T#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. L' O2 A1 E2 u #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. r9 o& [ \! n, x3 r6 |
9 o! c- h& U* O. q" t" X9 l4 v/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 L; K' s4 Y% `' y8 ?0 o7 \, v void MD5Init (context)
" ]6 t- \7 A8 G/ w+ F' A+ K7 z MD5_CTX *context; /* context */
% F" D6 t4 [7 Z% f! t4 f; G {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 p1 K& U2 u' Q6 v* F*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 W& P+ Z8 P3 @: s}
& L( |6 I4 W3 H" s, ]
s+ Z# E: ^4 I: [/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( _3 `+ \$ i0 C; v void MD5Update (context, input, inputLen)
8 i5 ~; P+ C5 X7 z7 v( n: o7 g MD5_CTX *context; /* context */
0 ~- w9 }- j4 g. _unsigned char *input; /* input block */
6 B P/ P: V+ n9 U unsigned int inputLen; /* length of input block */
: C+ _1 C+ c# {! f/ A0 D {
unsigned int i, index, partLen;
" H, d e3 g% t) \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 ?; H& G) x$ Z2 A1 `
/* 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++程序


0 I" r* W0 Q/ H( i+ b! r
9 j2 |# t, q \
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-1 23:27 , Processed in 0.106450 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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