中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, `4 v. O/ w& Z5 f#include "md5.h"
5 q4 e4 P0 l' x# n) n( v# A
; r) s1 C+ v+ e$ y4 {, X* i /* Constants for MD5Transform routine.
*/
U; u( o6 y: O' g
: B. J6 |9 ^/ V8 @
L$ U# U1 s+ N#define S11 7
/ Y3 K, @+ {' n* q8 Y \+ d #define S12 12
! D# @8 e9 N4 k( [9 O #define S13 17
4 s( o& H3 i, i6 ^; p. \ #define S14 22
2 i2 D4 l+ Y" o#define S21 5
! \! s1 y6 q+ N2 K/ z #define S22 9
; R2 \5 ^! I; a7 T( k& N+ O& g# z #define S23 14
$ o" n( b8 R' _+ H% {( U1 v: ~' e2 v#define S24 20
! G1 s. T, |8 D1 O2 T#define S31 4
, k! W+ @8 U9 f #define S32 11
4 t" S) a" D% ~3 C; Y3 ~4 Z; \ #define S33 16
$ n$ E* u6 Q4 p4 d4 ~ #define S34 23
; C1 Q% l0 G5 I- Q3 l- _#define S41 6
8 L' I0 E- p; r#define S42 10
' y9 W0 N3 O, w1 I #define S43 15
2 ]- z5 p/ S8 C! K, Q* L#define S44 21
/ Y) _) S9 \) [- o8 }
# H9 G3 O3 q; P4 \0 k" b0 T3 X static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) ? z# z4 w2 G: i2 c! q static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
3 x G' Q" R- i8 lstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# [! u: ]1 H' @: f- n `$ t static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
~+ E+ s3 w6 n" t static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. D! p$ I2 v, F& Q/ W* ^
) s+ h$ Z3 p) v8 _/ lstatic 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
2 ~# X5 V; W, m* Q( u};
: O+ N* E, ?, q/ C1 \
. K0 D0 _% A6 [; s, z, Z2 U /* F, G, H and I are basic MD5 functions.
*/
+ F/ V4 D6 R# u #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ u. l0 G6 r* k" c3 \( f #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
p& e& N/ [5 I8 f. Q$ U2 y #define H(x, y, z) ((x) ^ (y) ^ (z))
3 [$ k) R1 c5 n& v#define I(x, y, z) ((y) ^ ((x) | (~z)))
% R1 {) R; `" R4 l3 A( \' P
9 u- v; x' i! A; g% [' r /* ROTATE_LEFT rotates x left n bits.
*/
- l7 B4 c# l3 H- M( B9 u* F% p0 g#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 D- h/ u6 C1 _/ u( T+ `" @ Y
& {/ q2 h7 _5 d /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
M9 g7 J# H/ {' G* n1 t Rotation is separate from addition to prevent recomputation.
*/
% i5 o2 ]; u& a3 | #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! e* ^3 ?. P, z% Q. b #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 ]7 L; D+ x1 g- c#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 @3 _) M2 m# J4 W, G #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 G* F1 Q. }' E5 s6 x
7 h0 ^1 x/ p8 w6 Y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" G9 F; }$ h c% Y& G U+ G8 s void MD5Init (context)
" X1 S& L) A8 M% @& OMD5_CTX *context; /* context */
9 L: h1 m9 n, X1 U {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# O5 G6 H2 r1 J+ F+ Q */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( P4 ?: K6 M) N9 B2 p9 U }
7 s, ?: k0 \0 O( \8 Z3 s3 i
$ g$ w& _. @( K5 Z: n2 j y) l/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
+ i" Z6 g5 t% M6 Y7 w. r* t/ v) Cvoid MD5Update (context, input, inputLen)
K. e' p& \& Q& _MD5_CTX *context; /* context */
# @0 ?; S9 x! D. m* i unsigned char *input; /* input block */
- a( V4 Q, P. Y; R4 |$ | unsigned int inputLen; /* length of input block */
6 s* T% q8 Z, {, G1 }! o' K {
unsigned int i, index, partLen;
- K1 }4 o0 X* b% Y/ a+ ]
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! K( T+ k0 h8 ]; c
/* 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++程序


5 o3 e% ^0 f* d+ f& X6 R) ?8 m
' D5 |$ H+ t8 [3 k5 ~+ P( S/ ~
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-6 09:51 , Processed in 0.075954 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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