中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" ]" h7 p8 f" x#include "md5.h"
' A1 L5 Y9 v" `# k r" f3 p
! m! ]: u' ~: u9 Y/* Constants for MD5Transform routine.
*/
& L& T$ Y G- I% w0 ]. N, ?
# w# J' R% c8 X- X, `
0 a. A+ l6 @1 \#define S11 7
- q$ V/ B/ d7 J; }) b #define S12 12
/ a5 B$ K. u0 r* P/ l#define S13 17
( w1 W# \) P1 y" C6 ~: k #define S14 22
# J& \+ ~9 F) c7 w9 H8 @ #define S21 5
4 V% L4 N$ q7 x. X& Y1 \* f#define S22 9
# ^# f1 H0 r$ H. [* |- [- M& D: G5 m# [#define S23 14
$ v; Y1 f! P, F #define S24 20
/ V; b, u2 ]* J/ D% z#define S31 4
* J- f4 C. {; F. I: D#define S32 11
+ Y% @3 E8 x, Q m7 T #define S33 16
; F7 c- U, g" n6 t6 h& Z0 | #define S34 23
5 X! ~$ Q$ R0 w2 S#define S41 6
! p0 T o4 u( T% c7 S$ H0 ^+ I#define S42 10
1 e$ m4 X; Y7 F2 d7 B4 s: b& {#define S43 15
) ]4 Y0 z3 [8 Z9 s1 P/ {#define S44 21
7 k% @- C& @$ o/ k* }
0 |$ F3 E! _ f2 g5 U2 ustatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 u2 {( \, m; ~" S% [7 _static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- C3 s: L, @" h% |/ ostatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, w7 G Q! n4 g* G2 Ostatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. N$ Y9 v/ I! L8 v: t static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
7 z% N/ f3 c+ k
- ` A. d& x; ^; x+ o 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
0 M! V- z: @+ _! D- h) o* R};
2 c2 t" E! F3 O
{# e( K* r- U- U" K/* F, G, H and I are basic MD5 functions.
*/
) j, p+ X2 G: g1 d #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 W7 H- q* S" x; V b+ l#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% `4 L! H! S. ^! O4 ^2 [; l #define H(x, y, z) ((x) ^ (y) ^ (z))
) O* ]- h9 ` w6 V" p#define I(x, y, z) ((y) ^ ((x) | (~z)))
- z" ?: m5 p6 p8 h3 G6 F! I
+ T; Y6 [) @+ F0 h /* ROTATE_LEFT rotates x left n bits.
*/
% ^1 H& H0 M5 x! B #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- L) Z: ]( }" v8 I
+ V8 L2 n. n r# R /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
^8 p8 S3 `) I, t Rotation is separate from addition to prevent recomputation.
*/
# a' t3 p' }: N# s" |2 q0 c X z #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 d$ X# x5 e) I' N9 L #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: h/ y! |* C- K #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 `! p4 b( W4 L) k5 ~2 c' j9 h#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 R& }, `# @( W( `: r% b3 W( \- y
& t# L: f; K. _+ ^5 r3 a/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
0 T& x \2 d, mvoid MD5Init (context)
/ |/ S( [9 x& U- P' }! n5 yMD5_CTX *context; /* context */
: t/ d; ^/ d+ J1 q1 @{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 p, W) D) U2 Z Z% w*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( I4 |' F; x3 j, @0 J6 c& ~}
3 M8 b; [4 I0 H6 v* ^- x
" {+ f' C5 n- ]' M) J/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 B1 i3 H4 r5 Q. F# Fvoid MD5Update (context, input, inputLen)
( j! Y( C1 j& ~& ]MD5_CTX *context; /* context */
& B4 d1 w5 N0 \unsigned char *input; /* input block */
5 M( X$ e& Y7 X7 u8 G$ nunsigned int inputLen; /* length of input block */
+ B* s# K( T, k5 J{
unsigned int i, index, partLen;
3 y. m' s# M! e' K: }
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: l" N# g4 | V: ]/ a( I2 s* G* 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++程序


. B4 v) `# f8 {
2 R( T7 k! a% G3 W* A8 [, |
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-16 04:44 , Processed in 0.056094 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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