中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& |$ R9 V) |9 z( V( L6 O' [#include "md5.h"
8 P$ _# P1 x b& o
& u) |7 H7 a1 }/* Constants for MD5Transform routine.
*/
. ]6 A1 ~9 Y, A8 R% I( `5 T: i8 o
8 g6 {8 z- U4 b9 m' v5 n+ p, G
k' m! L! s2 o; o5 x #define S11 7
+ F/ Z5 {/ v- l# |- t, U3 K#define S12 12
! \4 T# ~: V) i5 n3 Z4 E$ D6 R#define S13 17
4 B+ v( N8 |* o1 H7 }#define S14 22
' D9 [$ ]; j! p" {/ y+ i' n' E#define S21 5
4 y3 ]; f1 v& @+ s' J" p #define S22 9
0 k4 h) h' R' d& `- I# g #define S23 14
* h# d7 Q, K4 V3 b#define S24 20
# r4 G4 j& L; |6 O+ S7 P #define S31 4
\. {5 z6 ^. n! ^1 B #define S32 11
. A/ X" y5 T0 C #define S33 16
5 ]! N5 |* Z! ~- n2 `# y2 T #define S34 23
: Q& K5 J, W* _3 t8 G; a8 m+ B #define S41 6
8 E |% ?# k% Q3 Z ~#define S42 10
$ g& ^. ]% Z% i" K) d" C+ Z" } #define S43 15
2 f H5 j4 {5 \8 M5 f: ~9 U% q. u- B/ \ #define S44 21
s9 ~4 U+ w: d3 }# K; A
; |8 R2 ~& Q9 [+ Y3 x) `$ C static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, @/ G q z! w static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ U. T' E3 X. O0 hstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 |6 e: y- H6 c# h' [" k, [5 q" y2 E static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ ]- @- q: N4 gstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 R( Z1 t* x5 f7 E) q! L2 s+ T
" h. N! Y: W9 f( ^- 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
, T/ i8 x- l5 S3 J5 Y7 k3 g8 E};
: x; G7 ]* H- ] a# R5 h; @& k
" `0 t1 d. X( X$ b) \' y /* F, G, H and I are basic MD5 functions.
*/
1 I, Z% y6 c' |# ? W z+ x5 c: p. K#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 z# ]7 V4 [, }* X5 a9 V- Y; D5 Q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 W+ r' v: [1 J. U9 p) N: d9 C #define H(x, y, z) ((x) ^ (y) ^ (z))
! ^9 K( k0 o/ j& n0 t #define I(x, y, z) ((y) ^ ((x) | (~z)))
- `: O7 r) o- X3 y7 |, u% l
! t# a6 r, x. p: n f$ r' H+ ~ /* ROTATE_LEFT rotates x left n bits.
*/
$ }5 L0 s1 |( y; l# K9 M #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! v& S) [: [% Y2 |
: `. _* h1 C. D( u1 o /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! K7 S e5 v0 ]2 IRotation is separate from addition to prevent recomputation.
*/
) q8 g9 c- V- J2 b #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 N7 q5 L; P7 Q" n8 \( R: 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); \
}
' I; ]2 m$ ^3 ^3 c8 {2 ? #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' e' E2 b% |" G$ n3 N7 } #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 C$ W; f$ ?
5 Z7 k# Q4 E$ Y% D% K. G. l4 ]" ` /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 n. a% g6 X3 m void MD5Init (context)
) k# s, K7 R6 C" E8 b3 L$ j% q MD5_CTX *context; /* context */
& p' f9 J( G8 B1 E0 H$ M! G {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) i& O7 c+ K3 n' r I& ^. f*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; y; x0 g. A6 C5 ~; k9 q5 u}
- u0 y) c! W, m% w) i5 L1 X
9 s: V4 a2 C4 K! A' \ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 w, r( H% u# a5 E& j; kvoid MD5Update (context, input, inputLen)
) L6 H) M- n' j8 QMD5_CTX *context; /* context */
3 w! y0 A+ {) q& p( j( U unsigned char *input; /* input block */
1 K3 Y* v2 a+ j' b. b unsigned int inputLen; /* length of input block */
& j$ j* X0 t# [* ~{
unsigned int i, index, partLen;
& G3 _8 l- q9 j" k- _- a
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- A/ `) m. L+ T! M' a' j+ E$ J* 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++程序


, N+ ?( L8 I. ?0 F b! F. f: {- s
4 }3 M3 u% g" d2 c; U" \
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-16 04:57 , Processed in 0.080467 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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