中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 `0 I9 H1 n: J$ T7 u9 L3 ]' S#include "md5.h"
5 D k6 N% q; v) } X4 B" x! P
: q0 }- Z0 s. V' Y, T8 S/* Constants for MD5Transform routine.
*/
; U6 X% k, f) ~, u* F
, F9 @8 F k# J" }: u4 `
! x$ I4 _% p) j" J0 E #define S11 7
. c/ V$ g. t3 H* w9 j #define S12 12
) O2 V% }% p. i) g+ y, v& t #define S13 17
& |. b: r( t3 i#define S14 22
) x8 {8 w$ S: \3 E# ~8 r" @#define S21 5
; ]/ w$ U j) E" R, n1 }. F#define S22 9
3 L, w/ h2 m+ ^0 G0 v #define S23 14
: X7 E7 x" y9 ] l& Q2 M#define S24 20
9 ^0 I/ O1 h' t# ]" U8 U#define S31 4
. ?1 |% V' e& i% D' O$ P #define S32 11
* R+ Y5 }! W& n& t7 A#define S33 16
Z3 W) {3 W! D$ c#define S34 23
, h& K# I: C* K6 y! b6 j #define S41 6
5 v) R! i5 t8 p/ o9 @+ t#define S42 10
( @8 `- l+ D8 { #define S43 15
$ n% j; J2 ~( J( X #define S44 21
# _# n1 u& v6 p
) Y- Y/ c7 K% L: q3 R& cstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" ?) o: \! J% _4 {9 Q E6 S4 ^ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 t) |8 v3 i2 h: t' C1 nstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ w. I$ U+ h6 L; p5 A9 D: f e4 c static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 ^" q0 u* w8 S: Y$ H static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 i( ]" n/ x0 u
5 o, n# ? p6 G" H9 b4 ostatic 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
, I c" \7 E1 J! V- \' w" S0 U$ {};
2 l' z+ m- E1 x0 S- _) t4 T& B
; G/ f+ d9 }5 W5 S' Z/ p. }7 X/* F, G, H and I are basic MD5 functions.
*/
/ W3 u, s6 N! K1 u" x #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! r2 N2 p6 g. ]# t# s* n#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( e7 J+ Z3 V w$ @ #define H(x, y, z) ((x) ^ (y) ^ (z))
9 F, ]; m- ~; O) l4 h6 h& i #define I(x, y, z) ((y) ^ ((x) | (~z)))
! {) ~% D* i" B- O x7 ^/ d
g4 [1 r( r# ~! O, L. o; d/* ROTATE_LEFT rotates x left n bits.
*/
; L4 h X% D, R" A; a+ V& q #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, m, ^) o/ Z {0 W; ]' N
. x( W: U8 v5 P7 D( A# P /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# B% A$ r7 Z6 [9 R% URotation is separate from addition to prevent recomputation.
*/
# Y0 L0 B0 }+ l #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% A9 H5 N: s6 R! [8 v #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" ?8 f4 P: I: z6 Q( M# t #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& w, z1 V# Z# K( d4 G9 s#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( o/ g, }1 X$ W* z5 h6 K
$ c- G2 p! e, r0 r; Z/ X6 T /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# |# F% ^' Y( xvoid MD5Init (context)
8 ~% F# _6 ]0 }0 n8 L8 v( I MD5_CTX *context; /* context */
) I# {+ k7 [$ x* V$ G {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 O$ e' Q8 _4 P */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, W. r5 i) L" I% d2 {7 _}
' m! Q7 T( ~: C
# J- i3 h7 P0 {. P /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; q& x4 X- U- tvoid MD5Update (context, input, inputLen)
; W2 n6 ]1 o( |9 f0 O( t MD5_CTX *context; /* context */
1 C6 Y( d! l/ h' M) q- \, w2 B1 Aunsigned char *input; /* input block */
/ Z1 I% T$ d1 X7 o5 j; }0 eunsigned int inputLen; /* length of input block */
9 x3 {& k# I* x6 v4 @: Z{
unsigned int i, index, partLen;
! F4 l2 K4 Z' j" | H4 A7 b, t
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, X) j: m9 s8 r
/* 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++程序


# Z' @. h6 H: ^2 s
1 p" Q: _1 n4 e5 H: m$ L
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-5 22:07 , Processed in 0.060214 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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