中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. G* N# L) |: G$ p: i, p; }- V#include "md5.h"
4 l. r {3 t' v4 o- m
" X3 O9 q p0 Y" R, C- L4 w/* Constants for MD5Transform routine.
*/
* m# k! h) B7 E, Q% \
/ A" z! {, C8 i+ z" _
7 N) G4 w- l% \, l4 E& y#define S11 7
{0 h2 R2 g4 E% ?/ J #define S12 12
- V6 E6 n7 i) U2 ~ s ?9 M#define S13 17
* P% W6 v# f4 @, ?8 Q# ~/ u4 H#define S14 22
0 m1 N5 W- y B* i8 H7 u s#define S21 5
- g# O! I9 m ^7 ^8 o1 e#define S22 9
; Y+ n5 N0 {* h #define S23 14
* z; }& j1 y8 p( X #define S24 20
1 s2 `; n, G1 O5 t5 a #define S31 4
- O* \- y6 J! j! z9 u% ^6 x #define S32 11
' d- a# l9 C2 _. h #define S33 16
; N4 Y7 u3 m1 S! d8 v3 L #define S34 23
& S4 q7 q- d q- `5 G2 [7 P #define S41 6
1 f! }" X3 o$ G7 e' H% a4 o/ W- k6 M#define S42 10
$ w# `, l& R# |2 T/ S' O3 ?/ m#define S43 15
6 G6 z7 M! s. a ~0 l#define S44 21
' Z# X: d, u( B! u4 W) O
. p" m+ ^# _9 E/ ^/ d: kstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 y; o# {+ k5 }, d& p2 t$ A, U- Z9 H7 hstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 H( ?) q; P2 X9 L. j' w. i2 qstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 O0 E Y7 S1 y' O+ Xstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ d$ _8 N7 }# q) C+ t0 { static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. R) X, i- T1 a0 R
5 d! F. X- Q+ @( f; ]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
: o/ V0 I. _& _7 p/ r4 D2 J };
+ f0 f: \$ y7 l* ]0 V7 Q# ]
' ^! q. P# `# J" N+ v- \9 r9 } /* F, G, H and I are basic MD5 functions.
*/
* R5 U; i& g$ ]) Q3 c#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 B, ^; a4 j' x#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! l& s1 Y( O4 Q+ ^6 D& J+ K4 F/ ^#define H(x, y, z) ((x) ^ (y) ^ (z))
( j$ B2 z. b4 m* }/ \ #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 w4 j: e; c* @/ X( ]
- ?) G( t* x7 |- j. s/ M6 C( G; v& y/* ROTATE_LEFT rotates x left n bits.
*/
& O9 d3 _ o3 k# c0 N#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- s& N/ }2 D8 K; M) v t
5 f, I0 x4 X y0 l \- ^ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; f/ i$ ?8 \& ~ Rotation is separate from addition to prevent recomputation.
*/
+ W: O4 \/ o9 `1 a #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' s- S. t2 f# T#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$ D# @9 \& M! o: d2 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); \
}
- C# Y( x2 r* p% t6 b4 e3 ~#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; J, l; W/ I
/ N& K- y: Y% m( K9 n& M /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 a8 H O4 B, N5 B5 X6 J0 ~ void MD5Init (context)
0 b$ V) h+ `) e) j! D9 N& W+ v) JMD5_CTX *context; /* context */
8 z" P- [5 g7 |2 y$ v) A$ D{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) Z! o9 }! J1 Y, ~/ e' D */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
0 }. \4 B: L% K6 D5 B7 \}
( M8 s ~5 U! z$ S
. I9 ?5 |8 H4 r# a- @1 o/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
% k2 v+ t$ ~' @- I- J) uvoid MD5Update (context, input, inputLen)
: Z" h: G* `1 s8 dMD5_CTX *context; /* context */
4 L0 y% g7 r* I, E2 m unsigned char *input; /* input block */
! G! u4 ]" Q; x0 c8 B unsigned int inputLen; /* length of input block */
& Z! @4 R8 ?0 J7 B% \: l& C/ b" M{
unsigned int i, index, partLen;
4 A2 [/ Q) e3 U4 g' W) Z) c
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( [6 g! M- R4 e$ h9 ]: 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++程序


0 U7 o7 I, A4 [9 @
. T b" J9 d# p- o, Q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-16 11:03 , Processed in 0.078266 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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