中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
9 w7 x9 D( |& \- ^) ~ #include "md5.h"
+ A7 U/ }4 ~4 [2 T$ L
9 R4 g: y, X- B0 X /* Constants for MD5Transform routine.
*/
4 ~8 K! C+ u V( X0 x8 Z
) y: k5 ]. ?, C) U+ b% e3 J
$ E0 S3 p: ~/ N6 f& S- e. a #define S11 7
; K j1 t$ N5 T #define S12 12
8 Z5 m' l. F% W7 W#define S13 17
/ \/ D( W# A; J% G& b% P#define S14 22
2 b. o/ {8 x/ d: I #define S21 5
' M) @. D) u* J5 t #define S22 9
) s$ v" s, c; { #define S23 14
' l9 c7 u9 ^; k% D+ Q2 V #define S24 20
6 |0 b$ A. O2 X9 `#define S31 4
0 j k# J8 s G/ i$ w* i) g #define S32 11
O* \4 r4 _3 P" p* G#define S33 16
8 g- Y* a. P! e, t% I! d4 h$ a #define S34 23
, p! Q/ X3 N, z5 A( W% k* \ #define S41 6
( B1 X0 U6 N& Q: a" t* J #define S42 10
8 A a5 R3 b9 I/ {) f #define S43 15
2 Y* l, c" {* i #define S44 21
J. Y7 c# t- t; Z4 @0 x/ R
5 t. E# ]- q" `3 r3 _/ f' nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* ]+ [% B) Y$ q5 [ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% J1 ]/ w$ O3 L3 B( Dstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 s" I* Y6 C" L, ustatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- `% K7 {' x$ V% Z( Qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
* ^0 a& L' @& g( [ s7 W
, s; g& ~0 X$ S# n w! c' W 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
3 W1 L& Z) f) t' _5 E3 U: | i( S- {};
( v; a6 ^' }3 }7 r* M+ e
4 _3 {, h# D: j, b m( m. s /* F, G, H and I are basic MD5 functions.
*/
! a) s7 s. I9 ]" E% l7 c" j5 |" L#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 C3 P9 `1 o# ?#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; \, Z: U4 l! F% Y5 y #define H(x, y, z) ((x) ^ (y) ^ (z))
5 |7 x$ N( w* W* d2 S#define I(x, y, z) ((y) ^ ((x) | (~z)))
+ N8 J) a. { z! s' E' R
2 g! w, ~ H+ p5 b1 y /* ROTATE_LEFT rotates x left n bits.
*/
+ c; _1 i M, a2 @4 ^: J5 h7 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: U+ l! s9 ?2 N; i
# l7 d' Y) _9 k6 l- \/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 A; ?# D9 V* p7 N6 C6 y Rotation is separate from addition to prevent recomputation.
*/
: h' A3 J+ h( F& {9 Q# Y: v' f4 l2 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); \
}
0 N1 N* u2 G" W#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! m# F8 x! C8 i7 I7 a0 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); \
}
# G% b# t: o$ F, g9 B #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& y; D1 Y' ~+ i
$ N) p# X! R' Y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( n& |- z: d) o( [7 v void MD5Init (context)
/ G# i4 b( k5 _5 X/ q( x MD5_CTX *context; /* context */
6 N$ P; b+ w" l1 z7 ^( H- x {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
3 F* C7 g( @ Z+ t8 H*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 d: Z( a/ n4 ~9 o9 B3 Q; X9 h1 h }
6 O8 V% k5 Q, A, W) s
- c W$ ^- g7 g0 d \8 h5 B /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 [+ b# n) x7 W3 z5 l. n* Y6 F void MD5Update (context, input, inputLen)
" x) q( X% k9 L1 J) L: c d MD5_CTX *context; /* context */
5 S) q9 E, K6 g+ P v unsigned char *input; /* input block */
" n$ o( U5 D1 C0 o, \: k6 Yunsigned int inputLen; /* length of input block */
+ J5 q6 K* J6 t% _# p{
unsigned int i, index, partLen;
% Q7 `( U. N J8 u0 X& C! `
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* q! C; b0 |% p$ o2 d! N3 m% i
/* 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++程序


+ s* n- v: U/ V) D0 s
3 t7 F, _! k8 h9 }+ s* [
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-26 15:24 , Processed in 0.059044 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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