中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: ?+ s/ n, E3 u$ e- f #include "md5.h"
& C8 ~$ Z) s1 {+ l7 _4 U
Y5 k6 z) d8 j# |% p. I" g /* Constants for MD5Transform routine.
*/
2 h& a6 t: d! _8 t! @8 F; x
+ J/ h$ T& m1 o1 \* U
) P$ L* l; N! F/ k #define S11 7
1 K; W0 ~ _+ | g9 q/ x #define S12 12
, G5 L' O! f1 r/ o" C% B4 X8 u #define S13 17
/ m; a% b# g+ ]0 V8 n" w# O/ M #define S14 22
: {0 g5 k/ A1 I( Z% k #define S21 5
+ G4 |9 p6 o- Z5 t4 _# u( w( a- M( s #define S22 9
; ~8 @0 G& D2 G4 q- h#define S23 14
# m+ K0 s7 U- |' y4 a% L #define S24 20
6 k& n: V f1 A6 k- ^+ u2 a#define S31 4
: G. {. z/ N" m5 _#define S32 11
. i" q' L7 o# W: T#define S33 16
3 u1 w& U' N/ S H7 Y9 k #define S34 23
5 ?9 j# q K l) k7 R( N. v& A #define S41 6
1 \# x9 w" h/ W: y5 K #define S42 10
3 h( i1 t, m5 { #define S43 15
' k/ M, r* N0 P2 E* |8 p #define S44 21
( e% d9 a9 A3 e
+ ~8 m+ |8 P$ l" ostatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 ?6 n, C! j; N8 g0 g* d/ |static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 W- v/ \& [/ R' l+ q( g( u2 N5 ~static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% {5 }" K5 L+ |static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 w1 P; g" _: X9 b5 xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 e! @/ T+ P8 ]/ @3 J/ m, q7 ?
; `4 A" H% n: Q! w$ B 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
- \# f9 z8 {& H9 E+ |( K/ A };
4 S+ Z# w& V3 M& e; z* J
. R7 X6 c- r4 @. x% e/* F, G, H and I are basic MD5 functions.
*/
' S) Q3 }7 h0 h#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
9 t/ V* ]& Y3 K; F, @ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% y. }9 w8 W) D, f' i, J#define H(x, y, z) ((x) ^ (y) ^ (z))
% C) S( R0 p* X7 E #define I(x, y, z) ((y) ^ ((x) | (~z)))
! M& I9 l" L: Z% T( d% s5 p
9 T+ W% @/ r, A. Q2 n8 s /* ROTATE_LEFT rotates x left n bits.
*/
2 k+ ] x" f' X" Z5 q#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- S* A) J2 e$ D8 Y6 Q! |6 H
+ w. L3 U! B' N6 f* C7 G, X /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 z) f5 T4 t. g5 h* } Rotation is separate from addition to prevent recomputation.
*/
9 w2 k% r9 a4 q, s9 g4 u* k' m#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 `7 l8 n& e h. l: T #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. T& b+ @8 q+ F: } v9 ?- 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); \
}
3 T- _, Z+ ^: N, A #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; _$ p; O$ J: m7 j `
# q& {4 g0 P. d! ]3 c& t /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* {5 A6 |: H7 o' m" Fvoid MD5Init (context)
9 H9 e2 O: q! Z+ { MD5_CTX *context; /* context */
9 Y3 _1 w4 h- i+ t# n! k6 L {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ K b4 t$ F* N4 ?; N! r*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 X% i* \ d o' u: h; l}
* N* p: v8 ?# p* b
6 ^1 C V1 |, C$ r6 y8 @/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ o6 {4 c* A2 {4 } void MD5Update (context, input, inputLen)
0 j0 B5 n' h6 |% ?6 iMD5_CTX *context; /* context */
* V% e/ N. U' Uunsigned char *input; /* input block */
7 N% F+ u% m4 J$ m ]5 ~ unsigned int inputLen; /* length of input block */
% P. ~$ d% m3 s( E7 [1 V, G ` {
unsigned int i, index, partLen;
' s8 N* \* }" r6 k) D- P( p8 W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, ]$ H v5 F T7 m
/* 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++程序


3 ?0 M/ ^: K. a/ H d8 S
; R' U$ n Z$ K
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-25 14:25 , Processed in 0.083094 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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