中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! d8 D! \ H9 H/ }; V+ n! b0 ? #include "md5.h"
2 O; `5 |: B- }* ?
& R* [$ ^3 i: q. h8 c% ~ /* Constants for MD5Transform routine.
*/
s2 b! b" H+ b N6 ^
2 O. m8 o* z' D4 z0 y+ `
& @- X# I2 a- _) K#define S11 7
% r2 g- }! W/ [% ~: I #define S12 12
; C8 r) D! u4 u; A4 x5 Q3 g' p) I #define S13 17
# a2 ?% @" E+ F9 G0 z: @: W #define S14 22
O! R w! D% p* V5 n #define S21 5
6 M+ y) z( ]0 v6 H; f/ r9 Y #define S22 9
1 Q P& o( D9 J" x- | #define S23 14
+ ]# e$ {, J6 l5 z! }3 l #define S24 20
0 [3 V9 h3 K# ~1 Q/ p Q' u #define S31 4
5 g! c6 \4 u; o5 i) s+ }#define S32 11
; `& {% T( B) p3 z#define S33 16
- t5 u) v( k: C3 M#define S34 23
3 J1 W8 x7 t/ Z7 f#define S41 6
1 D# j n1 T% j. Q7 C* a #define S42 10
( ?5 b2 C8 r# \' q! n6 a #define S43 15
/ J; E# P0 e% Z$ P2 | #define S44 21
4 G: P1 l, e7 Y" Z
$ {% T ?" Y) J7 k+ Z2 {7 r static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 a6 p6 q9 l1 T* x/ I, ~! ? static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
9 p) B% o% U7 @static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. B( K* d& D/ }2 D7 b/ t static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% f' E7 l& g8 O% s- zstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( `% n% V; E( Q" H7 m, m! Y7 s6 b, o
$ x3 @0 P5 ~' R; b- estatic 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
% j4 @+ F( A3 D9 ^$ {};
1 e8 t; Y3 ?& ~: V! @, B) V" M- u
* P/ V8 [. ]1 ` O$ @. E/* F, G, H and I are basic MD5 functions.
*/
4 i4 A- i1 ^1 G7 W #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! I2 s! Z v8 l0 t# F: _ m #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 \; ]( ^1 A6 O# h6 g- {7 I #define H(x, y, z) ((x) ^ (y) ^ (z))
7 |7 a+ ]2 W! b, A; ?/ w4 ~#define I(x, y, z) ((y) ^ ((x) | (~z)))
( I. f9 T" e5 ]$ E% z: r i% l" |
+ Q P+ b7 x: B$ f6 b1 V /* ROTATE_LEFT rotates x left n bits.
*/
7 V8 e; g- _/ {& H# t#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
1 H/ A6 w0 C; J+ @ ` Y# }
- Z. I$ v. `! B8 e& F. r5 p/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) V! B' Z# t3 G( j Rotation is separate from addition to prevent recomputation.
*/
2 D2 _# D C3 P/ U( P- ?5 m #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ I) m6 c2 z4 O6 `! J: 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); \
}
; K W+ s) {* ]" R#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& C* J: d' G! K( a4 k3 j( {#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 ^0 u6 M" h% {; E9 e- S; Q- ?
6 m7 S! K& M& i9 D* J/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 `3 {3 ?3 @9 W: y- ^8 R6 F& n void MD5Init (context)
4 k2 Z+ m- _) H MD5_CTX *context; /* context */
, D& p+ E! E- L{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 [' L! J$ K% N3 p& }8 i* P3 g s* |$ B2 P*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 w4 L0 a+ k% I}
! D# X/ D2 j3 q
& [; l' q/ P2 P& {! K) B Z. c1 C /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 k$ R5 d# q8 t1 M' V' x4 Nvoid MD5Update (context, input, inputLen)
' U! V" n2 Q+ o8 ~: Z MD5_CTX *context; /* context */
# s. G# n- ]5 T6 @, g unsigned char *input; /* input block */
8 u! Y J* R( v4 T unsigned int inputLen; /* length of input block */
* O) V: K! C2 H p, ^8 H0 c {
unsigned int i, index, partLen;
8 i3 U% c) J6 i+ \! R! y" m7 l4 X* S
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& D/ ^ _9 X2 Q+ o3 r( E
/* 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++程序


* C3 ] E8 Z$ f, m
& W4 t6 w3 U: S1 a b* ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-2 17:26 , Processed in 0.076471 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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