中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
/ `& u5 T4 e6 R/ f# W! e: w #include "md5.h"
, X( d1 j1 Y, v) d6 z
8 f% ?% N: T: S. X4 x# { /* Constants for MD5Transform routine.
*/
5 a' U8 M1 p8 ?4 |1 u5 f$ s8 j
# I5 c* H9 J) L! K) C
, X2 A: R9 W' U" Y2 |#define S11 7
5 P+ }' U: q- d. T+ E) T- z #define S12 12
/ k7 M) d+ }2 _2 v \#define S13 17
^' a; W" R. S& j#define S14 22
& z7 Z; V( F$ a) O' D' H) s9 @# S #define S21 5
. Q9 w, }" k5 z! E+ L! h #define S22 9
$ A. H* X8 `8 v- G% h#define S23 14
7 g/ V6 I! @ v! Q) b9 { #define S24 20
* N) F: S- a' P8 l* e#define S31 4
4 V% V" S/ M z. G& C& t% I#define S32 11
/ P$ q7 K7 z' J. }! [ #define S33 16
" E7 O* K& o" H5 b#define S34 23
% p$ R( V6 e* b$ P. c& q#define S41 6
+ i" ~; g, j9 n3 m. n#define S42 10
; _6 v' n4 p# {/ L/ B #define S43 15
- q- U0 {( `5 K5 v" w$ W+ { #define S44 21
' w+ v9 P* N3 P1 j8 C# V
. R, }9 X7 p/ q! j% b8 V! N static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 F0 x1 B) U% ^7 b: Bstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' f) h- W4 j$ e: l4 Wstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# O! }/ w6 b; T' [7 J static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* C( i" | @8 X8 Y0 h | static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+ C0 T0 T/ n4 m5 P' T9 i
$ C* r6 E' I' x: f" i 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. y: c7 l: S2 a};
; W) X4 q' s- U! z$ m
9 \! Q. L+ J9 ~ f& C: L0 T/* F, G, H and I are basic MD5 functions.
*/
/ s. C$ \' H) u# b7 g$ b2 w #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- f- h/ O3 J+ Q N6 G' q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! D- l" w3 A$ p; ?1 F' @" t #define H(x, y, z) ((x) ^ (y) ^ (z))
$ f! K; t* L2 ^ H; Z2 O3 U6 L* e#define I(x, y, z) ((y) ^ ((x) | (~z)))
3 s+ I8 w: N7 [# i$ Z
4 d4 d* I! \0 ^3 e /* ROTATE_LEFT rotates x left n bits.
*/
. E! M+ ]% E2 j* \' C7 O, m; o/ i #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% i( h# J# |7 e
) b5 X# s$ {- H" O. E I) {+ g/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
9 s7 I& {& {- S2 z* `! qRotation is separate from addition to prevent recomputation.
*/
, C: \1 q Z* h6 {% M E, X4 F#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' D$ F2 X8 g' Q #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 y% U2 S* O' ^1 e( 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); \
}
$ p6 s% v1 i5 E" O8 E, S2 Z#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 g: A/ P# G* W% B+ O
! T5 d5 I8 _+ }( H5 y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: Z7 D5 B& ?5 y7 Ovoid MD5Init (context)
' D+ ]% H/ I% m/ L MD5_CTX *context; /* context */
) p9 { J2 ?9 p" J( I% w{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 ~$ [- z" J" p$ l A */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( ^* h4 Y0 L' {- b4 L2 H8 Y }
3 _/ H/ N" n" C) y) I
: s$ p( R; J- F5 n/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( Z% W* E# O- w( G void MD5Update (context, input, inputLen)
3 @% v( x& y6 i: q0 h MD5_CTX *context; /* context */
; A& o; g$ f f2 g: W- A unsigned char *input; /* input block */
. E; S) O) l" lunsigned int inputLen; /* length of input block */
5 J: b4 D1 \) g' V. A( o2 P* `7 u$ R{
unsigned int i, index, partLen;
6 y3 j# T& Y2 {; Q# k6 l7 c2 G
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 t" `6 B. _) H6 S4 [0 M( `
/* 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++程序


/ K9 o' B3 N8 p& }6 P
' i e3 k, ]& D4 a; a
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-31 20:20 , Processed in 0.105948 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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