中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 S% R9 Q% n% F1 l$ m! ?, I& o#include "md5.h"
" A+ k" ^6 D% \ X! ^& F7 Z
, z- ^! S. Z0 R2 `: X- l/* Constants for MD5Transform routine.
*/
/ R! E! W/ u$ ?! Q2 W# ]3 l
' P0 w" v8 Y, V) J. {
0 b# X$ v+ h$ Q" p6 u [ #define S11 7
5 ?8 G, u3 C6 v) B7 _; r! L* g#define S12 12
) g p# G% U" o0 [6 g3 R #define S13 17
, q/ r' q2 \) z1 G) F; A- X& \2 ? #define S14 22
1 a/ ?( w8 w! a V#define S21 5
3 w7 y. y( | g* k3 T) a( l6 i#define S22 9
7 T# D* h5 ^# |. X x d2 z% f$ h3 N #define S23 14
) T/ v" X* s/ x7 x8 y4 n2 A, ^ #define S24 20
) Z% [& |( r+ K: ^) P7 o #define S31 4
& M6 K) R2 p8 J, S6 g( N3 I) g #define S32 11
' E! p( @, m; z0 M& n( m* I) c#define S33 16
6 C' U9 i9 { O8 i2 |+ U o #define S34 23
6 {5 P; y6 X1 l/ {3 r#define S41 6
4 ?# C5 |( f3 p7 b#define S42 10
" c- @3 @8 }* x8 m5 f: _( {#define S43 15
) g! F) r5 [. x8 { #define S44 21
3 l! ]! e8 ?. N, e. E: l# u
7 b; L, w# t+ P6 P# T. o static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: f& j5 d! ^, P7 q4 V) f. ~! Fstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( L3 u5 y; v9 A+ z& V" u8 L static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 s, Q4 n) ^$ S, S0 R6 lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. @- i, }6 X1 \" k. Q6 M" K& lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 O) W, N9 m7 K) f; ~
+ E7 P4 Z" v/ t" M6 U5 cstatic 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
- F( S) G! Y% y. O1 }7 T Y% T; C* q3 H};
% Y" X v) Q. R9 ?
: u4 a9 z+ X; \ /* F, G, H and I are basic MD5 functions.
*/
! _* b3 a9 y) K0 p& E/ v9 V- O #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 z4 q4 {* D% j#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# J' _5 G0 t. t& A* n, E #define H(x, y, z) ((x) ^ (y) ^ (z))
* M, e+ P% z M- h: x1 g7 a& i9 v#define I(x, y, z) ((y) ^ ((x) | (~z)))
: [' y9 D8 h! {# b
9 K$ o. t% H( g9 ^ /* ROTATE_LEFT rotates x left n bits.
*/
0 ]8 ^( E" u- w# q; ?5 ^: [#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. k9 R4 G6 ~# b6 R5 ~! M
3 m3 X0 R, E0 A) |1 b$ W& Z# c6 f/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 o# o' _# u' J+ v2 N CRotation is separate from addition to prevent recomputation.
*/
* Y' R7 Z! N2 j4 C3 _ #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# h9 {( l% B. d' e#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- E* s; s% J5 D( c: ? #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 y. U2 v. q/ Z* U: y) 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); \
}
' E# L6 l6 Q9 A- ]( Y: C
. ~9 }( j. N) h7 _/ ^* ^- K* b) l/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& P6 l, [& l5 }& Ivoid MD5Init (context)
1 I: B, O9 `* f ^; q MD5_CTX *context; /* context */
! r! V0 ?" ^+ w! ^: e; ^2 V v {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, e+ W1 C9 [5 w; f; ^7 ^; x5 F*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 u( G. T* Y! ]0 l) m8 w& X. B9 _}
" Z" R3 R p9 J) O/ m
+ p/ B: l1 t' n: U$ x: A/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" N( Q w3 T2 Pvoid MD5Update (context, input, inputLen)
7 I$ a) s- E; a$ _2 S! S MD5_CTX *context; /* context */
- n6 T& |& d% G1 D unsigned char *input; /* input block */
/ G7 \4 O( {0 s8 r1 n$ e$ c) v( d+ u unsigned int inputLen; /* length of input block */
; Z U5 A) u' B5 w6 s {
unsigned int i, index, partLen;
/ _1 H4 @$ f, y3 F) U
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: n# w; q% S' Y; B% P2 r
/* 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++程序


4 I& Q9 ]6 d6 ?& V8 O; h1 x g
) f% t0 g# b# M7 q$ V
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-12 22:10 , Processed in 0.073272 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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