中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 |! s [0 p. C. G/ r- ^! r#include "md5.h"
1 b' n' A0 ~. r) m
7 c5 [0 A+ Z* h" x( P! F& z3 f, O/* Constants for MD5Transform routine.
*/
$ S" ^+ v2 Z8 X- X8 C% j* A
- I4 {4 V {% x- e% w* x3 U F
4 }. C! `$ X2 S9 N#define S11 7
" c' G4 Z: r ]1 E i/ \ #define S12 12
5 ?" _+ H# X ~. T#define S13 17
) C8 B# }( J* O: J, k; h#define S14 22
! t; c2 r' g+ T9 G#define S21 5
/ ~5 P2 m- W4 G8 U& J #define S22 9
! ?- J& D$ w5 O$ }; A9 U8 F) c l#define S23 14
% K" |2 Y9 f8 d, f) V #define S24 20
5 p0 o1 A' B; ]7 r9 w% M #define S31 4
" U) W" o" Z9 X3 F, j #define S32 11
: ^* G0 S' U0 I* _/ x! V) l+ O; i/ W #define S33 16
* i: ^! O0 Z% I. C$ o0 @#define S34 23
+ X( E& d7 y$ D6 j7 @( l* s#define S41 6
# K4 c3 q- L1 E) c! |/ j% e #define S42 10
& ~& P/ c7 I# n2 ^1 Q#define S43 15
% a% Q5 d* Z" p #define S44 21
) S1 ]& a5 l) p+ G% v4 k3 y! @
# o" v+ [! u( F$ l9 {6 h7 jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
. f( h) z2 E) M, |2 L$ |. } static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! a) E8 U2 y1 J static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ c, j$ S2 s+ L# D% l" z" lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 E$ x9 ] H8 N7 Hstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, `" ]: G3 a" X; Q) c3 H
& ^0 v3 O- h/ |) t ~" R. l 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
6 w& E; a3 M8 W( l# A; E) a};
" I! N E* ^7 ]# a+ w& B, |8 K
! P+ D9 z* p8 {1 y& ~3 y /* F, G, H and I are basic MD5 functions.
*/
8 w. k5 G3 D% g0 L3 T9 U+ X #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ A9 a% C4 q) v" u; a#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
* s- q) P; F; O6 K4 j' Q #define H(x, y, z) ((x) ^ (y) ^ (z))
9 N: R* j. ?! w; z# | #define I(x, y, z) ((y) ^ ((x) | (~z)))
0 g0 m1 I z |: a7 J% G" m$ R
# N" W( e" a# J% D; T* K5 } /* ROTATE_LEFT rotates x left n bits.
*/
3 q/ G6 s6 S4 S( q( z3 R/ K5 Q) {8 \ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( Q. Q- s _' Y+ O
. N6 z: h7 B, C: B f4 k/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 n4 e3 I7 m F0 E3 v- E+ hRotation is separate from addition to prevent recomputation.
*/
0 ]' |3 @( [( e; B$ M( x) }$ 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); \
}
3 g3 l. X3 C* E7 B" a#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. ^& R! n! j2 b1 O& 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); \
}
0 V6 S) ^- ^& j8 I% q9 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); \
}
& d) b. N; j+ b R6 T; S/ P
. D; @; k; E8 u8 o; h /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 u. Y6 L7 N. ` T, s void MD5Init (context)
3 B" m0 [! Z1 ?9 E4 h: U' M$ t MD5_CTX *context; /* context */
; G9 j! r/ o" ]. d9 n6 O& w {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! U! I0 m$ Y1 |; Z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( o5 t4 n Z6 I3 C% ]5 {0 Y }
# T* G, r( @' ^8 s3 q
" a6 E4 ]/ X7 b5 h" X0 N/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 F+ a& }# _, x3 N* t1 [ void MD5Update (context, input, inputLen)
' Q2 `/ Q! i- i. F* \ MD5_CTX *context; /* context */
: X4 c C9 z b. s6 lunsigned char *input; /* input block */
8 A x; N5 @, s/ i$ w* n unsigned int inputLen; /* length of input block */
! }! V* T7 O2 K- S ?' v) H+ Z4 ^{
unsigned int i, index, partLen;
( M: m. a7 O/ X+ J* E- Z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& O/ u9 a& m, D8 }6 B; p5 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++程序


$ o4 x, M" ]7 T6 E/ E
% O# v/ K" x# A4 \+ O2 p% ^" _
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-11 06:53 , Processed in 0.080769 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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