中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ t! E' \! F! h) { #include "md5.h"
8 l1 P! O( i6 u0 Q5 z* H
# X9 ~8 s% U5 ~; e6 ?/* Constants for MD5Transform routine.
*/
' ?7 o3 d) Y$ U0 i% W% r' {" k: m, r0 D, I
# c T# N* r8 v' r* v- k' H
) G1 a' u0 }" n* ~7 E0 d% |#define S11 7
9 t% q" D9 F- E+ \ l #define S12 12
! y# M2 W! D* Z" `; X+ W; d9 {: Q; z; _ #define S13 17
( |* Z c! p/ ~. [ #define S14 22
$ o* C( G0 i) b4 l6 c+ J( \+ y#define S21 5
- _! D. c& M! J+ ^ #define S22 9
: j, w& h( c+ F7 m #define S23 14
3 t, g0 r1 O9 H# ~5 b5 h8 D/ p+ e#define S24 20
) W4 S* p1 G- e% C0 D2 v: r #define S31 4
9 [6 W/ {$ l5 o8 p) D( p#define S32 11
/ X: ^' k( M0 {7 n! H4 |#define S33 16
# T2 @, R; F2 K0 y s2 D; f1 m#define S34 23
8 |! h# W7 {' @$ s$ z- @#define S41 6
) H$ v! s: s8 }0 c #define S42 10
1 Y: w U2 {( \. V, o! Y #define S43 15
4 }( m# d+ J' N" C' v/ e& P/ T5 w#define S44 21
9 E, m7 \+ U1 \- Y `
4 a7 F3 F6 A% _3 \; I static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 b% h# R- K* {" E3 n# wstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 G. Q Q! F" Bstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& y- K# ?( ~0 i( z2 j- A static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+ o4 C' s- P) m" ^2 Y, [* t static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# D" W7 s3 W$ [
3 B M5 y0 n: Z' T( D9 V/ M7 M& U+ h2 F2 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
1 @: Y6 Z* A- I2 O4 r: J};
* P' M, U8 ]- B0 `6 X9 w
7 {* t- C; \; [4 u2 U; }: p0 b* ?/* F, G, H and I are basic MD5 functions.
*/
; i2 g, b6 X: [% l s #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, V" x; D( ^4 D* D #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
* d, R9 F. B( W1 f. P* Z1 i2 X #define H(x, y, z) ((x) ^ (y) ^ (z))
% _) ^% W3 c8 i& ?5 a7 a. Z0 g #define I(x, y, z) ((y) ^ ((x) | (~z)))
4 ]. B: o& j r3 w8 I
" s, N/ H, G+ b# I /* ROTATE_LEFT rotates x left n bits.
*/
6 Y c. ~9 T* O4 u# p: x! t #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& b; q7 F u; G/ o
7 ?2 o& N3 u" R/ k5 f V# N /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ }. [- Y; b2 X6 l( G2 p. o- _Rotation is separate from addition to prevent recomputation.
*/
( a( n) x: j( |2 D7 O( } n5 o3 n) X#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) }: _" r ~& X- {#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ l& Y8 ~1 o3 x; h! |( K4 U7 ^2 ? #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% M6 Q- f* T6 o, y$ c0 C/ \ w. i #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ |3 h; ?4 Y$ ~
) x3 I: \& C8 v. K/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- G: r7 @6 e$ K void MD5Init (context)
1 f1 E* Z- h5 F+ Q6 R# ?. H, u MD5_CTX *context; /* context */
5 X7 y' J; F4 N; A1 k) n{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ p" }5 Y0 O- B( z*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) v6 o% R* [7 y. E0 e6 }4 N* J* m }
- e. Y; ]: M9 S- j
" o7 ]3 I$ n0 V, D& n/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; s# T& N U5 C( nvoid MD5Update (context, input, inputLen)
2 W: Q( m4 I8 d0 XMD5_CTX *context; /* context */
+ U8 b2 l( g# f* @% M2 ?3 X( y+ y unsigned char *input; /* input block */
) x) Q, ^! k8 R; y+ i) X unsigned int inputLen; /* length of input block */
, p; ?9 p. I/ } x: P/ i( U{
unsigned int i, index, partLen;
5 \: f/ C8 X. x8 w/ [9 Y) j" u
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% o) }. G! h7 R7 W7 e- P
/* 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 f \- Z! ]# O# W
) W, G& K: X0 Q* ^) r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-8 06:55 , Processed in 0.057990 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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