中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* z: ?* I. ^& U' g% |/ ~#include "md5.h"
+ s* v5 C# U2 Z; }0 }' k
0 x X Q) y5 F' R% \/ F+ L/* Constants for MD5Transform routine.
*/
l. g) _9 X$ h0 P1 u" G' V5 [
( ?+ a( }& A7 ^ B/ `
- h( ]( \# N- { s+ z#define S11 7
& j/ S1 V' R0 a6 _, y#define S12 12
) {: h/ K% K! K, r0 K#define S13 17
9 V# }0 x) U0 n2 E6 L #define S14 22
3 C7 S5 o% h5 z8 v: v( {#define S21 5
8 l: c1 p( P2 {& c/ Q% A { #define S22 9
' N8 I f! S6 F+ I O E#define S23 14
& T& J0 t1 i/ f. A, K#define S24 20
q6 x+ Z; }# x/ r$ i# [ #define S31 4
: }# V4 m! ~: n) K& u$ N #define S32 11
2 n3 ^9 L3 H A- m #define S33 16
) o7 H# z3 W6 _. O( g; n( h( A# ?' ] #define S34 23
' Z& ], f# {5 h #define S41 6
9 h% y& b; `8 R( U, E6 b0 {# t#define S42 10
' q' _$ `* J# i$ C7 @3 q #define S43 15
( s6 S* @, t# u( x- L7 X Q8 N- \ #define S44 21
; O8 @; g( Q9 B- E6 _7 p* D
* J+ q. u+ B, ?! m static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! c, @! F+ V' E' x- A8 G& i2 c static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, B' K: O2 |: u( V# } static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. v3 T8 t3 G+ y( o static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
R: N6 S6 e. H* |. [& ?! ostatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. [) E* Q, ^8 w. W; U% b
1 k/ U8 u1 N' e6 P5 l# T4 O 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
, c( m& ~" T0 X: I };
& a( n3 }2 E' u' ^7 w, `0 ~
3 z5 m; ]. i/ H5 w* P4 K7 M! h /* F, G, H and I are basic MD5 functions.
*/
[* ^ q' g3 d+ G" |9 C #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ T2 P5 Q7 }$ }" M& M#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; U$ T. Q% s( w% f/ } #define H(x, y, z) ((x) ^ (y) ^ (z))
# w3 X# L1 O- i #define I(x, y, z) ((y) ^ ((x) | (~z)))
# O+ }4 b3 Q" W4 e [! Y- d
C( v# p- i3 q9 R* i/* ROTATE_LEFT rotates x left n bits.
*/
( w# M: D- B4 y6 i* G- E$ z#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 Z9 L- E! a4 P
0 o; M% g8 L3 X u- H6 `/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ p1 Y9 y1 \! C+ B) @0 z; YRotation is separate from addition to prevent recomputation.
*/
/ y2 O. x/ X+ J& r& g* {$ j1 D#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ Z# B0 r `. c' ] #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 q2 ?. M! ]4 P6 x#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. R4 q9 N& J( ` B) x2 G' h3 i#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: z5 P/ @3 J1 u
5 s8 Q- t$ a4 ?9 v, x/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. ^1 D- S) x( F6 r4 e Uvoid MD5Init (context)
7 L) O5 ~9 Q0 b, i' R- w( zMD5_CTX *context; /* context */
1 i6 k8 a, a. U2 @+ j{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
- O8 N: z$ J6 T* T3 Z*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- E% f. ^, u& z3 M}
- w7 O1 U1 m- A
* u& u) }* L# U- j; g# ~# c /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! }7 g! Q, s" |) P% B void MD5Update (context, input, inputLen)
3 M" t- T+ ?$ ~0 qMD5_CTX *context; /* context */
. b% w1 ~, }$ qunsigned char *input; /* input block */
: a8 y! b1 F4 q# V0 s+ M unsigned int inputLen; /* length of input block */
, D$ ?& k" q; o; A' U C {
unsigned int i, index, partLen;
0 }' B! X5 o$ p X. T: \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 {8 _, L% }3 r5 A. D5 t
/* 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++程序


7 S0 r: j# b# ^: Q
& z4 X j+ p4 Y* m! k' ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-11 23:14 , Processed in 0.060297 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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