中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( H8 E) b6 N( j/ L$ M6 a- k' V, h #include "md5.h"
* r( E$ ?) r& P$ u- e
( |; E$ E* K! h /* Constants for MD5Transform routine.
*/
# B+ s4 |0 o( A1 ]
' B9 b+ ~: y, U+ ^/ ^) N' M' z, z
- b* O% j0 r- Q( X$ B% `4 ~ #define S11 7
5 ?# y$ P$ `# U) _ #define S12 12
3 G! _# p& I2 [( B ^4 y8 }$ `% W #define S13 17
4 d7 v: S) Y x0 V: u#define S14 22
5 j5 k0 w. x, J1 M#define S21 5
& d- e0 r8 S3 T% n9 n0 y$ z #define S22 9
* z# ?* p; N- p8 G, Y& {6 X. { #define S23 14
2 V6 p$ m. V8 _3 K* C6 D5 R #define S24 20
; }( F% W, p6 l' \$ P4 p& g I #define S31 4
. o$ L+ E7 D1 S4 ?0 A( ] #define S32 11
4 I% O; ~5 {2 x% S0 H4 L/ m! m#define S33 16
5 M1 G2 f. Z1 R' w( Q$ K* M #define S34 23
1 F+ r, g( d4 {9 B+ M#define S41 6
' p8 `/ K. M% m #define S42 10
# |, \/ }2 \: ^" r1 @/ D3 E8 K#define S43 15
# J( T$ e+ u4 D( `2 A% B2 e2 C. e#define S44 21
/ E8 e* p+ _+ j( s& [
( ~/ F+ y/ f! t: e& G static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 Q) c* a, ?2 m, I1 I9 c+ ?static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
. N6 d3 l) x6 H* n& k& D* R static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 p( ~: o, m1 I6 z& ustatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
( h. B/ A- k: u, B6 {) E static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
" F& r5 f4 E2 s3 H
" k* R' h' K! ^& h* o6 p& l 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
) n! {1 W' S5 ]% ~. l" y};
, R$ u8 ^ t2 R
7 p! H( g) C! R, ?' }5 p2 t/* F, G, H and I are basic MD5 functions.
*/
$ P& U$ D6 e. n6 P #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
5 A' V; ~, V; _! q$ f #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 C' W1 _) y/ W$ f# I* F/ L #define H(x, y, z) ((x) ^ (y) ^ (z))
" R$ O, o# w5 T9 B4 y8 _#define I(x, y, z) ((y) ^ ((x) | (~z)))
" H0 D/ `: k" S/ F1 e m
- i8 [+ M- c: b/ S5 m4 x/* ROTATE_LEFT rotates x left n bits.
*/
/ {5 P& T$ K5 Z7 l#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
1 v# V! Y% x5 y% g
: O) C" x0 }! @0 a, \/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( H3 g8 r! j+ K r5 lRotation is separate from addition to prevent recomputation.
*/
/ v, Y* m3 d" H# H#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 n# N9 W+ P8 `" o/ I #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. @* R9 c- t$ c0 U, j. f( L#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- z& q! o& A& M: {& K2 @& f #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; |, K' J$ K4 s+ }2 H
5 g' I# l" b7 z8 @+ ^/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% a7 [5 A/ y+ j void MD5Init (context)
! @$ F& ^4 a3 p8 ]" S% WMD5_CTX *context; /* context */
3 T& K6 f5 k4 `2 i{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 E4 j* S. f9 F& T*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# ~8 q d: E2 l; p" }( ?: I}
3 i7 g3 U) r" q; `
; T1 C7 S% J0 G" e! U0 Q+ W8 A/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, M1 T6 @! ^; b4 c9 d5 G0 \) @" S/ K dvoid MD5Update (context, input, inputLen)
3 }4 a9 y l, g" r6 m" P& L8 i MD5_CTX *context; /* context */
; f1 z p$ R( |6 N8 j8 L: I' ^unsigned char *input; /* input block */
) N% g2 q* o# d; \" K unsigned int inputLen; /* length of input block */
& o7 f' t% X3 J* Z9 F7 Q! l: l{
unsigned int i, index, partLen;
2 j6 M1 D: C0 Q1 Y1 w. C( Z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 V+ }8 d! [/ r2 J: [# 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++程序


, ^. F& n- W& k& D
5 w7 }% Z, C" n3 r4 d6 g( C! v
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-16 12:20 , Processed in 0.088074 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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