中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) x3 |; E6 V' x# @5 w #include "md5.h"
* M' ?) B h6 o1 s; r' k
: b0 P, F1 s _1 p/ y/* Constants for MD5Transform routine.
*/
6 L& a& O3 [5 Z
, t. h9 U& Q- x7 }0 G* g
' c* I; r5 c0 V' T. l9 ^* p#define S11 7
9 W/ R* ~9 N; J7 g6 d( |8 h) F% N# ^ #define S12 12
1 \6 n/ @0 b* W/ m( [ #define S13 17
4 s0 _( t6 \3 s( j7 t #define S14 22
) o1 u1 P( F* B! Y& z) p#define S21 5
: v8 u n0 x7 R#define S22 9
0 X% Z# ~6 |/ V0 W( e! u% \ #define S23 14
% q$ k3 x- E: t9 _5 \ #define S24 20
, `) D! I |& i* o #define S31 4
0 b+ I5 z' j2 e9 m) Z#define S32 11
; k, u& b+ |9 u #define S33 16
; X* ?3 {. i7 D+ ^9 y" w- e) J, E#define S34 23
, l# n# V9 c; N% I2 I( \ #define S41 6
" `# }+ B7 F. e, L) ^#define S42 10
* z% Q* T+ Q1 N- }3 \7 J0 q9 m9 u #define S43 15
) T- T3 E% |3 a5 G #define S44 21
5 H( J1 r' H9 k& n
g4 b$ u; I- T; G1 w, v& f6 @ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ M# k" C. _# Fstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
M" O. j- l4 K% C& }6 i# Z static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 G+ |" Y, {+ k+ b+ d# N, Z5 K static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
: |- s! c2 z/ r$ y4 N" E: A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+ @0 X9 }2 C! O" k
+ J$ h% X& z3 j, L$ dstatic 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
7 z2 f( w+ d# J( i" M };
& E$ v% O) ~" P
! y, d) d1 E2 ?5 @/ t" e; f6 T/* F, G, H and I are basic MD5 functions.
*/
8 x$ x! j5 @: T/ O #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. J5 s( E; y( D# j#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) \5 V8 l8 f5 D. Z" V4 {+ G: _ #define H(x, y, z) ((x) ^ (y) ^ (z))
# `. [- A; x/ |" r#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 m5 J' t8 y/ l+ Q7 |
6 p" h7 W# s- J" f# c/* ROTATE_LEFT rotates x left n bits.
*/
/ _% h* M( S+ B# Q; C #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
\! a( C) H; e+ b5 s& k3 X1 _
" r: d B2 I2 |4 D/ ]/ P/ o7 @ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
9 B) g2 ~# K# a! _ Rotation is separate from addition to prevent recomputation.
*/
' `7 b9 j2 L( [#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! L/ L5 q; y9 `; F- k/ 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); \
}
8 \" d* |3 s6 \#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 I( U) m) T# o) X2 |; F1 g+ j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 r0 y0 B/ t1 `5 V
+ X& f2 Z$ t; K! B, v2 T: R/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 U K1 k4 @# k7 x% t$ X& Vvoid MD5Init (context)
9 _' e4 \6 `3 w' i' w MD5_CTX *context; /* context */
% q1 w1 o5 H7 T+ B; `2 r{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. T2 j( i! \) `. { M*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
" M. T# d' _, f9 y* ~: n }
1 S$ {& y t% P$ {
W2 R, L0 Z' g- ]% _! o" R2 W+ i/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
% f# W% u' V6 Y! M* ]7 E void MD5Update (context, input, inputLen)
1 I* Z, ^6 p# A2 r5 ?7 \MD5_CTX *context; /* context */
5 v, D9 Q* L: y0 p1 \7 I$ sunsigned char *input; /* input block */
7 l, V n% m: R. @ ~# ]* n unsigned int inputLen; /* length of input block */
% V3 A1 |1 T! a p {
unsigned int i, index, partLen;
q* g2 S2 V$ w4 h! @
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ d! H: V& H( g+ L
/* 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++程序


! r$ k4 X. |' W8 [
& K# i) _3 s4 x7 k4 L' [4 x9 g
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-3 01:36 , Processed in 0.065146 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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