中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% |4 } Y$ e @, p4 m #include "md5.h"
/ u4 Y. s2 u f' h5 t( K; P
$ y# v2 L1 V( S/* Constants for MD5Transform routine.
*/
, C4 }2 @2 V. H! v: Y
6 M, I/ p$ H8 b- c
9 B: v' g# e! G) i #define S11 7
" a) ?; F1 ~. C/ D4 k' G#define S12 12
2 \% M; Y" o4 _7 Y" G' e#define S13 17
0 z# ~- o& A; {9 P% U#define S14 22
`# d) C+ m3 v6 u5 L5 @#define S21 5
1 Q: ]: [6 S- u. F0 l #define S22 9
! P* X9 r* \2 O: z#define S23 14
7 B% F% Q- m% q1 m+ F#define S24 20
# k: g+ r) W1 _% h3 T#define S31 4
+ Y3 i! F; x, F#define S32 11
4 z: }" \0 _! G #define S33 16
/ ?1 Y; U3 b0 Y$ j3 a! z3 r#define S34 23
& S- h0 L- F. [1 h#define S41 6
" H) u/ ]4 Z, x( q% k/ o #define S42 10
5 c1 l4 H) p7 P( ]% P5 V# ~; d #define S43 15
7 z5 e9 p+ z$ A+ L/ Z8 \2 N #define S44 21
* G" K- C- w( J+ ^$ x
3 x% V3 R* d' h5 ^7 |# y% i static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, M5 X4 a" @" k9 t0 ]static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' A; m( b( v6 Y4 j, ]$ b0 k$ b; c% Xstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
0 D9 _- L& e7 v6 i+ u: I4 u* |' |static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, O! J7 }! _6 ?! L: Qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* f2 W3 R' @7 D& R: W
/ M$ b4 j8 D9 m% |& k n) C7 w+ J1 Lstatic 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
* z3 f: D& a9 K* W/ t) s4 Z };
" u' d, U/ @# Y ~! N5 K9 t- m _
+ Y5 p1 P, k7 O0 N& \2 O5 N7 M /* F, G, H and I are basic MD5 functions.
*/
9 \" s. |" M" D* K#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- Z' U4 ]* f7 r* p #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+ |* |& u8 P! D- z#define H(x, y, z) ((x) ^ (y) ^ (z))
% b- w: t* z5 \7 z/ n) s #define I(x, y, z) ((y) ^ ((x) | (~z)))
: z9 o; M, |/ s1 c" p
1 z# B5 ^: n. H2 \+ t7 c/* ROTATE_LEFT rotates x left n bits.
*/
( O+ \+ m0 ?$ u& z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( A5 D. Z7 N; }0 B9 U1 k
, V$ Z6 z/ B# w/ _( G+ P /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* }5 Z0 S- P( d9 O& Y. w Rotation is separate from addition to prevent recomputation.
*/
2 p8 c6 d6 O- n #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 e B# ~* T+ `- S#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# ]# h3 [# V. S# d0 [ [/ h0 p# j! U$ _ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' A0 u% \7 i& \. E #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* A& s9 M( h4 b
: W5 V5 A& e8 R! \ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, _& d9 S* n7 H# `! z void MD5Init (context)
$ F5 l9 D( H5 B+ F% ^MD5_CTX *context; /* context */
/ K: ]4 i" L$ S3 D0 a8 B+ |{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) S; b+ k6 k; n */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 g- Q+ o6 X" h3 \) j( }5 g0 g }
, W2 h% j) `7 j0 y' V. G
7 {2 J( U- ?3 S4 P /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& X- D+ a% N4 D5 s2 C# U& Lvoid MD5Update (context, input, inputLen)
: a( b. i' a( I/ P! hMD5_CTX *context; /* context */
$ \: G. a2 J( Bunsigned char *input; /* input block */
2 T9 V& Z: J" V' e7 Funsigned int inputLen; /* length of input block */
. c0 {/ t5 d9 J {
unsigned int i, index, partLen;
' h" |' t* P2 F* h: `. H
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ Z5 |# E: N4 d' n0 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++程序


( L/ N! ~ z* a3 d
% ?0 }7 W7 p) A( R
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-5 23:31 , Processed in 0.065390 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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