中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) I; h8 i; h+ ]% l9 o1 I- c6 c8 l #include "md5.h"
, p7 ?7 j5 _& n
7 O: \" `9 g/ W/* Constants for MD5Transform routine.
*/
/ _5 A* e& l- l5 U, [+ u
+ @2 j9 I, _5 x! v, t( N- f- c4 f% j0 [
, s' _9 ?1 a2 R4 Q #define S11 7
: J# B) L" F9 G: X #define S12 12
3 B1 I( l; A0 o. Q #define S13 17
) \6 i1 V1 u- u A% ?! @, E #define S14 22
# ~- ]2 u5 D2 P$ S#define S21 5
$ h0 ^4 v- a9 ?; F* m8 V #define S22 9
$ H" P( u% ^1 p5 ^3 {- X #define S23 14
0 @! S2 P" ^6 A; \ #define S24 20
- O) c% S2 v4 X$ p. q8 j #define S31 4
2 v5 g0 ^- o) J |* ~' H* v8 M #define S32 11
- u) ?0 ~ v: [2 N0 G" r#define S33 16
3 a' _% ^5 w4 i \! ] #define S34 23
/ s* I- j/ Q9 ^) V8 R #define S41 6
8 J1 O4 H" ]. l; D8 n#define S42 10
# ^9 F1 `, R' {% E$ } #define S43 15
5 d \" t4 E5 T! _ #define S44 21
, P/ W G' @. `
/ w! l4 p4 Y5 Y- e6 D: V static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 ~6 E+ d# g* d' Hstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
1 K" ^0 E. w7 A4 ^! `3 h static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
C; q) F7 _0 M& O' P static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 r4 F7 k2 p5 S2 U9 D$ @1 z |# sstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( W |4 w) n5 y! |
8 L+ k# s- k b6 A% }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
9 t' a0 k6 J8 Q9 q2 j6 O };
, ~+ c' S7 y" [) N, |
, l$ K9 J4 L% W: F4 S /* F, G, H and I are basic MD5 functions.
*/
& W& L2 W; B+ }9 U( V9 U6 b #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" V; {* z A0 g( c #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! Z! ?8 M+ ?) h$ n+ A* X#define H(x, y, z) ((x) ^ (y) ^ (z))
4 q: v+ y$ m% u. U. h! r4 {7 M #define I(x, y, z) ((y) ^ ((x) | (~z)))
8 @- Y0 [- V1 [- q2 {, I
, Z( L" e1 b" t6 m4 U! o N /* ROTATE_LEFT rotates x left n bits.
*/
' r6 }5 i/ K! K. I! S" Y2 n5 [ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% Y. \. g! n+ u" B% C
- ~& h$ c1 R7 c" U/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ w; Y3 `8 N0 M$ ]/ ^2 |3 | Rotation is separate from addition to prevent recomputation.
*/
* i, L; Z2 w$ L) A0 L #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 I! q: s% E6 P& W u: h #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 T' P+ ~& ^4 f( J5 d1 G #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* x% C3 u; |( D8 K6 q/ F( 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); \
}
0 _5 s6 \: R1 P- N3 c2 J/ p
; R* n- R+ I8 u" u4 T6 L' y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 ~* y0 I9 s1 Z! `$ e6 A8 { void MD5Init (context)
' y$ B- C1 T' Y$ C5 S9 ]; x MD5_CTX *context; /* context */
& ~, c/ I2 f0 ^3 G{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ ~& ^8 u6 N6 D, p" h1 U' w */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# i! z, E& O5 Z" l. [}
0 ^* h( L3 K3 ~" \ n
5 }6 n" o5 t5 ^2 C. S% d- U* U /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
X X. e; f/ C* ]! x1 J8 }void MD5Update (context, input, inputLen)
. K0 b' h' d( W: D MD5_CTX *context; /* context */
+ M0 `/ r$ K2 S% I) E( ^unsigned char *input; /* input block */
8 R9 t: M+ H7 q1 X0 I) x' z unsigned int inputLen; /* length of input block */
% B5 E1 l; n" e% N: t{
unsigned int i, index, partLen;
9 J8 ?' P+ `& o' g. U
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( H+ D: U) \$ I& [7 o* y
/* 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++程序


; B' o& H6 T% }/ V$ O
# E+ I# n7 Y# i, \! Q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-13 14:51 , Processed in 0.056557 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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