中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ }& Y3 k3 o% {% Z4 J2 v# B- X #include "md5.h"
1 T$ m8 }' o: w
2 z8 @( w O- h0 Y# h /* Constants for MD5Transform routine.
*/
0 l3 _3 r! b8 d3 j v& \
4 `- q. m' {; H" V$ @
, a/ M: T: |& F7 S, m! L. E( J7 p; S/ a#define S11 7
$ G) P( x7 I) n0 N& g #define S12 12
3 |& g* h: y7 M5 N" G u #define S13 17
. D1 K- r! i* ^#define S14 22
) }' m% X+ U2 f7 Z, J2 K#define S21 5
: O! V' v' D8 F( z% f #define S22 9
3 r# ~' b l( m* x* }) K0 ^#define S23 14
1 ?) D" |9 F+ e+ }3 e0 V# O#define S24 20
6 k% k4 n3 O) w#define S31 4
+ @. \) L% Y0 A3 M+ P6 h& J/ A8 m #define S32 11
4 ~# u, L" s5 `$ u# ~& I7 o #define S33 16
+ Q( T1 X7 M4 {/ a9 z% }/ A1 [#define S34 23
2 ]6 o- l6 S+ ?* Y! T/ i& ]#define S41 6
; `2 h! j( w7 c/ `, l8 i#define S42 10
4 I; v: ]( u, w0 z% m #define S43 15
; K5 K8 b' }0 O9 o5 X0 l8 N #define S44 21
% F( X4 W0 K/ u) r
- c; W6 ~( S, D1 T static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
& ^. p% Y8 T2 Z1 p1 V' Pstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; N8 s& H1 e8 h) l. ^1 \- M, T. C static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ |6 D, e$ }' r! j3 h* w& k1 s+ vstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, `1 C4 M. u1 k! O0 T* P0 Sstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' \+ v, R# p! }, u$ n, i' a' H
3 E+ R/ N* {0 w) p2 Wstatic 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 Z" P2 g& N9 w1 s" g+ L2 h };
' q; S2 _" H7 l# x0 s
2 W* s/ E) S0 T% b/* F, G, H and I are basic MD5 functions.
*/
2 p8 T) L: j# M0 q$ F: E, Y #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ v* J9 n$ R9 W' Y7 J; k#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ M' h8 e3 S% B( P. @1 b #define H(x, y, z) ((x) ^ (y) ^ (z))
. w& i/ G$ l2 m `9 x8 \" v/ r! T#define I(x, y, z) ((y) ^ ((x) | (~z)))
% e! n1 d1 M+ S! ?
( q+ {/ X/ x0 c: U0 T7 r# g/* ROTATE_LEFT rotates x left n bits.
*/
! J6 E2 Y) P3 r1 [- X#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ y& b p: ?( |2 c6 {
: x6 A/ N" o5 \+ q" H /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 o# `9 _9 |, _% _$ v s; h Rotation is separate from addition to prevent recomputation.
*/
% C q, o$ M* T. 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); \
}
. \3 t* l; {! ^; f, q2 a#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, z. W" |: Z( u9 m) | #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 ^$ ^& A; e0 l5 C9 r/ N1 M2 U. X #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 h7 d# M, g% D% p1 O ^0 a
6 l6 O, d N Q4 Z( z. y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% y) o9 B9 N2 g1 m% s/ K) ` void MD5Init (context)
5 W( Q* x# J9 P7 w( y MD5_CTX *context; /* context */
% ]4 C/ c8 I3 ?1 B/ m8 s* u# k {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" t+ W+ V& m* G) R# ]$ D* d */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 |/ I2 k$ y# ~+ z* Z}
* Q% E& H f# b% d Y& z$ [
) |4 a: U5 ]/ C/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
g' Y: D; F% x, \3 ^void MD5Update (context, input, inputLen)
9 D2 e3 W' s2 k- GMD5_CTX *context; /* context */
( T5 \/ D* K' C' s% G0 N( punsigned char *input; /* input block */
- Q) Q' e6 W. Z7 T unsigned int inputLen; /* length of input block */
# B* A% u& ^/ T6 C{
unsigned int i, index, partLen;
- F, O S* Q8 D# y$ ~$ o. c: V, x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! v. ^4 e6 _, c5 Z- {( 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++程序


. P5 U) B5 o( E( d
% y# A# |) Z* y. Q! C# e4 @
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-2 11:45 , Processed in 0.064744 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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