中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 Y) O) I& Z+ e# A2 `, C #include "md5.h"
% b0 L. g; m0 x1 P+ y! j9 v% E
3 y. H) z9 C( c9 N9 o# Q( q6 { /* Constants for MD5Transform routine.
*/
, ?2 N4 M# A# d8 ]
0 \0 ]; Z8 y1 a! d0 {/ o5 L
0 ?/ `' m* X- p#define S11 7
: r- S5 A2 Z: v* q#define S12 12
0 _- a$ w, s7 U( M #define S13 17
" O3 E" \8 v1 v #define S14 22
: u" f$ B; B" u3 n/ U #define S21 5
/ U1 V- \$ }! \" e( F) L3 V& C/ m- S #define S22 9
4 X& T& M. z1 I q/ [, q# c# k#define S23 14
& W* D1 ?8 k" }6 D#define S24 20
9 U/ t! [5 z2 B% p: J #define S31 4
0 e( k* c) _: e3 p. L #define S32 11
- }# ^: K% g" n& N X #define S33 16
' V& ^6 q5 |- V1 F3 |# m #define S34 23
' b' i, F& Q' V8 u7 _2 N #define S41 6
0 l% [% l- @: A- E #define S42 10
+ C2 d2 A* z5 n& R# D7 W8 a#define S43 15
0 p7 L4 H: l J5 O# T; x) N #define S44 21
% J9 S' |3 K$ T
: \& q2 V; Q3 r. sstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
{3 Y: W4 h4 u% ~+ kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ m5 F/ C1 d/ {static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
w6 T+ [4 y+ a- Tstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
d4 x/ }' B' W4 O2 I. E$ n& M2 Nstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 A% `! n4 k* [9 \' _4 `- W- f
5 g8 ?4 l# P7 O6 e" T+ istatic 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
v% J' U+ C) t6 y1 H# U" w};
4 \/ s+ ]2 I! L& S4 e
4 ~. I" Z _& {/ J$ o /* F, G, H and I are basic MD5 functions.
*/
' y' K% D8 Y5 U5 e; z3 b6 b #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) g; L3 Z, b8 _# v #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 w' A) H+ R3 p# a. T+ k #define H(x, y, z) ((x) ^ (y) ^ (z))
4 i% M; }- h& A2 | #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ V: _( m7 {& P* r* \0 B0 q
& H8 ^# L: n: t, u/* ROTATE_LEFT rotates x left n bits.
*/
- @5 X( i: x7 i" E#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 y |$ y$ ?$ q; y/ S5 u$ v
$ x+ ^1 _1 M1 g" Y1 ^6 u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 g& h% @0 S! `3 R8 e& ] Rotation is separate from addition to prevent recomputation.
*/
- L$ C, V; r1 D4 H. P# M _0 b3 T #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
n) D8 @- v9 H6 h1 v# G, s#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; X1 ^# e" ?5 @) V #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" H4 A' ]9 \6 m) m8 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); \
}
) w" ]2 N& ]: s. |- @. q! L K- q
7 L9 d' q( e' z' `6 B8 F /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; [! z' M! y. D7 `3 {# A void MD5Init (context)
: \, e$ v: x6 y: y. P) FMD5_CTX *context; /* context */
: _4 d4 {0 E( s) {{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 P0 g4 S- r" G0 L, ^ y) \& j*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) Z0 U) i0 S7 ~ }
' y! b+ Z# ~1 F6 j1 v1 w# I' O
' T9 V v- K5 Y Z) |& p /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
]+ _$ |. g7 Q# g* I* P void MD5Update (context, input, inputLen)
4 L7 T- i: c! F0 L; Q6 E9 ~8 N MD5_CTX *context; /* context */
- D8 v- [( P$ b* iunsigned char *input; /* input block */
; L! `' D' i, N, ^" Y( T unsigned int inputLen; /* length of input block */
" ?: X; K! m I{
unsigned int i, index, partLen;
: J* y/ F9 ?$ P, B
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
; K, B, U4 }* c) K- K
/* 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 {% M6 d( L8 z- [
3 a1 P& j( B8 s1 \
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-27 01:21 , Processed in 0.084752 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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