中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' B5 D9 ]+ g+ t/ I0 e( [#include "md5.h"
$ G9 G7 ^7 e5 W% q0 X
% i+ g" [ R% T* s9 `7 Z/* Constants for MD5Transform routine.
*/
% L/ w2 T% s6 C& n
& L$ Z g1 h* B! o, M! ]% i0 s
8 }9 G( k" K8 k#define S11 7
/ _9 O( D1 h$ W# j3 ?. V" Z#define S12 12
: X# p$ F0 x3 J3 @#define S13 17
2 W: b$ t7 _& t! |9 i1 ~' H#define S14 22
; t+ I$ S5 o7 f9 @0 b/ n#define S21 5
5 K4 z' p3 s+ G( p9 C8 x6 H#define S22 9
3 G$ n, b1 _" I1 o2 [; u0 {& A#define S23 14
" c$ G. ~. T4 M) s& [ #define S24 20
; H9 m- _# ^' F9 z9 a( J: m1 Q #define S31 4
2 u* {+ B: T4 V4 u#define S32 11
; t+ T$ l- L1 P( t7 }0 Z#define S33 16
+ s6 H" {; ^! G% {#define S34 23
( \' A4 z9 t9 R#define S41 6
& d3 r+ V% X8 G: C1 u% Q2 [#define S42 10
* H! S4 U; A# Q #define S43 15
/ }3 }* X$ G1 W( r9 I#define S44 21
5 \0 }! t4 b+ G' ^3 R
0 Y9 B7 Z q% s. Bstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; V' j, S3 H# p5 ~1 y( k- M static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& M {8 F; d5 Wstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 i% n1 C$ |( y/ c* E5 v6 r- ^static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, Y% Y( M) A9 ]static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' I5 R$ H% J) }' k# B/ Y! b5 ]4 F
) G3 }. s9 @6 W( n# 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
8 P) g2 }% A$ b: V2 \ d};
+ v2 }/ l/ q3 ]' x' k
7 ?% m6 ]8 `7 ^9 d& E /* F, G, H and I are basic MD5 functions.
*/
' i; B. X0 t0 h* c* W! } g, V+ _#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ C1 Z" ?- R# O" d. @! G9 a#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 N) d. _8 f1 C( E, O8 t #define H(x, y, z) ((x) ^ (y) ^ (z))
6 l# k- p) j( o) W8 E5 M* m; _ #define I(x, y, z) ((y) ^ ((x) | (~z)))
+ j4 I. [( m6 ^
4 ~- I; c" K6 w8 p! l /* ROTATE_LEFT rotates x left n bits.
*/
) u. D/ ]+ @8 U. @ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) b! b- i& Q8 ~9 z5 I, ]- f- H
. S2 N6 s4 r% l l% P9 O/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
9 `! ^8 |& d; j u8 URotation is separate from addition to prevent recomputation.
*/
0 @ r; x, j- _. h#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( c1 o1 \4 _# G/ a# D9 r, B$ d#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, }: o9 v' L W' i#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ i* g8 B" d( F2 d! C8 F, e #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
z; \/ t, L' k! F0 P0 X& r" i
" N& f6 X& }, X& |6 U6 i /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! E, M7 n; a4 W void MD5Init (context)
) a6 @$ f& m" G& P) \% G4 p' s0 M* bMD5_CTX *context; /* context */
- Q( }! D" u' c2 c" J5 U% b{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
3 t8 t2 \8 Z# K% r* F# b*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 e4 |0 W- o. n; l+ z) B6 ~ }
; v9 @/ r* [+ _% q
: H, F/ \. z- q/ z: k /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 G6 P4 e3 u4 e! x8 r4 k( |void MD5Update (context, input, inputLen)
5 O6 P( t3 q |" X+ U1 r8 ]; ^ MD5_CTX *context; /* context */
0 Y0 T4 F1 U+ S( l& E, iunsigned char *input; /* input block */
; Q# g) \5 J0 ^2 M) H! A# A$ ?unsigned int inputLen; /* length of input block */
+ W& c/ u& o; x e{
unsigned int i, index, partLen;
4 K& a! o$ ]9 v; B" z) M
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 N8 u4 X2 B5 I3 D* S
/* 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++程序


* e' P5 S, {6 V
7 B+ A8 L$ h( V" g) _
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-30 21:20 , Processed in 0.079441 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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