中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ x$ |* A* [' i2 } #include "md5.h"
5 Y6 O$ w7 k! d9 ~
" c1 c& ]; k8 z3 v) u6 i /* Constants for MD5Transform routine.
*/
# B+ u5 X2 m6 e
& e/ y( D( z S# e. Y4 K, V; D) B
1 {3 B9 s: e G" M/ `; G#define S11 7
4 K0 H7 B7 J ?4 x( D3 Z- v #define S12 12
0 A% \+ [3 K j. }( Z+ \ #define S13 17
, O7 G3 W* s( e0 l$ s#define S14 22
6 ~& `9 E: K* H6 D2 l9 n" l#define S21 5
! G0 ` n6 h. R7 A/ V" d) G2 t) U#define S22 9
7 u+ @1 p# r- a r8 L# ]# u#define S23 14
* A1 C, C7 z6 `/ ]) Z#define S24 20
~8 @$ a: U' q% ~9 {#define S31 4
2 s" C" P: h! U/ v, N& y#define S32 11
! K* z" f) X9 e/ m$ Z #define S33 16
4 Y- l) c' G: _- B5 b; a* P #define S34 23
. y" M, Z: f9 r* P0 h/ O C; g#define S41 6
0 K( W6 r( M0 [ #define S42 10
( [' ?. q B- b B- s2 x #define S43 15
# D* f8 |$ C( ?; g#define S44 21
~+ c& e ^6 Q6 b* `- z
$ z1 K4 w/ P$ C# o" Kstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) L0 @. G, ]" i) L. J: f4 R; Tstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ |& ^ j( f9 \) _9 M! g static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 ~" v- g3 X, t8 a' E3 X$ J static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" [$ |/ W$ f+ ^static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
/ t, Q- T7 l) L; j
* Z- n2 _. ~6 A. [9 {. ^/ v& tstatic 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
% N% S# X6 |' A. K8 G4 J, S$ l};
/ J6 N: J- ?( e- ]
2 G1 f/ t/ j; g/* F, G, H and I are basic MD5 functions.
*/
- W# |- ^4 @- X) Y( x# Q _#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' O& E5 V6 u$ M1 |7 f" }" N" G#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
W- w2 X9 L' B8 n& ~/ d #define H(x, y, z) ((x) ^ (y) ^ (z))
8 g* R. ^8 a0 I( g#define I(x, y, z) ((y) ^ ((x) | (~z)))
. z1 s/ d" B) a, v# z C
! e$ _3 p% F" c( t7 X: p3 I6 U. ] /* ROTATE_LEFT rotates x left n bits.
*/
# T2 o; R5 K7 ]7 |$ Y6 } #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 x) V5 u @9 P4 U9 N, L! H% A' W% b
1 U9 [' c0 x6 U! x$ z' Q' I ~$ Q/ A/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: E4 g' c; |: ^0 m: k8 P. N Rotation is separate from addition to prevent recomputation.
*/
! M* r) t+ ~( h+ F3 z#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ r& o- ^: J+ |1 j% c8 Q$ ^ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& b" C& x/ v; M' |5 A #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, B: U' n$ r* ]$ q7 n+ C5 v #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 x- Y9 w2 N4 p1 k' v
- w: _( \, s" X4 K* m/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! L' b- K6 b: z; n& ^9 B9 Ovoid MD5Init (context)
/ z! L( ]& J- K. T/ m4 o MD5_CTX *context; /* context */
: w: l8 F' U4 z{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
- _' G- \9 L% M, o5 F! x */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! { x7 F; H3 e3 _+ a5 t$ l}
4 q6 [) {$ k$ \. Y
) A6 ?1 `1 h' a: V; Q4 r( p& E8 K/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 z, W2 x1 u" `# }0 l2 W1 o void MD5Update (context, input, inputLen)
1 `! T1 I4 q2 y- x1 ?( h8 uMD5_CTX *context; /* context */
% c2 \+ `' B- V" `6 c# }7 Tunsigned char *input; /* input block */
6 ]1 {! X7 c, Y/ q unsigned int inputLen; /* length of input block */
0 x6 h q2 ~0 L; `4 d' y. V# G{
unsigned int i, index, partLen;
3 x) S" M1 d4 h& ~ y0 P3 ]
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( ?/ q8 `$ r5 b& U" W, d
/* 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++程序


$ k+ h3 a, z/ n. P) \4 W8 [/ ~5 d
, J+ f$ |1 U* O. F3 B% p
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-2 23:41 , Processed in 0.073560 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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