中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. x; Z( M; p" Z( L#include "md5.h"
5 _6 p& ~' `' N8 j+ @
/ a/ u P! @1 p- i e9 D/* Constants for MD5Transform routine.
*/
j. L; k! D$ G2 t0 s
( o# q2 `; J8 @% q/ R0 S: [ T. b6 w }
9 w# |7 v8 d8 H c. ~: O #define S11 7
4 g! V0 d$ r8 L. F #define S12 12
# e! X3 ]5 Y5 N3 r0 M8 V#define S13 17
) n1 g* y! x% M1 c. H#define S14 22
, O1 x5 B6 r5 S( K3 i+ t; X" ^3 S#define S21 5
! v# d& y, W+ W#define S22 9
+ G; E4 I2 G# d/ p#define S23 14
8 j4 ^0 T2 L$ D9 Z3 ~, a- f2 I#define S24 20
6 D1 D; O* B5 @4 ^, @; q) p: R#define S31 4
3 V) H1 a& M# S' q) F* S8 W6 l #define S32 11
1 F* c" l* T; k* g7 S #define S33 16
' B$ H+ X( z0 F8 }0 U6 q: t+ ^ #define S34 23
4 R7 \4 f% B+ L n' Q- R5 {% q #define S41 6
# K n W O/ O$ p) x) {& f y#define S42 10
0 Q: E. Q0 O2 h5 W) y#define S43 15
/ q& i: o- S" y' B" y#define S44 21
7 t- o$ p$ I1 L
6 H7 D* Z' F8 C. l4 @* N% I7 Mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 y+ g# }# f2 C9 G static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( A1 O8 [& {* o$ V static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 E) G4 ?/ D2 I# I/ r" V! F static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, J- A6 o# d( w2 T9 H static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, r1 Y+ }* f& L
' r1 H2 h# V. X# F1 { 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
/ s/ W3 t$ B" U};
7 w$ {4 H y8 t7 m, N: W. I
/ w5 _; i, ]/ { d$ q7 N/* F, G, H and I are basic MD5 functions.
*/
% |* R( f0 U6 Q! g% t/ L4 t: d#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 v9 Q( L/ T6 @. h/ l #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
. n! Y: s) y6 H8 e#define H(x, y, z) ((x) ^ (y) ^ (z))
/ J) d- l5 {: r0 j! | #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ |* N) g. z/ }: R! Z! D f
7 ^2 P- ~& Z% P2 [ /* ROTATE_LEFT rotates x left n bits.
*/
3 W' Z( ]; }- i, Y* q4 }/ F#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) t$ i2 Z, g# }$ o6 Q9 i
# V3 I+ U8 q+ u/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; x( s$ `6 y( L Rotation is separate from addition to prevent recomputation.
*/
, Q3 ]' {& L/ @: q" Y7 e* y& v #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 [7 F" l: D$ V* P4 A1 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); \
}
, o" d2 l$ a& c7 m* n#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! ?7 c8 Y' N7 d! G/ 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); \
}
* t% R( a& F0 p
+ S0 O( c; l$ ~- t+ q0 N! |/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, M' ~ @ u) E/ {# B( ?" W- Wvoid MD5Init (context)
% ]; s5 K6 n( ]8 ~# QMD5_CTX *context; /* context */
' H7 |# [0 {* f6 Z {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 T" p0 M1 S9 {/ {! J5 l$ l' b*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( n# s7 V# Q7 v7 A}
U! W! y; R8 y$ ^
4 T# n0 N$ Y8 `5 ^1 p B1 W [8 G0 ^ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# f( w8 e% M# Z9 ] void MD5Update (context, input, inputLen)
- w7 P3 `5 t u' }5 [MD5_CTX *context; /* context */
7 @ W' q9 H3 Q: U: J0 ] unsigned char *input; /* input block */
5 s6 i2 Q. c2 ]8 X unsigned int inputLen; /* length of input block */
6 ~1 K5 r" `1 r( E6 ^{
unsigned int i, index, partLen;
; U7 O/ x; Q/ v4 R% p+ ]
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- o i S5 s @( p0 u
/* 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++程序


) n4 X+ m3 l" c7 D& `, X/ d4 P7 _2 p. p7 X/ c
, P# D# W0 y# Q, f$ r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-18 02:27 , Processed in 0.088288 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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