中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# W- Z) f/ q8 t* ~. t1 Q9 g$ J#include "md5.h"
& l+ g) d& u' m# o$ U, E$ K
3 S2 c6 \8 U- D4 X/* Constants for MD5Transform routine.
*/
- l* ~/ M/ U& H+ }6 ^
* |7 N2 S3 |* K* e/ Z3 \4 y
5 o @2 }5 m, _# A" j+ O#define S11 7
+ K' g9 a7 S: I4 X7 v#define S12 12
% m% a$ P5 I, z( V1 z; J/ X#define S13 17
0 c5 b8 _8 I; u% m/ t8 Q5 k#define S14 22
+ {% Z' v7 f/ c# T. ? q#define S21 5
7 Q+ }' O7 u* w, Z#define S22 9
! ~. V/ t! l( V#define S23 14
7 r4 X" b7 ~4 z2 h#define S24 20
& F; D3 \0 U2 O5 C. \' E/ k #define S31 4
' r* O) C5 B; B5 [4 F9 k8 O& s$ u #define S32 11
L0 K) x) H# a' z#define S33 16
* L3 z* f( b; S/ L1 c U! k, k#define S34 23
* y) r& }0 p3 |- t, W. ^ #define S41 6
g9 L' a3 y( v1 \. v8 `7 P#define S42 10
! M& J$ w) I4 ~6 m2 A$ q5 Q# f5 B#define S43 15
. i7 b$ Z8 U: E( C+ y4 \: Z; E #define S44 21
+ Z3 d3 M, [+ |/ u7 |7 z
( s, F0 l4 O- F* T6 M, Z static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 S- q% i5 d4 P5 ?" A static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
6 v3 D, W9 j& z. ?" @5 sstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 M) m; i; f }: F% k8 J9 K9 dstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+ R$ [7 v5 u3 s# B static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# r- u2 z: G5 l6 ` o9 j; W
& A7 [1 o. V/ H0 M 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
( } l( H' Q" r5 M };
! T' H& y1 P M. A- O: m
# u. h8 S) U' H4 E/* F, G, H and I are basic MD5 functions.
*/
1 F2 G7 S* g- D! y$ y #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" b% J7 h4 W$ \& e$ ?5 e" @4 e#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# E* B6 U/ h1 Q4 v* ?4 {$ v# T, }3 o #define H(x, y, z) ((x) ^ (y) ^ (z))
6 `. k$ k8 A" D #define I(x, y, z) ((y) ^ ((x) | (~z)))
) j# N+ V2 `% I$ @3 g
& \9 `) C" \& N0 ] /* ROTATE_LEFT rotates x left n bits.
*/
7 y! U0 ~4 y# I3 C* f4 \#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 _* K9 @: v* j" f7 L9 q& o: P
, _0 D( H3 k% T; ^: _7 y, \2 q/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( r, G% e l3 RRotation is separate from addition to prevent recomputation.
*/
4 p. _; `% ?! |# o#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ i% {& h% f# j6 h5 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); \
}
8 }; o; W! C( c! X! h* G3 `* h#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- V: v: {8 [6 _#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 Q% g1 Z0 k. f; a8 T
: `% s( `* z% r. Z5 @( U /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 n* q' Y0 N T void MD5Init (context)
" N" B+ z: A) X" U3 `4 M$ q' J) `( IMD5_CTX *context; /* context */
% ~/ G; u( |1 s f8 |{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
W2 K2 H/ m) b3 k */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! @& M; ?7 n% B/ Q }
6 `, e/ B* S4 E. U) A, C
) ~+ ^7 l' Y& F8 |' b) O8 T* R* |/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- ?* {3 g P8 L, Y; ~) u. O1 Gvoid MD5Update (context, input, inputLen)
' V3 m0 R$ I7 e- K0 V MD5_CTX *context; /* context */
5 n1 b- a, @2 l% `" i+ k( Dunsigned char *input; /* input block */
: f1 t4 n- S3 g2 z unsigned int inputLen; /* length of input block */
) V" n' N: O7 R5 Z# O% ?: V{
unsigned int i, index, partLen;
: J% t* @, Y- x I
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ E2 M1 c" \& _, n; L! `& T
/* 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++程序


* A0 E8 ^' Z: b' W6 O( B, s0 H
3 L5 P. |! z& q" D0 w) F$ Q0 g
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-11 06:26 , Processed in 0.083319 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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