中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 F5 f# l3 b' |, Q #include "md5.h"
: j; P! j& G n9 h& j
, C: z- H& b; X0 f6 s+ B4 o" | /* Constants for MD5Transform routine.
*/
( l. N m. [7 Y/ P+ k5 d5 U6 O
7 r$ s! x# a9 v8 x
: j6 k* s' |' C: E; S" H; t#define S11 7
& ^' N) ~" X8 L: x+ g #define S12 12
x/ X9 |1 Z$ g2 O" u #define S13 17
/ X5 |5 \, _2 y% H# M0 y9 ?#define S14 22
9 F$ y& _1 {) w$ m0 ?) J2 X; S7 Z#define S21 5
! V6 }7 e0 M- ?% N#define S22 9
* R F$ t3 k8 ~+ h5 E$ b/ W( e #define S23 14
! z$ _" j1 W8 J6 d @1 U3 W8 u#define S24 20
9 ^+ M" U) ?( |#define S31 4
% A J j$ x& d" V#define S32 11
# n2 d& c _+ Z8 j! i #define S33 16
, F; H$ l) x- |: Z3 s0 _#define S34 23
4 P! d# w* Y* c X1 @#define S41 6
7 x) T9 g2 z/ g7 T #define S42 10
# }) C+ _, m/ R2 X3 e #define S43 15
6 e v, R( c M7 F #define S44 21
% A2 d! m# `, F# G( u4 ]* z
2 D& B' \' c/ l, A' Zstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ o. ~. N0 `: S' k) e: Xstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% p' y$ U+ p3 U static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! r c5 D7 g/ Rstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ U; z, F0 V( o7 P9 i' Dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 @* \& o( d0 x4 e
6 X$ R; a: Z" k' f+ jstatic 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
2 E4 T: x9 u7 V6 Y };
4 k6 j5 }/ b& j N* L# y4 H
) o6 d: E+ @! F/ n- D/ u /* F, G, H and I are basic MD5 functions.
*/
; M5 Y$ t5 Q% p" _#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
U: m* v& s* p- Y" P #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 E6 \5 w- q# ?1 A; E#define H(x, y, z) ((x) ^ (y) ^ (z))
# E8 N4 x5 @; Q3 d1 Y#define I(x, y, z) ((y) ^ ((x) | (~z)))
$ W% u1 O- j% n' d: R
* g/ e( b) o2 u7 L% @ s% ^+ R" P /* ROTATE_LEFT rotates x left n bits.
*/
+ b! b4 ^% o/ b' L3 Q #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! G6 e4 |8 {8 {% o: o1 ~! h& g3 ~
$ X2 o& ^9 B: P: D: y. K6 C" H /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
, ^ R# F2 `/ H. @+ [$ r; cRotation is separate from addition to prevent recomputation.
*/
9 S# G. M( e9 t #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 ^- ~6 h! X8 t u$ G) B #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 A3 ^: o9 Y. A+ S#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& B1 h5 D. M5 v z+ h! t4 o, g#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% x' C4 g9 \" Y; F* A
7 J3 N. A7 y# E# J. q/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# I: j, k) S) r4 I. z+ G% i8 T void MD5Init (context)
9 ?$ A+ E& F" c+ e9 T MD5_CTX *context; /* context */
% ?: z- b: d/ g) L6 c6 J+ p{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# r% ^; y; ~: s) Z( n */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) r" J" y6 J0 h2 I; F}
4 G8 _' v6 |0 k6 e' q2 Q& ^
! P: A# R: I4 ]; V /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' I- y0 Q- n- k/ c; fvoid MD5Update (context, input, inputLen)
; m" D9 }1 I, w0 ]/ gMD5_CTX *context; /* context */
# @1 x8 m4 ^4 h" z0 Funsigned char *input; /* input block */
' I+ Z- @4 a5 X unsigned int inputLen; /* length of input block */
9 p7 Z# v1 O3 Y8 e. Y! Q {
unsigned int i, index, partLen;
- r* Z8 r/ ^- e1 n1 h% v3 j6 S8 G
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
7 _: } t7 F0 j: E+ 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++程序


# V+ S- r7 Q* a
7 t* T Y+ r R% u5 X
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-13 06:27 , Processed in 0.074386 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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