中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: F! j. p- v& u+ C5 q#include "md5.h"
6 H: j% M0 s3 _; _' I$ x4 o
, B3 ~9 s% N$ |+ y/* Constants for MD5Transform routine.
*/
3 R0 U1 [, Y: W
/ D( S+ f+ J6 i7 ^" ]9 e
) R6 v1 W5 C. ^7 G #define S11 7
8 W, j. ]) \3 r#define S12 12
/ T9 G! W* k( m0 _; A" R #define S13 17
; X2 ^. Y, L$ b #define S14 22
& S9 A y; _5 ^3 y* H- J( x& Q#define S21 5
* v" k. [& m* S- r#define S22 9
$ _8 s7 N+ [: l; T #define S23 14
& g1 Q! P7 H! h, j #define S24 20
* ]3 u3 A! [' T) e* {' T7 z #define S31 4
# _( w8 g; ^' }% c#define S32 11
4 n% C V7 o4 N) k# {$ y6 \7 ^ #define S33 16
" [! Q: |& ?' X+ s! k#define S34 23
2 }" C' Z/ ^7 G$ D% z3 \! S #define S41 6
# _" g# B& p+ W; M#define S42 10
% A0 G% C+ l. H. h- O Q#define S43 15
5 r: j4 N& \* G0 m4 ^6 B#define S44 21
! I- q% G8 s0 S3 h4 o) }2 J# g
% y, q- f0 f* k m static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ b) S: t4 C# K l5 j static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 I5 K: D# w! S2 { _static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ l* o" ^/ ]3 R7 A5 G+ {1 N* o0 M static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 x6 \; Z9 z& V; xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' B4 x! O- N& i+ m A [: |* i
# A. N$ j8 [1 e 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
; g3 H0 ~# P. V6 ^" \# e };
# P1 \- v! X5 R3 O
5 u4 g' f* f& i; N1 A/* F, G, H and I are basic MD5 functions.
*/
9 Y; C5 W7 q- q) f. _% Z#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
5 u. }' d T0 N( H5 a9 ? @0 T #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ j( q7 @9 V$ i% S6 \" E5 q #define H(x, y, z) ((x) ^ (y) ^ (z))
2 E5 O/ e v( M1 j) _3 d$ E$ e. \7 T#define I(x, y, z) ((y) ^ ((x) | (~z)))
s3 M, X4 f. N! k8 G
6 T- R; b8 T. L. W, v6 I/* ROTATE_LEFT rotates x left n bits.
*/
" X! Y1 K q$ i9 X#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( S' ^( i! o7 f2 V$ J5 D% l- J
( n/ [8 ]# B8 L! I/ i/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* x+ ^- V( V, {% U( y: k- eRotation is separate from addition to prevent recomputation.
*/
+ g Y7 t4 Y. v: f- u2 |#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 @, @8 m5 b2 n) J #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ u, \ P) F' [#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" ?6 ^9 q/ f6 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); \
}
9 r2 V& M% A4 L# E5 a& u
% T6 n. ^% m R% ]8 F /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* M# K/ [& ~7 f" }+ i j& | void MD5Init (context)
. p" R( A3 S0 p. Z& ^MD5_CTX *context; /* context */
' h4 V j( H, T% l {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' t; _% {' q# ?, p1 D- }) ]*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
% z5 p/ o/ J' l o* |6 o4 g }
' q/ y W2 e: N- {7 h' _
" l: G6 h, ^$ x+ S/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 l7 W0 q# ~1 S! H1 Z$ S' Z- fvoid MD5Update (context, input, inputLen)
( @6 ?- o/ m4 X; w1 j3 J: ] MD5_CTX *context; /* context */
- R3 U( i2 M$ n4 p! w& N' vunsigned char *input; /* input block */
U$ X) J( R1 y. a+ Y9 U unsigned int inputLen; /* length of input block */
4 B. F; \5 u3 {* k3 c {
unsigned int i, index, partLen;
. b( p# c m! u9 }, |4 ~) f3 P, D+ u0 P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# P P6 V9 @! x) ~# Y' o
/* 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 l( z6 f& i* n
V; L/ Y. b0 J! L" C5 _' h4 Y! d
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-16 15:58 , Processed in 0.159154 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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