中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ q& y3 Q* o1 \6 G0 s #include "md5.h"
5 b; K6 V5 W' o# F1 ~% M( r+ ^
4 E5 z9 R2 ? c2 n /* Constants for MD5Transform routine.
*/
; P% v0 _$ h4 t9 l8 S" K
* I/ f* E* `- g. Y
& C/ C2 N5 s7 d: V% i7 X6 l/ k #define S11 7
" V) M8 q) S/ j, J3 K8 h% ~) E: M#define S12 12
- O* A/ j" N4 a9 E- s' f" @2 w#define S13 17
6 l) ~1 x) Z2 S. D. k* D#define S14 22
8 c. b1 h! d' h1 M6 ^#define S21 5
r9 J+ ~6 X$ O/ N% [" ]# j#define S22 9
# l5 Z* w8 K) S! N* X #define S23 14
# x4 ?/ |& G. i( b, \- t0 z2 \9 F8 @#define S24 20
( p7 K. k' Q% @7 p, \7 E #define S31 4
; p7 l v$ j: E2 B8 n2 T #define S32 11
- {; {2 Q" [( q1 d( `7 J$ z q. a #define S33 16
6 g. u8 P; g0 j n/ U! H #define S34 23
% {% Y2 k4 T* I! r! W5 w#define S41 6
; [" ]& }3 E) w: T% c4 C #define S42 10
0 l2 Y8 K) W' o, `8 [5 S& S #define S43 15
/ I" H. A" t. }" A5 K0 T6 u #define S44 21
k0 I( J0 l3 X0 w$ C3 g' l
4 v% {3 p( ^ y3 N% o/ X static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 s3 ]8 \, V, Y" y) O' rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
" N) `# W; [3 B( i" W/ Z& G, ]" p static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) Q0 R" P" h5 D; h static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' b0 g9 ~" P) O+ M" @; F static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! e I- [. {5 |6 i
$ {& d& B, i: D, C" W$ S" ` 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
# W! M; G" K' N0 D7 M, t4 e};
* j a3 k& c. A6 S3 ]% l
% E+ w) Z& o0 M' e: g* b/* F, G, H and I are basic MD5 functions.
*/
" m+ G- ]6 \5 b+ i! i, O #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 h& k! y& U6 V! K" I C9 s#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 K! F$ O& Q6 o7 c. T+ p* e#define H(x, y, z) ((x) ^ (y) ^ (z))
: m( M7 S' a2 P. h$ @4 ` #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ G2 U; I1 @7 H, A9 m2 J' Z
, L( ^4 `2 o0 V* h' `" w /* ROTATE_LEFT rotates x left n bits.
*/
" c: b1 o3 d) }5 G$ g8 J7 j #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# }; M7 E9 u* M* l
; }7 R0 K& d# D$ W( P3 A /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. ]8 S+ e8 T* t# DRotation is separate from addition to prevent recomputation.
*/
- @! C% t6 _3 x, ~0 @8 B$ 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); \
}
# n) t$ n& R( J0 n( O, d3 ~- n #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 m6 G) D3 h: u' @- P4 `" d& U- C #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* J9 L$ C, U$ s( l6 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); \
}
z% o, e* T- w. r
* m* s! G' m+ n, x% b /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! D D' r8 d4 @4 {6 I/ ^9 } void MD5Init (context)
* N; [& ?- h# i6 D0 e0 S MD5_CTX *context; /* context */
. n' o7 W% L ?; r {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ B) }8 b" N& |% E. [4 k*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 c/ r7 K, ~; X: i1 v3 o' Z}
: D- g& \2 t! i, F& d2 Q0 e
% I7 c/ o4 u. O9 V, l! S# s! v/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* |5 h! o( [ z# \- r9 cvoid MD5Update (context, input, inputLen)
: U# {/ e m0 \MD5_CTX *context; /* context */
4 j8 ]: _& v# M5 n- @& o# z unsigned char *input; /* input block */
; P2 Z# O! k$ b unsigned int inputLen; /* length of input block */
5 }& |) \! t/ M) I x{
unsigned int i, index, partLen;
1 h3 ~# K0 o' D+ ?
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ I N! _! \# K6 X' m- B3 I: d2 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++程序


- g; p6 [$ g5 G/ x
7 x* e1 s8 N8 i) l v
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-24 08:47 , Processed in 0.142589 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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