中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 }1 \: y% v- Y7 q9 ]" x. o#include "md5.h"
k9 B K X) O2 F
. H2 a5 c9 l: `0 R6 B' u/* Constants for MD5Transform routine.
*/
3 \. W7 v- ^ j
/ M# \, I; H/ k- S e; j7 k) C2 S
3 w5 ~4 Z6 T! R' o#define S11 7
6 O1 R9 W% D- H1 T1 F5 i #define S12 12
% w" Q4 [/ S( {#define S13 17
5 c5 D5 A! o' \6 Y% i+ j2 H#define S14 22
: k; @6 J) O, J! d9 M! N2 D% Y9 J O#define S21 5
1 ~9 B8 x7 v9 a0 r' g4 z #define S22 9
4 o9 H+ ` c/ f#define S23 14
8 o2 n3 O; u4 o- o #define S24 20
) s. e% A- Z, A#define S31 4
; R$ w" v( K8 _, K8 [# B #define S32 11
+ ^6 \% f# t: j3 k#define S33 16
1 Z. g8 A- N& ]2 \! o$ W7 B #define S34 23
* n, W* s2 W* l" E1 P3 j, t, p% G #define S41 6
( v9 ^, s4 \- A4 y% ^: n% d#define S42 10
+ h* ?& j5 `5 Y& R# S& X #define S43 15
: `3 A* R, ?+ @#define S44 21
7 G' T4 v0 |* v l
" T4 ]! ?% G2 u- e# e% i5 r; }, W4 r5 c static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: v9 g) L) L$ R7 ]- ~$ @ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 o6 G6 }1 p$ y+ C3 U" x& gstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! D' }% y6 j# Kstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, l# L* w3 ]0 d, K! Q1 Y' v static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! M6 `( @4 O% B# @9 b1 S* L' Q m
2 A# r- o8 B( q! t1 l vstatic 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
' p# o2 D9 f2 a' ^- ^ };
% l% w5 X1 a: q3 Q" v4 K+ A: R
. n3 z. d# E+ `: D/ R/* F, G, H and I are basic MD5 functions.
*/
+ v5 N+ ~6 h* |9 h9 ]#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! y1 T) G- {# b* {2 d& N9 f7 O#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; v# D$ {6 _+ S6 ~3 B #define H(x, y, z) ((x) ^ (y) ^ (z))
) |. V5 e1 t) V t7 H# _0 K#define I(x, y, z) ((y) ^ ((x) | (~z)))
/ T; E5 k- p! g+ A7 }9 \
* t( ]4 ?6 B5 ~, Y0 R8 w6 z7 f8 I/* ROTATE_LEFT rotates x left n bits.
*/
2 @2 q+ r1 W& d1 T3 [; [ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
p: R3 a* Y0 ~! b! E3 c6 I7 T# m
% r% a$ N: `& V0 ^0 U8 C* j9 @/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
2 s P) @0 s( o: I Rotation is separate from addition to prevent recomputation.
*/
- w3 U( t. N0 J) l6 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); \
}
& K& F; Y% Y, N0 ^4 L$ R; ]0 x4 l #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 Q/ Y8 C' h4 O$ c; a& x$ g+ k#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 n/ h; L. W( o- O) {: C1 N- 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); \
}
3 o" t1 n& v0 m' C
4 j: K5 r$ n; h5 ?" O' W /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 h$ Y% y; r" l7 D6 G- j: _6 mvoid MD5Init (context)
3 H+ j& ^: k6 N+ E MD5_CTX *context; /* context */
2 s$ w& d1 \2 u. ^' Y4 {1 g{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 N0 h6 k) }, W% J */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ h$ s. ~. z! Q! W ~6 ?8 l }
8 h; i- I+ K J
, ^8 F4 E ], }- u) c$ P& z2 B /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 R6 {4 P5 r0 [ void MD5Update (context, input, inputLen)
0 r8 u. y3 \( u+ SMD5_CTX *context; /* context */
: J" ]. N3 U# N. g7 V unsigned char *input; /* input block */
e; W5 a2 S6 m9 x4 u; c9 N unsigned int inputLen; /* length of input block */
" D, N4 D" j. _: h7 j{
unsigned int i, index, partLen;
U4 O4 x* f1 S3 t
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, {! R& ?# `+ s( F
/* 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$ Q3 ?: Q3 n$ b% \
+ f8 H7 f) ~4 @2 L' Y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-2 08:11 , Processed in 0.256611 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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