中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; M K, k$ E/ p: s: i* E/ C! f5 e* H#include "md5.h"
! k0 u+ A8 ~6 l; S3 |& ^
0 N& @3 f! g, ~0 s1 @ /* Constants for MD5Transform routine.
*/
% A, E6 c) J) I- }" }
|, \$ ^3 n# l7 m' X3 n" x
: n+ z* u/ n. {- H#define S11 7
$ Q$ E: Y: G! e! a9 p; q- T- ? #define S12 12
( \, S, Y( R% c9 n9 W#define S13 17
* s" X" @: ?" [5 T0 \#define S14 22
9 d- e$ }/ q& | #define S21 5
, c; n& K, ~. e n4 \2 p: I8 ^ #define S22 9
t" C& b# C8 r #define S23 14
* r J5 ]) u( j5 j2 N #define S24 20
3 d2 o9 S8 _5 Z" u#define S31 4
$ t* O& \: c1 D; t#define S32 11
, G9 s r" M" O% k6 B #define S33 16
. H3 |- N3 }3 g* o+ T& r #define S34 23
: h) ^5 F: M) p+ g #define S41 6
1 R2 ~# j8 z3 ~% Q8 q#define S42 10
8 w' f6 G7 ?3 }) E9 V #define S43 15
# G2 K3 `, e5 ]' H5 }) n# O& V$ T#define S44 21
3 y$ q+ o; D4 t$ O5 w" w' Q `
" Q' V# f. d& j- ]. I! W. r2 m static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 M0 D$ [* c- T, d" Gstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- c$ |" c2 B; {$ }5 p/ A static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 z0 A- j3 a/ O( T static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* D0 i: I s- z5 T. h# J static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, i8 _% N1 N5 ?$ e
) c- X6 w" j! d. o/ e& j) ]% q 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
2 y0 r9 @& [; ^4 `8 x, q};
; X- s( y* m0 d' I7 D) o. p
4 D! o$ ^* B5 H L1 B0 w8 x2 R/* F, G, H and I are basic MD5 functions.
*/
, T& H# h% g7 [4 Z& Q7 O$ F+ _ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) l. C/ ]9 B$ v) C3 p. z #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, D% X4 |5 @4 z2 Z1 y! {% @ #define H(x, y, z) ((x) ^ (y) ^ (z))
; m! [. }' V' u$ l* ^/ ? #define I(x, y, z) ((y) ^ ((x) | (~z)))
# ^; N6 y8 K/ _- W) y. \+ |, H8 b, X
1 p0 o$ Y2 g! F# U) n /* ROTATE_LEFT rotates x left n bits.
*/
0 C( m- Q7 q. r3 c* W8 y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% @0 [; A* I. x, @. |
. [* `) }0 {: X+ W8 V( e; r# ?# z/ g /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; g( ?5 g/ H" L* V9 D( ^ Rotation is separate from addition to prevent recomputation.
*/
9 r L! S3 T8 Q+ z7 c #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
i c8 ^. D3 j7 N' z; r #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" \. k# T" N8 \! B+ W4 p#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! h! X$ `$ N/ y0 f& F #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# _: H* k& w* H2 J1 L
2 V% O0 X0 G9 r; c( _5 \0 Q/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: s* s7 V& m( bvoid MD5Init (context)
" n7 ~+ g6 K1 h2 C MD5_CTX *context; /* context */
7 p! c7 U" j' r{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' o% k. `* d/ z! F. M# R& t' N*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 L% I! s/ D" ^3 X }
8 v+ L! ^- H% Z% J# m: y* q+ ?
n2 f6 R( Y: O. R( b: L0 x /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 Z7 b" J$ B; o& D3 b% T& |& N' ]8 V void MD5Update (context, input, inputLen)
. u4 o* Y. W" k9 ~ MD5_CTX *context; /* context */
5 `6 [% Y3 d. e$ p2 uunsigned char *input; /* input block */
* n$ i/ \: M+ x8 ~: x: E unsigned int inputLen; /* length of input block */
( ]4 g& Z; U# t {
unsigned int i, index, partLen;
* L( M/ M) |3 N2 T# U
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 a/ T5 [1 D8 u" }. t, y
/* 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++程序


7 K: H3 Z- ?- E" I5 L# q
) u. K( g i1 \, `! P) f: N
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-13 12:51 , Processed in 0.059917 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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