中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# Q6 @9 O. h$ F1 h& u, u #include "md5.h"
% U2 g: \* g+ Q2 \# J4 X/ C
6 o2 N( b. P" v/ A) d I4 T /* Constants for MD5Transform routine.
*/
. H8 ~% ?5 T) n' ?8 a6 z0 W" ~
* `, r" Q9 f5 c- k1 t
% o0 q1 y& H) w& P% c" r#define S11 7
) @0 Y# j5 \0 R( r#define S12 12
" i; `4 W2 u/ {2 S( A! k#define S13 17
' K0 e4 b$ [0 |/ |9 b( a #define S14 22
, \9 A* L V/ x& e3 A#define S21 5
9 Y$ u* ~% O) Q+ m) H, |% }3 e#define S22 9
, T% |+ r r1 h1 }1 {/ y#define S23 14
3 a/ z- ]+ D" I W1 J/ n7 [) O#define S24 20
) u$ Z9 f4 k5 a; V- Q/ @ #define S31 4
p9 d' t- p4 Z' C8 d5 t #define S32 11
3 G' Q- v9 n' K( h: G; O' K/ S #define S33 16
. h: G/ n j3 L% M" s #define S34 23
- B. q1 M; Q% Y2 h w#define S41 6
% p: q! m) @2 L6 }4 S #define S42 10
& t9 u6 u4 D0 r& m8 V$ J! o#define S43 15
) q. G! m0 y* m" L& Z: H g; {#define S44 21
" c5 o6 r, M+ i: I8 P
2 [: i2 J1 E. u/ X! ~static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) [' P1 ]6 X5 T estatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
5 ^1 ]$ `& p3 e3 z9 q9 D static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! M0 ^4 T+ U. p. a) I, H7 _1 cstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. ?7 { y$ p' ~8 j! v* p5 t0 r9 f; h static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 ~* ]6 X$ Y* A; n
: c, Q" E1 ]2 L b" L6 t' 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
( e! F5 {$ z N. @ };
5 r' D' @' v' i; Y" x3 S
2 W; x! i; e c% v/* F, G, H and I are basic MD5 functions.
*/
. Y- I* P7 v4 {2 \: ^9 X/ c/ c #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- F6 q r: @0 y8 @+ x2 |#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ k- P9 G' @; d8 p* L+ d; q, [6 j#define H(x, y, z) ((x) ^ (y) ^ (z))
) e* v3 P9 d; i) [4 N: r#define I(x, y, z) ((y) ^ ((x) | (~z)))
$ t' H9 o% I f% j+ j4 B
6 @: X* H- P4 B. x( ?4 \) I [; ]/* ROTATE_LEFT rotates x left n bits.
*/
$ B6 l, s3 J9 l! B# p$ Z#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ C6 E2 l! |$ P( L& ?
! v' w7 t; a9 G- C' g% b/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
N' i+ b* a6 X6 T Rotation is separate from addition to prevent recomputation.
*/
0 b- B0 @/ W' j# }& g z# U#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 ]3 A& n i; V" [0 t* m#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 T R2 ~. r: L, o9 V1 q#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ y+ p E$ l; s, c3 Q/ k#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 y- y. k [" [' @. T
3 M" y& _. p1 n6 s# U) x- w /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 d/ o& R5 X6 N, c9 {3 c$ k# K: V' [ void MD5Init (context)
! E1 \7 e5 W. v6 ^ @, V) JMD5_CTX *context; /* context */
. u& _' L+ [$ U+ Q' ~" V1 b! A- l {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 t/ b+ L- u, U6 [" s* V */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. }9 {' f$ \( f. e3 g2 p}
6 w7 h6 _/ ~ m
/ A$ p/ H: x( L( e8 Y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) e2 Y. K8 N4 T6 G# W* E4 G jvoid MD5Update (context, input, inputLen)
- t) C& a; d: }- B1 J% a4 f. w MD5_CTX *context; /* context */
- P7 ]# \( ^0 c& V+ d unsigned char *input; /* input block */
5 ?( r# B7 X' N0 R6 X! gunsigned int inputLen; /* length of input block */
% K* Z+ f% ?+ @{
unsigned int i, index, partLen;
2 l7 o: e$ l9 G" H3 V
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 y5 q1 ~% X& b: p* i- k
/* 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++程序


- k1 u5 q" E7 {: V' Q: J. ^
7 W' Z0 m; m6 E% V& E) X
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-25 22:56 , Processed in 0.071235 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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