中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) I& g, ?% D' w#include "md5.h"
; o) X4 B+ W$ @) Z/ e, U
2 i" _4 B9 I% u /* Constants for MD5Transform routine.
*/
/ v( U7 D. S s2 O
. J4 o+ z( d* N5 g6 G. @ a4 O
1 F& ]5 U. r, z, ^#define S11 7
0 n9 ?' \, N2 Y+ A. P4 M; S! W( T9 N#define S12 12
' W- g6 i! n6 u% @#define S13 17
; C, D* j5 Q+ U0 R5 f- Y" a) o #define S14 22
s" T0 ^: O2 s1 s #define S21 5
5 {* a6 S& J( c+ y' N#define S22 9
2 M* I/ q2 F2 G+ U/ |8 c% `#define S23 14
' ~& v1 G W, X5 c #define S24 20
( y( U$ a/ f# F) Y' F$ ` #define S31 4
$ ]* }9 a8 y; k* ]6 p #define S32 11
U1 |% F- e2 V #define S33 16
5 f6 ~: ^% t% A' Z% N; I' S* e #define S34 23
- P! j7 j5 V! v& Q#define S41 6
c' }( G4 s* s, D6 P/ d#define S42 10
- {# m Y O, Z" Z#define S43 15
/ b4 q9 @* L' ^# m. G#define S44 21
8 q. s" o: ?4 \5 G% p% x) ]" o* G
3 p5 t( a' [" {7 J' wstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
. o4 \( _/ H( Z' `2 C( F$ J7 ostatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
* q* `$ H! j/ I; M static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# h% z) [2 W* s) p- m3 } static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 G/ O6 w9 `0 J0 u8 xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, Z6 c8 j' | T, K( k
1 D0 D4 C# N7 V1 T 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
& r4 K* j6 \: f& l};
9 ]6 h9 b' ^! L: n
0 |3 ^2 P& y5 E. W4 ]: R/ s* H3 I" S/* F, G, H and I are basic MD5 functions.
*/
( k4 l1 f, \5 K# L7 d$ \% T' P) T #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
9 M# H" @- \1 ^. a2 m+ U #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# @4 c" I6 B5 I( [7 y2 z5 X, P#define H(x, y, z) ((x) ^ (y) ^ (z))
/ b# }1 V8 U h: c #define I(x, y, z) ((y) ^ ((x) | (~z)))
$ \) N% q2 b! k
. w' @: v( A/ V7 E /* ROTATE_LEFT rotates x left n bits.
*/
# y' x! m: b8 X' m: `4 T4 c9 b #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: r, ~, r0 h% j! _) ~
0 [8 {3 j. F+ Q" ]; B4 K /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& n9 x/ t& B7 q% e Rotation is separate from addition to prevent recomputation.
*/
6 u0 b. h3 b6 y' |# `% v/ M #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% E! s6 f! a: o9 o' ~; K2 z! l- ^ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* J4 C# b5 M; j9 Y4 f #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# [ ]; A$ j6 `7 H#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 Y: I( [: r: X: B: [. M
+ b1 f6 [. J7 z: n/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- o' T/ V8 N4 J# y( S& F2 z void MD5Init (context)
8 f; T6 j9 J2 y' Z+ y* |MD5_CTX *context; /* context */
A9 j2 t7 `' S. m; W) ]0 b{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. [6 P3 y, }/ m*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 @% V" U# Z. q- ^+ O* Q" z) H }
' Q: k6 u( t4 N; l" {4 g
4 s/ @( _" t1 a, V& w5 B# q/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' b( ?( G( \9 B b: k5 }2 _0 gvoid MD5Update (context, input, inputLen)
8 o1 c% d# W2 I1 l. M, n' b$ A. L% W& c MD5_CTX *context; /* context */
) g) G$ S( C5 z' P; o1 Iunsigned char *input; /* input block */
- L2 c, I1 O. ^1 a9 }: v unsigned int inputLen; /* length of input block */
' U- }2 p3 g2 T+ B{
unsigned int i, index, partLen;
3 R: r9 c. a# R+ V" J
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
7 H: n2 K( _ `( k: w9 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++程序


, l7 Y# ?+ q' i3 u/ f+ F
2 |0 r% B4 l$ L- \- S; L, H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-10 06:28 , Processed in 0.074533 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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