中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" H8 g3 g* j; h J- \' T( h- c: s2 ? #include "md5.h"
7 R6 i- M! {4 g+ m5 x- r- V# _
# t! U# {) i( T /* Constants for MD5Transform routine.
*/
( ?) Q! i) @5 G
; F7 v D+ E' ~. \; C* O* B. I
: s5 p# x- A4 Y( p h: u# m4 m #define S11 7
1 z, r" Q+ w8 C #define S12 12
" {% S, I, S/ o: C% `2 ^7 q #define S13 17
+ {1 g' r8 t6 p& z#define S14 22
N3 J8 S- ?- Q" F+ D #define S21 5
7 q8 v. b: ~+ ]( N #define S22 9
8 S. b2 K! t3 {7 P4 y! U) D- L; \#define S23 14
/ N8 Q8 j1 c0 p2 c#define S24 20
8 H9 H# o# y& p7 T% J( f #define S31 4
' p. c; m8 `! @ #define S32 11
S7 K; r* ]9 ~) l! M: K#define S33 16
0 U% A' \* y6 h/ P #define S34 23
4 O4 I4 w* z% [1 e3 H9 z1 t4 E8 b#define S41 6
1 l& E% e2 u9 o/ O. c: {9 w #define S42 10
/ Q3 B; w9 H/ z, S8 V0 C #define S43 15
$ y# F( @$ s& h2 u& X" D4 S #define S44 21
' S2 c, U' K% p( u0 N/ C! N
, P+ {4 z3 `/ h6 v1 ]( { static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
' e% }0 F# c3 D2 a Z1 Astatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% D+ d. v2 g0 A8 c9 ?' W1 ] static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, {' h5 _; B- {7 }6 x! T; j% ~' p. n* m static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 x, b" v- h- z( S% M& f static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
" _8 m- b$ T1 i5 l% |6 I
7 `/ o4 x& A6 b) t8 @* R- [' c( V. Zstatic 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
, o1 j. P7 q( D0 r; j+ ~: o};
9 V5 O; w+ _$ R) d% I: j1 {+ b
, ]8 y8 G. o8 Y/ N; p* p6 S /* F, G, H and I are basic MD5 functions.
*/
2 _6 S1 M7 _0 N J9 N#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
: y. [3 Q' x1 w- [8 G5 c' x9 D" I#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 f% X8 H$ J2 W* v& H #define H(x, y, z) ((x) ^ (y) ^ (z))
! A1 h& D& {9 |( r, @ #define I(x, y, z) ((y) ^ ((x) | (~z)))
: J5 r$ \4 G y# g& t; v
K; m6 ^- H5 O" I9 b# U0 w/* ROTATE_LEFT rotates x left n bits.
*/
: z# P+ O: b8 A X0 q; i# c9 q #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 e2 O8 ?% d) F
}: K7 H* F: n5 {4 H/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 H+ k; k: r4 D# w' R" l5 u Rotation is separate from addition to prevent recomputation.
*/
+ c2 m6 D3 a" M2 r3 s #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 ?* U' h% N) w2 W; }# g' Y #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; }: t- q/ ]! L2 N6 ]1 ^$ q4 i8 d #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# b2 t1 R4 Q& b1 y #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 a. B4 G' G1 x' W+ R
( `0 t5 K- H4 G /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 J* c( W6 g" J; Kvoid MD5Init (context)
# n V! x0 e: m) x5 ?* @9 AMD5_CTX *context; /* context */
, E( v* g7 ~7 e; T$ J. m* ^{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
* p8 k6 V; `2 X! K! z- {1 z*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. P1 [. A6 p/ c4 v1 A }
. u+ @# d! W2 h* J8 q9 u
% V& s# Q& t7 F4 U7 n/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# |" y7 @) T7 S0 T5 J void MD5Update (context, input, inputLen)
- ]6 b, }9 [5 B# G( R9 {6 xMD5_CTX *context; /* context */
5 G* X. n8 n1 _! @1 \! N5 o unsigned char *input; /* input block */
T! n* k2 O9 x7 h' C% @( j) @unsigned int inputLen; /* length of input block */
4 N$ l: s$ g; c' r {
unsigned int i, index, partLen;
$ U4 |5 Q( Y) ?( A+ c+ W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' r4 Q4 }( R$ F" l8 w
/* 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++程序


; W! ?4 H" o _4 z2 I
, K$ N- @8 s- O9 O" ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-12 01:12 , Processed in 0.060320 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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