中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* I' D) ^- v6 D" s1 |5 Z. J6 M+ a #include "md5.h"
& `& |* a& \2 Q) x3 E) o i8 T- }
0 c5 S) z( s8 X, D& y" x6 d! }/* Constants for MD5Transform routine.
*/
& |( K3 }, Q9 N% N/ |# A8 t% W2 I# P( O
+ B$ m, Q2 ]% {4 y
$ a+ G& `/ |* [ #define S11 7
# W2 m( C/ U* Y$ K& f- s# j #define S12 12
3 f; p- a5 n9 V/ o" x1 a( W' p6 I #define S13 17
6 \* u0 o) E& E #define S14 22
, z5 g: p: B9 m z: S0 J#define S21 5
& h0 Q1 y: a0 R7 A #define S22 9
; _* a+ {3 R( b3 e, M+ |0 ~% @& S#define S23 14
1 w4 B5 m2 }7 z+ g p7 c$ S#define S24 20
; I9 O, k' h8 p( @#define S31 4
1 P- m3 ~# r }$ S#define S32 11
/ L7 H1 c9 H4 Z9 N8 e( y4 d#define S33 16
+ w% }; m' v- d" Z0 F# A#define S34 23
; z2 m( l t" r; { #define S41 6
7 c: i! I% V8 |#define S42 10
- h* }0 c+ a r7 r/ X#define S43 15
' |" S$ k7 ?4 s$ j M #define S44 21
9 H# r) i* K8 g1 h
5 j9 {8 M2 |( h% R3 Nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, u4 G4 t9 ?! ystatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) X9 |4 F: O. S- T7 Z& e+ y: cstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 s# T) }, v: o# ]. o. Z static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 ^& ]. s2 r) `; I/ _+ Wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 T: u9 F, }$ v. F' R/ ~' l7 B
/ W! ^6 ]% J7 j; q( D- x 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
+ f* R* X R& W; ^6 `' o};
: x/ ?; J# K0 e9 ?6 v/ Y' r" b& k
" P( P7 P% ~+ E' b3 D0 F3 p+ N/* F, G, H and I are basic MD5 functions.
*/
& r. V1 U2 m5 g; `3 I #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! f2 Y% V+ N3 }- ^8 i #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 e) [( z4 `: s2 J* r#define H(x, y, z) ((x) ^ (y) ^ (z))
, h. H. I' F% A2 Z# e) m#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 q) m6 r: J" |& ^8 G1 W
' U& O9 {3 J# {7 L0 v+ {- |/* ROTATE_LEFT rotates x left n bits.
*/
* \# d/ W. f x. G3 {#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 r8 G' `) M; k4 _2 `: ~. L
4 B+ H" L4 Y; N9 Q$ {# b+ y8 O /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# T! j. o2 `$ `: b% X4 MRotation is separate from addition to prevent recomputation.
*/
2 g7 |7 p8 L/ r* a8 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); \
}
; ?& z% _- u& A' D5 d#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, }" o. }2 A; j" b6 Q0 @' Q8 D#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" @' ~: S# r- \6 Z1 K#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ X" _% l. a- Y7 A
: `' I+ L* o% [+ G5 i: A7 ]/ E! d/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& a3 @# R5 ~- n8 s" l" t7 y void MD5Init (context)
4 z Q/ P: |% }$ z$ ZMD5_CTX *context; /* context */
2 d1 o9 S) ?( q{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 `- r9 F9 z2 ` _' ]*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 f/ \# o8 t* O5 ^ }
& y, M0 P% W) T1 I" N- ?
4 f9 J: H5 k0 k5 D! a /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. b* I% C9 L$ A- j6 n4 Y0 s, L void MD5Update (context, input, inputLen)
; _; e* k n. {$ WMD5_CTX *context; /* context */
" }6 f( }6 e* W2 Nunsigned char *input; /* input block */
/ h7 N4 u8 g$ T9 [$ Tunsigned int inputLen; /* length of input block */
; [( p) D+ a- ~% ` {
unsigned int i, index, partLen;
8 E3 \0 l$ t+ B. O |% ^/ @& F
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( e' V t, E; _/ S6 T8 o3 V
/* 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++程序


. ^- v9 H- o5 i. H/ w- P! d
5 Y- g ~8 e$ f# E% s' z) R# n+ a
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-20 14:38 , Processed in 0.056124 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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