中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ i1 K* l3 Y5 M* S #include "md5.h"
6 T D2 {" p8 U
0 V* r% H4 f U+ J$ x /* Constants for MD5Transform routine.
*/
1 k* ~: e5 _& p5 w
. ] y( R8 y* \- v' y
/ z1 @9 u0 _0 H7 v% Q3 `& @# q #define S11 7
: K: d1 }. e4 y1 Q* |/ T) ? #define S12 12
7 d0 s3 E3 X2 w8 F- {+ { `7 `#define S13 17
( j: n) F- u& _ ~0 E9 V/ R #define S14 22
1 M4 I) y* ^( \1 i/ F#define S21 5
s% D" f* Q2 D( ^# Y F#define S22 9
3 a3 Y& `2 U3 k, M6 `1 P#define S23 14
% m' D$ D+ g5 }- k. C6 t#define S24 20
0 k' [6 v$ v) D#define S31 4
9 d5 i$ r% o, V #define S32 11
7 Z1 i4 X$ M! G1 F( ~* {; h% r #define S33 16
& o8 ^$ U$ I$ t. \ #define S34 23
! t) e7 V- |1 w! E3 M- G #define S41 6
7 S8 p. d; {2 Y( R/ G! y; q#define S42 10
) f4 t) H" U6 M7 s5 l; | #define S43 15
9 e/ x9 O, a# A #define S44 21
0 j- X7 b2 @% E7 m
* H$ t5 F& L8 F0 |9 u- ^5 jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 A5 j# F/ @4 b# W% \) y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% X! s- ~3 c5 {6 C( E7 f6 Q* G" M static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 Z! G- K, u+ e6 R* zstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
W/ f$ ?0 ]" C& }; [$ F static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! j. G; l5 c7 Z3 `6 ?
+ k6 u/ K# m V* p 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
# K7 n! |# S; ?2 A5 U% y };
5 n4 Z" d( A0 @' s3 b1 u# N
1 S7 g5 ~8 b; K- O& | /* F, G, H and I are basic MD5 functions.
*/
/ |) f3 U$ M7 X! F#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* f1 {2 A* O1 H2 b1 I0 N#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; e, y" U8 [6 \& Q2 n. ^- u #define H(x, y, z) ((x) ^ (y) ^ (z))
, ^* y# P% ?9 U; Q6 Z3 y. M #define I(x, y, z) ((y) ^ ((x) | (~z)))
" _, k% k* X% |: j$ E
5 W6 o; \. S( a# e /* ROTATE_LEFT rotates x left n bits.
*/
# {" [5 K1 |# a- L$ y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' v7 H) x% _6 J% {
( C. w- q4 e5 @* w! F/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 ~1 R3 o9 Y* s" G0 ? Rotation is separate from addition to prevent recomputation.
*/
& b% s t: d& m/ e#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( m/ V/ n* X- L1 ? #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, [2 x3 j2 k. R" M) ^/ {* \) s #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 b4 q4 y0 ]2 L% j, f6 J8 i9 c#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; l' K) Y6 X; r2 v3 w1 R
+ u8 u' p8 X; \3 z2 `* o7 I /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* K" i1 d4 J% uvoid MD5Init (context)
2 i) W: U( A+ b0 c MD5_CTX *context; /* context */
1 D c% U* W2 o% Q{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
; y: y1 P6 R, k+ \ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 k) v; x8 T, f4 N9 Y# @9 l; Z2 W }
8 Z8 T/ X1 C( ~6 R! N
1 E% X' _/ R. W$ Y /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' M& r5 o6 T; ?! ~ Q' I. N6 Nvoid MD5Update (context, input, inputLen)
5 D$ Y3 U; ^0 JMD5_CTX *context; /* context */
6 c7 a4 B' ^ o* Ounsigned char *input; /* input block */
7 k3 g% R! Q% J' B K& w) A% z0 p% m unsigned int inputLen; /* length of input block */
2 g1 \+ v7 c* z/ T: V/ c$ w" a{
unsigned int i, index, partLen;
# ~6 p$ c1 _' m2 i" M) k' P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 l6 J# p% h8 W* x7 U7 a: g
/* 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++程序


% x. T2 H1 W7 s. k# m
& o- D8 L) l7 ]5 x
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-2 08:10 , Processed in 0.070602 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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