中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 }( c5 ~: L' w; ? O, e. \0 H. ~#include "md5.h"
9 Z4 s5 ?# C/ n% c N1 H( l' G
5 z+ Y& k( K1 d5 _) P* q0 ]/* Constants for MD5Transform routine.
*/
9 Z9 C: n; H1 j% R
" A( S$ c0 J) L' Z
& k# d4 S1 g5 C: m+ H#define S11 7
; q3 r$ B, R6 L8 }$ ?& {#define S12 12
- R8 X% c1 }- S/ ` #define S13 17
" q$ g' d. D6 s8 j/ X8 A#define S14 22
9 j2 j( G' J5 W6 ]4 S& @ #define S21 5
+ S5 }1 i! ~% D: A8 W #define S22 9
+ i) D! G, ^: O; x8 ~; r/ S% ~ #define S23 14
: Z' n$ A0 m4 W# K- _7 i X#define S24 20
0 g) A- O0 @( R" ^5 Z) T #define S31 4
1 f$ ^) I. H+ r& x0 L' B#define S32 11
0 Q- D# F' p3 f( U0 O4 u* `& m #define S33 16
6 ]9 a/ I6 V" O #define S34 23
5 o: q5 _: t% U( _* H #define S41 6
, j6 c; L' F( l$ z: i #define S42 10
; l) z0 G' i3 z5 z3 D#define S43 15
* [# h8 G6 V. w5 S#define S44 21
( h% `+ H/ R6 C! A* A0 k
$ N# ?& d/ C1 W \5 x" X; n& m& ~ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ u( I5 I+ I; p" F/ nstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
G2 |) A5 }$ i) ?static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 t; [6 F2 c1 G2 z$ n0 {& d" G5 f6 a static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, C7 T& h7 o4 H$ k4 M- D& X static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% R6 E9 B- O& ^- H' x
) r* C3 E* q; h ~9 J. G9 qstatic 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
2 A7 E2 T; |6 A5 z; N6 z! L};
: M1 M& s, E* N% J' }' t+ W
8 D8 ~3 {3 _" v: z0 b/* F, G, H and I are basic MD5 functions.
*/
4 I/ w) K* r& s#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# v% @2 [% Y+ c& q#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: Y' ^( b. t' g+ C3 @ #define H(x, y, z) ((x) ^ (y) ^ (z))
1 R: D$ a' _, l( I: B: u6 J J#define I(x, y, z) ((y) ^ ((x) | (~z)))
7 D! Y: @2 }5 w. |, e, r8 i
7 f+ Q. ?& B/ ]2 K, f, y" o/* ROTATE_LEFT rotates x left n bits.
*/
. r3 \4 P' Z, P1 C$ T* u#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! E0 W0 m: _6 ?1 ?3 W, p3 \3 O5 ^
' z" H# e" {) ~2 Q4 M/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& b' I2 j/ B D6 `+ P. N Rotation is separate from addition to prevent recomputation.
*/
& X, c9 A0 V$ ?% d #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 D% l; e2 o+ f #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ N# @ o0 R) g. v, [, N8 L #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- Y+ S6 }7 x1 Q#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 M; _7 _7 Z5 m+ y4 {% [# [0 r6 e
3 o4 r- u" c9 B/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( }! D1 E' y- z8 S+ cvoid MD5Init (context)
( @: z5 f! g& c5 J3 d9 {, G' UMD5_CTX *context; /* context */
+ [0 c, L8 u/ f+ D{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 L$ Y/ _1 G( _! K9 H# U* }*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 ]/ V4 R5 h) J) T. w" ? }
9 ]% |. J* h) W9 d4 l( X
; [7 e" d/ {, V/ G* e+ R5 ]/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, }, J$ [+ z. j6 E) Yvoid MD5Update (context, input, inputLen)
$ r8 D7 f* \1 \- S6 ?/ FMD5_CTX *context; /* context */
* Z, |2 T2 `; a2 p" _unsigned char *input; /* input block */
: Q3 z3 l7 K3 S7 H# V unsigned int inputLen; /* length of input block */
; I% Z& j [# U; }4 s3 y$ o1 }{
unsigned int i, index, partLen;
6 w- x7 t" K/ c6 @ U& b4 |
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, e( U; @& L/ u V2 z: Y8 u
/* 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++程序


# [, F+ K( D! k: |) d
2 c, C6 q) z0 L @( W4 R
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-30 00:15 , Processed in 0.063226 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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