中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! X+ i6 J$ L/ Z5 v#include "md5.h"
2 o7 J% E5 i- Q; y [7 v
! }; \- I6 g4 _: ~1 \/* Constants for MD5Transform routine.
*/
# E# U: _* [! a, h5 |
: @; h7 W% ?4 C2 V/ T3 D2 P+ I# \
3 t& T8 f( r% Q9 X8 e& o$ B2 s3 Y4 d#define S11 7
, Z3 N1 @' U6 B#define S12 12
) G5 e1 ?5 R! p+ d#define S13 17
6 C2 `$ [6 f! q3 \8 a z$ J+ d #define S14 22
j+ _% N# ~# \# V, Q. n #define S21 5
, b0 l- t" M. I/ \' r#define S22 9
- B# i3 [; K5 [& b! X#define S23 14
2 c G( S6 }; Q: t #define S24 20
3 G& {2 X1 B6 U1 P/ m2 \+ A #define S31 4
# H( b; R t4 j1 H l, V #define S32 11
, {# u& d; h: [# P: s#define S33 16
. X- M; ?2 y- v9 a) }#define S34 23
% D1 y2 U& k0 f! l #define S41 6
3 L# o* f, n9 E' i( F9 c; p. C #define S42 10
! x; |- e: B0 E9 d4 n! ] #define S43 15
$ Y T! b5 S; D) b! d$ b#define S44 21
/ K( ^7 L8 E4 N2 T# L$ }
- E2 t0 i+ C5 d! Z* j7 _5 Ustatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* }3 t" h3 h) A/ C' J1 w: h- p static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
0 d# G0 y6 c& V9 W. [- x! Qstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% K) P2 W, _8 y& rstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
: u2 t1 }( v# O+ P% Wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 n5 \" |. Y0 q/ m' ^' ~
/ N9 m/ x! U( Q: v* Ustatic 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
! P$ C- i6 j, S3 b4 j" T3 V };
4 W. c( {$ U5 y4 h! m$ N
5 U: A; o' S1 s/ @ /* F, G, H and I are basic MD5 functions.
*/
9 P& I( y* w; |# [- o #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 A) |+ k9 X! c: U8 D #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( h" A4 c+ F* I. i#define H(x, y, z) ((x) ^ (y) ^ (z))
9 l. B' p# ^- H; T; c2 v3 Y d #define I(x, y, z) ((y) ^ ((x) | (~z)))
: q3 X& P7 i3 p3 J. T1 ?
, A# J5 m& X& j/ X4 W9 ^/* ROTATE_LEFT rotates x left n bits.
*/
1 a8 I* a+ V& H. {" K" F#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% W% X Q1 I% j1 G7 F
! B5 G k! H0 c, E# N6 Q' P/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 ~ X& v' Y: h0 ^3 z J8 yRotation is separate from addition to prevent recomputation.
*/
5 f8 K2 c; T! v7 J, F#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
b% R# e, n- E; c& e3 Y #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 }3 T3 g3 B" B. ?+ { #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& Q! o9 i2 k L' C) V& I* N ~#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( u: y( y6 s% o3 U* ~ R+ L
, v1 `' R- F4 j8 A3 J/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' t8 o; J) G4 u6 \ void MD5Init (context)
* `" m8 e6 i" uMD5_CTX *context; /* context */
( e( ^2 [7 |. n4 V2 b0 w# o {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 ^. \( i4 s* ^, F& i */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. e2 r8 \6 S% ~( F6 m4 C3 t h}
9 O+ l% ?& t+ w J N0 g
/ |8 K3 m* c9 {& {# w /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 I- L* E# {4 w; ^ I void MD5Update (context, input, inputLen)
% _0 C4 ?7 w1 tMD5_CTX *context; /* context */
+ ~: K& C# l, a% `& n3 A8 ]. D unsigned char *input; /* input block */
% I% I8 Q+ X1 ^# g8 Q9 S4 Z- i0 |2 Zunsigned int inputLen; /* length of input block */
7 w) m) N, h! n3 X{
unsigned int i, index, partLen;
% K2 n6 Q% H. I
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 V* c' h) l; W" @2 g/ P& p
/* 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++程序


) T9 M' `5 ~6 B8 V7 E5 ?* S2 M
9 j# G w% f+ u- G: d/ ^) M
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-10 14:18 , Processed in 0.292969 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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