中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 M! a1 f6 v( q. U; }: {; t#include "md5.h"
- S, J0 I9 ^' C. m/ B* U6 X$ B
' e) x. r% b7 Y9 u G /* Constants for MD5Transform routine.
*/
! |4 B0 b% d4 J( A! d' {3 W
8 p0 z' R& z) K% f
& `; _& g- }; J0 M#define S11 7
% a; k) g( M! T#define S12 12
# t/ E3 r4 [2 i! j) h4 U, [. v #define S13 17
4 w3 H0 l# J# p8 d#define S14 22
4 d$ C5 e" B* C' X #define S21 5
' Q" M; z+ ?8 \0 Y4 k0 u#define S22 9
9 A8 R8 A$ m7 C. \ #define S23 14
9 R0 j- o# J0 ?( }$ t; i#define S24 20
- s9 G5 a) x/ Z #define S31 4
/ x7 H/ {8 a9 e. ]9 n! f#define S32 11
! [" ^0 g( Y, C: h #define S33 16
# d( `$ R: |9 V. ~ x' r( w #define S34 23
: h7 Q+ V/ \. w$ l: T/ n# c#define S41 6
% m; G; l3 J- l- ] x #define S42 10
5 H) E0 `2 T0 b. d" |#define S43 15
0 _ J+ w8 V( {6 w #define S44 21
1 c* D' J. w4 M
' `1 m6 y" }2 l; M0 F2 s4 v static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ v7 d6 P7 y2 I# P& ^ k/ A( S8 Z& h* A static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
. x. X1 P" D9 E0 {7 B9 ?static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: E) P3 s/ G- H( P7 | static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# o) ~) {! x2 A0 {3 g) T6 F8 t qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
0 N- y( Y2 D# M ~, J
' g! t5 y1 b4 v2 R+ p6 K) f' U 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
+ K( O/ n. y# Y% P };
2 [3 Q% L7 F$ y8 K. R
- a- ]% c) \9 M/* F, G, H and I are basic MD5 functions.
*/
7 ?1 |% z) g$ N) \4 u) v" y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! T' K4 r' B7 {' g #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, y# j3 Z7 B" ^" H+ c" w& h#define H(x, y, z) ((x) ^ (y) ^ (z))
8 q2 S( w6 h! V" l+ a2 ] #define I(x, y, z) ((y) ^ ((x) | (~z)))
. m4 A: i4 s: `' ]/ f2 b
/ k- a3 F4 m/ E' x/* ROTATE_LEFT rotates x left n bits.
*/
5 o. t* ?6 K/ Y! m0 \* z8 Z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
?3 ?! m d( m9 i- V& V" b2 }
! L1 b0 a8 L, v0 k& i1 e /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 j2 n) S- v* \ G, d! ?6 s0 V Rotation is separate from addition to prevent recomputation.
*/
6 S; n0 r& a6 ]& l6 t6 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); \
}
. _ G; U- Q4 A6 r: G' G, Z#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 q g" M; U4 |6 s: h& n# j$ s* l#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 y$ i) A5 J1 j- u* w' p( P #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 q) f' l! g2 F/ e4 a
3 A6 q+ i2 x- c0 X: R) P' p/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 f- U! c7 c/ T+ n3 O void MD5Init (context)
* M K' ~- }) }( |' y; C: X2 sMD5_CTX *context; /* context */
1 B7 `' f9 j# g2 T0 f; U {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) C1 L3 ?. ?7 m0 r" b7 C Q1 j5 m*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
" b9 E/ ~6 O$ l7 u* k6 t }
/ H" y5 f8 ]: s. R0 l- P
& b0 u* U! X' A ?$ M /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ J$ i: P3 s* t9 h7 X- R void MD5Update (context, input, inputLen)
' f2 Z% l: o+ `+ tMD5_CTX *context; /* context */
- _) }4 I, L& y( s" Xunsigned char *input; /* input block */
; P- i8 ?% D# s" x- | unsigned int inputLen; /* length of input block */
. f3 i# F. M3 V+ M |7 i. |0 C7 h {
unsigned int i, index, partLen;
3 \2 d; V& r/ L) L& n
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 D$ s& r2 n: ?) H6 }
/* 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++程序


# q, v9 _$ I; @) V9 r. T" P
& ~6 C) _% t2 U+ b( E
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-19 06:11 , Processed in 0.079041 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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