中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
2 E/ q! u/ W8 v( X. [* y#include "md5.h"
2 R9 d( @2 D4 {+ j: u
V, u, [' E5 r' Q! L u; c) Q /* Constants for MD5Transform routine.
*/
3 i: b% p6 W1 Y
( P( i& ]5 C8 i
) z- m$ u3 A- t2 ~- e: E7 N#define S11 7
$ T0 _! M8 N& K3 o#define S12 12
* i% c+ p1 @7 o, j! u, ^#define S13 17
* X8 A% h/ V9 m #define S14 22
" S/ j8 n$ Z, N6 X8 I0 F6 C$ a #define S21 5
* ]* z+ K- T* ~# _' h! L _ #define S22 9
5 ?. x6 A+ s& @/ X; [# a#define S23 14
, E% P9 T; p0 m% _2 { #define S24 20
. L6 f0 F. O" y' ~# g! g#define S31 4
# x' T& Q4 }" A/ Y" s#define S32 11
& Y) J2 Y4 |% \! e #define S33 16
) T- n4 G1 ?# r" V; c- j#define S34 23
2 W; ^$ R5 N1 u: Y! L #define S41 6
. J6 f x; x `- U& a#define S42 10
0 r5 [; W4 ]- L5 A( ~* U #define S43 15
/ |+ f# P' v4 m! d+ R$ V#define S44 21
% ^, {, K' \ b" {. O5 S
% g" L7 F, o' o8 ^+ ystatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 [9 x' ^, O9 z+ k) pstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( v( C4 M' I8 l: tstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- F" o( d0 [0 D7 D) ^9 R* d" `, tstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 i2 ^: r- P$ F, U) Z& ~static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' s0 v/ }, O; K" f' z1 {7 F4 q
8 r+ l5 ~' b+ s! v 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
) q a; Y1 L$ B* H0 M};
$ x! @+ C0 M; c# C5 L+ D
1 y" W5 N% f: d! V9 o, Y /* F, G, H and I are basic MD5 functions.
*/
4 w& M G: p5 g t: c* S #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, t: I# ]* V) F( W; v#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% M) d9 q9 U! |* w2 A& X #define H(x, y, z) ((x) ^ (y) ^ (z))
' u6 F1 k# F( `0 C, P#define I(x, y, z) ((y) ^ ((x) | (~z)))
; s" b( O# w* B2 `. L5 C% c- Z9 `
# l$ C k: b" j3 j( p/* ROTATE_LEFT rotates x left n bits.
*/
( ?5 x1 Y. s I8 W& r# \; r9 ? #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# s1 p, A" e+ m2 v, E. _# R2 a1 L; }
$ S: W- T) |$ P8 V/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! ^: {/ s) O& }* P8 l" NRotation is separate from addition to prevent recomputation.
*/
* ]5 F! m7 e2 U- {#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) T% ~9 u Q. W2 |7 o#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; _( ~3 V& m! Q1 s #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) i0 p* t/ J1 a e8 l#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. R; _9 S" f, d- D2 }4 o
) f1 y4 X4 `, Y8 d* O. J" s: ~# d /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( `* ~% o1 Z& e! i+ R+ a2 p8 w void MD5Init (context)
9 o7 [. T$ s% ZMD5_CTX *context; /* context */
3 O) k& N; ~, `4 o5 B' o9 p/ V {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) u# Y) M0 ?2 D' F*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ _# M7 `9 ~, {. t: {}
: K; p# | o. W; c6 @# A
- X" W5 W! l, l5 `* _: @/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# d6 I9 P. x: _2 ?) svoid MD5Update (context, input, inputLen)
/ D; C/ c( `' c+ NMD5_CTX *context; /* context */
6 M7 Y2 g0 m4 F& I3 {5 v- x/ sunsigned char *input; /* input block */
, B8 e. Y3 P+ F unsigned int inputLen; /* length of input block */
# f$ ]/ r5 ~0 _+ l2 a{
unsigned int i, index, partLen;
, M% r5 C+ H; V M, k
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 g/ _; n% X T0 o
/* 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++程序


' X) q! e, R" W! H: ~- U
' U/ H5 V! i6 r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-18 13:33 , Processed in 0.077920 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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