中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" G+ X4 r; Z3 }#include "md5.h"
& u% F0 i( H5 B' \ ?
% u6 z+ D$ Z) V9 Q/ P0 n$ K/* Constants for MD5Transform routine.
*/
# S8 u: H. C& U
% @: \# r# M/ H) A
- v# e! D2 w, w% `#define S11 7
5 I% M% ~+ U. Q+ T* n: v' w #define S12 12
5 a( M4 i, s, R. Y' U0 e$ F#define S13 17
( A1 u' T! d4 C- A! V. N b#define S14 22
( F# J5 b7 F6 @; G #define S21 5
6 M, i9 n Y' F; ]6 o6 o #define S22 9
6 l5 F/ @: E: W#define S23 14
, t: Y. A' o7 K$ S# h5 P5 V5 Y5 `- C #define S24 20
' V f! |3 P, l2 C+ ?+ j+ U #define S31 4
* y1 o1 |* o* E2 b# m. m3 b* G #define S32 11
- ^4 N) A# x# g) x; J #define S33 16
" A+ b% D1 Z4 d* Q#define S34 23
3 G; b6 {$ N, ]5 b! E #define S41 6
+ y) B. s( _( |6 c, T4 j#define S42 10
& H0 a& S: R% D; V7 ^3 L q9 c#define S43 15
2 O: k" b/ j+ v! N; h* W7 @- i #define S44 21
# k9 _; U( G9 L; c4 a
; M. \4 ]6 W2 F4 Lstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
( Y# b- F' y2 e- |" c0 E static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! i c2 a+ ?0 L. z, l# x static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ N4 j K3 L/ b static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 C3 \- a1 T1 l1 J% | static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 }+ r5 \, T( V0 I# w, h4 C
0 m1 f4 |. W8 U, \! q 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
( @! ?4 Y& [# H( D5 E6 |};
2 q, B/ p5 U9 X8 F0 Y, l) w7 v
7 F8 q% F, H* A% N& |5 O+ b/* F, G, H and I are basic MD5 functions.
*/
8 G( Q+ s5 D& c/ L#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 a! J$ I3 b! O$ O+ q- O#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
4 n7 W% c# Q# U6 M. @9 Q& Y" K #define H(x, y, z) ((x) ^ (y) ^ (z))
" C3 \2 J4 R# o- c% q#define I(x, y, z) ((y) ^ ((x) | (~z)))
+ ]( u. z- X; K
6 n+ s6 s* f q# \- S1 n. Z /* ROTATE_LEFT rotates x left n bits.
*/
) l8 Y- y; v' v0 H& \. G" H#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 g j( ^/ @& M0 l! k
I) k5 K0 Q1 n/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! w% I$ @# ~ o: ~7 x# m Rotation is separate from addition to prevent recomputation.
*/
! Q# v5 L% d T% ^6 ^& q0 n- O P#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. e& g' E( {7 N3 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); \
}
3 h' P1 b& R% T/ 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); \
}
8 r/ u! L; D& a$ r7 C8 T& V1 @#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" H$ G) ?( t; K ?% |
6 n' p+ W6 P3 p /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* w- [. E7 Z4 J% g9 w) f3 } void MD5Init (context)
' a$ ?; Y% h; t5 E3 l5 a MD5_CTX *context; /* context */
. j6 l% S9 t, d" _{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! J4 V8 N( Z) q6 y9 C5 o$ I+ x*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 x Y/ ^2 O. Q. B% M0 \7 n}
0 }- Z4 J4 g/ ?1 U! e
8 |. Q: \; W1 }3 V- k7 R /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 Z$ N7 Y9 I/ A9 C, O% o p9 Ivoid MD5Update (context, input, inputLen)
$ b7 i! q4 B+ u- [, h MD5_CTX *context; /* context */
" e8 X a q8 k6 ?0 I! g& \unsigned char *input; /* input block */
; A/ K& d1 r6 {4 Dunsigned int inputLen; /* length of input block */
' Y- i+ E/ Y. H8 Z: j{
unsigned int i, index, partLen;
& R3 E m5 e7 C/ ?5 `1 Y# y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 y) ]5 F, g# w4 e
/* 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++程序


. B( N+ z7 |- N+ D
( l) o# ?8 A2 F9 @* e ^$ a. F
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-30 03:09 , Processed in 0.095072 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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