中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 X% @, t7 |: A5 g) z #include "md5.h"
" u7 V& v( _, k v2 q2 t2 o |3 R% q
5 ~9 c- W( m3 `/ K" [ c# H /* Constants for MD5Transform routine.
*/
! F4 l1 r, |' w5 N
) T7 b* J7 `6 V5 ~) u. ] e
& o' X. f; [: h' @$ R! I#define S11 7
1 g6 ~+ O+ o8 n, a- E5 S #define S12 12
+ |# e( P0 W# f: h3 f#define S13 17
, d+ T# d' ?0 b8 P- i7 G #define S14 22
' C1 g1 ]7 F( y3 G#define S21 5
. m7 c, J! e* q$ @2 @9 m3 e #define S22 9
: A+ J6 N9 K& N: u2 L, B! X" a, h5 b; }#define S23 14
- d5 Q1 k8 G( I; i( H8 G8 O) S0 P #define S24 20
! J6 A8 V2 O* ]/ e#define S31 4
; `9 v' E6 U a% ? #define S32 11
) [: c4 U% W b: w, ^. U4 y#define S33 16
9 b! K) }6 ~) R& D2 U% t. u6 P#define S34 23
* K0 o* u+ T9 w' G$ {9 x #define S41 6
+ O A% G# F* v% B. S: h #define S42 10
0 L+ l7 q, S2 X9 ?$ {4 \" i #define S43 15
" D5 f$ I! [9 o c$ X; _#define S44 21
8 x0 ~$ x4 J. X) t U
" G4 G, n8 [) L& z, U- t static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
& o Z1 O4 N# j, j& K9 N: Ustatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, r- e ?' p. ~- Y) c& c9 estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) f) {6 m9 w0 r( u) r static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 b! ^, S# J( j# A! |# cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 s% e, f( R; O/ H' Z6 K) t
$ {& {* C' H9 i" K' w 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
/ d5 \& E0 M+ F+ T/ h+ h};
/ c- N) t! r' J+ @ u- n. x: T; _, \7 ^& H
% ?+ m2 } T% s5 {; M/* F, G, H and I are basic MD5 functions.
*/
+ R( r& l+ L; h2 g#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* |( _8 a4 s/ p+ w #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
4 _) d- b9 D5 P; o- ]! b& P7 J#define H(x, y, z) ((x) ^ (y) ^ (z))
+ j6 S$ P% V' e* J #define I(x, y, z) ((y) ^ ((x) | (~z)))
7 q% T+ I7 M; s8 f2 D# m0 c
' F4 K' m+ {* U W$ c; v/* ROTATE_LEFT rotates x left n bits.
*/
, p/ u, i' h* x$ m% ^: ?#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
4 S! j. h) g! G
' v% ~/ W7 X1 ]5 h+ d5 C/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ C4 w# Z- M2 J5 N( M Rotation is separate from addition to prevent recomputation.
*/
. K8 I. L% l. [' i#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 E6 r, r" ]+ c( w9 C#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( f+ Q$ r$ D1 \0 d$ { #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. F0 \. m+ n: {1 Y) O#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& x [3 n" k9 X
$ j, X$ q2 [. `+ V3 n9 K* r /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; y$ S$ P$ ?# I! ~% n, _2 \4 O) V void MD5Init (context)
4 Z: v* _4 R6 q: T MD5_CTX *context; /* context */
/ K( ^0 q8 V4 T0 o3 u {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
p N8 p1 h. g5 c*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
' R8 B7 Y% y1 H' M% B6 v$ }" Q}
. K" T* \! R" ^5 |' \& J
1 Q9 m. o Q2 B. X6 Z) t /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 n8 X. ^, ]; V& Hvoid MD5Update (context, input, inputLen)
9 ~' r' ?1 \0 N: m* D; W) ? MD5_CTX *context; /* context */
2 F/ B; ` `# B: d; u1 [: Y% k unsigned char *input; /* input block */
% e( [* J" B1 z X+ c: r5 T! munsigned int inputLen; /* length of input block */
9 }$ {5 T. e, ~. @, @ b{
unsigned int i, index, partLen;
. F d6 l' ~' n9 n$ p0 A
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! V6 H9 }$ K1 _; W9 C
/* 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++程序


- p$ j9 x' U# V# Q1 F
0 M3 ?7 d6 M2 e! [
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-29 05:22 , Processed in 0.058822 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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