中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
1 U, z* i- y; o |6 g) W #include "md5.h"
K6 Q0 Z0 P V# g
7 G" ?5 C+ c, q( M" s$ P% M/* Constants for MD5Transform routine.
*/
3 o% `0 V/ T5 v a# r# H; o
. x5 r" o& R+ ]7 V2 M
O7 b: S8 y/ w, \, x3 L( [ #define S11 7
+ b0 g1 b6 ~4 Y$ }- z; ?#define S12 12
- L5 D& B0 |. h. n! R! n! I$ b#define S13 17
0 L5 W7 I2 G( P+ F4 J, j! _ #define S14 22
/ k" U! g' }7 z( R4 V #define S21 5
# [9 d7 W, f- r4 Y1 Q% a#define S22 9
* L' |) t i" q. M7 m! v #define S23 14
0 ]+ w3 E* N8 \" y3 j& e. A#define S24 20
( i. G5 U* c2 H) }( j! [#define S31 4
' F/ A; k) i: N! n8 r #define S32 11
% G3 s2 }5 G0 t, S7 Y#define S33 16
" i% w5 n |+ j& m8 ^. f$ ~ #define S34 23
3 f$ H# H' u5 e( G9 \ #define S41 6
. P, s/ t1 A6 E. Z' K3 O #define S42 10
$ S. F7 i: p* j8 y$ l! t #define S43 15
2 f5 s8 X( h `4 i) m* J7 l6 u #define S44 21
1 \% o- u) |% h6 z# G
. h1 W, y" l f9 pstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 q3 Q& T; b0 i! `+ J* N1 b static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ G9 H8 u/ q& {: [. U; ustatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 ~ t4 b0 E; x% U8 D8 {' cstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
: u3 s6 n s) ]- i3 U& A" w* Jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, Q9 x3 U9 X. N. J! ]) u
3 w% Q8 n/ f0 K, G4 y( xstatic 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
( u1 W) U' U6 O$ O5 N6 P };
. [+ j5 _; V, u. n
9 Z& k' F, W8 k1 q9 ?9 ~ v /* F, G, H and I are basic MD5 functions.
*/
$ ?+ v) t; u4 a8 Y% t3 N5 F#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 c- Z" Z3 n1 e6 P9 [+ @2 R# l #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
4 r2 R# J& H, l. p#define H(x, y, z) ((x) ^ (y) ^ (z))
) U; U6 O* a) B* B! u! J. S#define I(x, y, z) ((y) ^ ((x) | (~z)))
" Q# D* H9 G' d1 F8 d4 h( R2 v; h
6 O5 M" s8 n3 T# O9 H /* ROTATE_LEFT rotates x left n bits.
*/
; \1 o' L* p8 R8 ]7 d0 A #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: R% m3 {9 ^. F4 }' {
7 u9 n1 J: s2 R T* ] /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 |# x1 P, h9 U1 R$ IRotation is separate from addition to prevent recomputation.
*/
6 e }; u2 s, C# g& i7 B2 @% @#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 }% {+ Q3 ^. N% F- w! o& b4 S#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 \8 x8 C' [/ a, o, I6 K#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* T# `5 ]: R( a+ v& e3 y* Q #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
e5 L% g8 U% o
& W7 r0 ~6 Q9 ]: o4 r/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- ~$ Y3 g' \$ l void MD5Init (context)
) S3 C7 ]$ Z/ @MD5_CTX *context; /* context */
$ ^5 Z+ U: U4 N) z. ]9 E{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
7 D# y/ D7 x1 d7 X */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* |, A# Z" ` s9 T" U1 P; X }
3 j' q% G% w! @5 a% w3 b
, z+ r, D3 p4 T/ m; W% g2 R% O w/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 @0 W, d @3 Q5 _( |: Z void MD5Update (context, input, inputLen)
3 s. z0 @' N& W MD5_CTX *context; /* context */
& I3 ?- f. a' _" L unsigned char *input; /* input block */
( }; g, O' X- p, v, ]. E- V8 X unsigned int inputLen; /* length of input block */
3 C- y9 c' U2 y' ?{
unsigned int i, index, partLen;
: v. H( S/ I o# m) g4 x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 p% @# A/ o3 P- {
/* 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++程序


: U* c4 \2 L0 c# E, i, N
3 U( I( e( Q8 f( P3 q- C
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-30 19:16 , Processed in 0.083997 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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