中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! e/ F& O1 c y+ x) i5 E #include "md5.h"
4 V! \, t4 P+ V) F2 t! d+ t
: o1 m s0 J8 X. ]4 a /* Constants for MD5Transform routine.
*/
8 H! X2 ~& X) q
" D% G, o5 C& Z+ @
# C W0 F" ?. z #define S11 7
7 f# y5 [1 a3 L* w* ~ #define S12 12
8 \6 ]( f) r4 l1 U+ s#define S13 17
- s+ x; y+ q7 E+ {3 s( G/ r( z#define S14 22
2 X. I0 ]1 T# Z3 r" r #define S21 5
8 F' U7 u' Z6 I) Q#define S22 9
" { g# r3 w \' ^#define S23 14
) Y/ N7 I$ X5 ~& L& D/ B0 Y#define S24 20
: \$ l3 \ D$ G* }& j& r#define S31 4
+ c' ^% y4 ]' r; B& S- p7 C! U" I#define S32 11
0 B5 S$ t. z5 I! H3 X6 Z6 } #define S33 16
' S& u+ o+ ^3 ~% s/ [* S #define S34 23
9 U- B( W9 O; v/ c#define S41 6
5 p# S6 B1 I) J- {, N #define S42 10
1 w: K( v( e, W# b' ` #define S43 15
4 z, M; B& @+ ]3 l$ @0 P: U$ A$ w #define S44 21
( S9 M' [, e, _7 L- U) K
) O7 h( w3 a; z% g# m. n* s* Q, i static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ L- _! c& l* ~; wstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 r, p& [6 Z {2 j$ M5 m2 Ustatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- k, ~* w( X$ Q" [7 J; H- S- _ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' e$ y5 n; L+ F8 G7 cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
7 J. `# \' M2 n. V0 _
1 L v" w% o4 z- {. |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
) G# C2 m2 ]% b& J2 b };
( E1 E8 F: z2 f r: S9 J/ Y6 m4 L# I
P7 O3 q, Z- ]. ?" N7 R( V /* F, G, H and I are basic MD5 functions.
*/
! d1 B; C6 |1 D/ h J+ ~#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 }$ U2 i3 c% d& d4 H, g4 b#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 u9 V9 h; w& a; p" ~/ I* p#define H(x, y, z) ((x) ^ (y) ^ (z))
5 }' b! ?1 t/ ~( n4 L/ G8 _# Z7 A #define I(x, y, z) ((y) ^ ((x) | (~z)))
. X; `- V: T& b; [! A0 A
$ ]0 |+ I' o1 i; ?2 u3 ^ /* ROTATE_LEFT rotates x left n bits.
*/
. V1 L, W6 T& [ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! O9 t+ X- P8 t: y
% H+ ^+ E, i$ O6 A2 [ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
\" d" A. G- ]* P6 V6 R: w Rotation is separate from addition to prevent recomputation.
*/
8 i/ S/ i4 L% W* L7 N/ H #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* X9 e5 a5 t, {6 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); \
}
" F0 w9 q+ D- x#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% J: ~2 o% E- O e #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" Q% y. b, y) [) Y8 C& L
5 {: {- E0 d2 a5 g9 o# @& D9 Z$ _/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 I# r V! S: L3 i# lvoid MD5Init (context)
% S+ u4 |" i* f$ d" \ MD5_CTX *context; /* context */
* ]- {8 t, B7 Z' d7 b. R3 y {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) o2 `( a, o5 I6 R7 R2 M5 } */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 t/ H) @4 h8 S& ? }
- U# h# ]; _! J; x
' y0 G( C( U5 a( g8 `* N /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) |, N" C5 C0 l/ @ ~; `, D4 q void MD5Update (context, input, inputLen)
6 w* T! Y/ H. ^% Z2 c MD5_CTX *context; /* context */
7 Z: j+ O# b+ Z1 yunsigned char *input; /* input block */
0 ~1 C) x( c" B4 n6 c n% Tunsigned int inputLen; /* length of input block */
; P1 ] R+ Z) o8 p! L; U* Z# k% b% a{
unsigned int i, index, partLen;
' M7 G( j0 |' n/ N* z6 M6 n
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( ~! S1 `; \. _ d% F5 T
/* 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++程序


8 u: k ~6 X0 a" [
; V5 c& `8 |" I- X3 A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-31 05:24 , Processed in 0.105777 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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