中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& S" i0 e6 H: O2 ~6 y+ o" c% _#include "md5.h"
5 ~6 b# B& O" Y' ~' t
7 n/ ^- u4 n5 x. J0 L1 R/* Constants for MD5Transform routine.
*/
G; d5 S9 }& M9 w0 h2 B- {
m I. E7 N A8 {6 Y
# D# l0 F+ Q- v6 I6 E7 l9 j" @#define S11 7
4 b* E9 s0 M1 `* C; _" f: m9 R #define S12 12
: a$ E5 c( C* G. Y6 C" F #define S13 17
1 I8 H, H3 i+ e* f& _: K#define S14 22
; B1 I, J4 O" H" y$ d5 N" ^% S#define S21 5
) a- g: ?% s' i #define S22 9
! s7 a3 E$ p: `2 S a8 f x0 G #define S23 14
% f6 V0 @; l2 M3 d4 e- Y7 _. \ #define S24 20
$ ?" ]) ~; ^2 ^ #define S31 4
4 B! l, @5 C/ f# Q9 s& Z1 b #define S32 11
9 [0 F, q# N& n) b#define S33 16
) e8 s a# D/ r7 O" h, x #define S34 23
& k) l, t3 u* F( {( s" L #define S41 6
5 ?* y2 V J* H1 z9 X5 K #define S42 10
, A7 Z+ V/ q4 J: W( S/ R#define S43 15
0 Q, @) \3 N W8 U! t# q$ H2 |#define S44 21
, _. |$ w1 x, O3 u
L6 P$ q* n1 G- H" Y& a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, s; Q/ x" d9 ~. M: Y: _* W# w# A static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; n, o7 w, B! f: i6 F9 L- ]. G static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 Z. Y# g- ~2 q: [' [( Xstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; d& a9 V# T; b" j k& f5 Y static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! L, |$ ?0 \! E' t* j9 i9 y q
( ~! P- W- \$ z/ {' h 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
+ }+ c/ Z$ r* J4 i( O" f};
8 W4 [$ A. f! Y4 V
: m6 Z9 K9 [! }' c6 M5 _6 o /* F, G, H and I are basic MD5 functions.
*/
. S7 b0 d0 E8 M8 i( V#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! b, l/ e7 U9 X4 T #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; b# F. R0 f$ }2 y7 F#define H(x, y, z) ((x) ^ (y) ^ (z))
7 p: t$ t9 g3 y0 ~) k" ]( @2 h #define I(x, y, z) ((y) ^ ((x) | (~z)))
* g2 a& a: \1 M9 g0 C4 W$ f3 w$ s
: b; r+ Z1 V9 @/* ROTATE_LEFT rotates x left n bits.
*/
+ V% P8 M5 N1 X9 M) F' s9 h#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' ]- e7 p# t# K _6 c
5 c8 L, X5 W! Z1 D: D* X/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 H3 G: O, Y9 t7 E$ |) MRotation is separate from addition to prevent recomputation.
*/
1 T9 h! |+ r! m1 B- S* { #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- }: w: O/ s; G( E/ A* r% 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); \
}
8 M$ E" `2 H- o) Q7 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); \
}
9 ^" {& ]' J; F9 W& J/ h& 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); \
}
S. g, w5 I9 z% D0 d
2 R+ I- o& _" j5 P6 w9 M( t& h0 G& S2 ] /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
/ C( {) D) Y" z/ e7 `3 c% Wvoid MD5Init (context)
6 E& n( S6 b, s, |3 ^) a+ } MD5_CTX *context; /* context */
$ E k' r$ F: |& D: i& { {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, k- M! k; Y! O8 L */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# {' B& M3 g+ L' |/ J. `}
' Y2 i# o* ^+ j7 u9 ]" Y
( Q& B& ^: m. X, O- t j- V/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& C6 U1 H" J$ X8 c0 b5 fvoid MD5Update (context, input, inputLen)
9 s/ b) W( \3 r1 E MD5_CTX *context; /* context */
( c: M: U8 u$ F( D4 Iunsigned char *input; /* input block */
" q, F; s3 q; r5 l/ E% r2 zunsigned int inputLen; /* length of input block */
* D2 v' \% ~: }/ D m8 J{
unsigned int i, index, partLen;
9 d) [. s! W1 r% T! ]+ d
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# @1 k2 v" n& x: d9 P# D/ H
/* 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++程序


K- S, n6 l6 J2 E" j$ h
9 _2 w# |+ I7 z/ F
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-20 15:51 , Processed in 0.195313 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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