中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* p* O+ D9 V, B; C6 O3 \#include "md5.h"
5 y3 d1 i- W. {- S' N/ v, N Q
% e, r" q$ V9 C( i0 N$ A /* Constants for MD5Transform routine.
*/
4 A2 g) Y$ }* ?
: K U+ B2 p! T* {5 U+ {* M. I
) ]. X# V6 ~! f- g1 h8 G' r #define S11 7
- d9 g2 h1 h F5 }. O; J0 Q #define S12 12
* m8 h, u; r; M! Q$ T+ x7 X- O#define S13 17
F7 @- ^; F9 f! O1 q! ^* l#define S14 22
3 X, Z/ y, t! Z#define S21 5
% L- r8 A; b! p6 R; W& m#define S22 9
" c1 B! E: ]4 T# n0 H #define S23 14
& w) P8 B5 c. x2 I1 ` #define S24 20
7 E3 P- B0 f4 K' P! b3 X#define S31 4
! Z$ _3 _" J* ^- p: h" P #define S32 11
7 U7 t5 X! P& K#define S33 16
7 {+ y2 K( r. b #define S34 23
) Z$ R3 N# t7 K5 h" |& S #define S41 6
: @4 U: D& l. W2 R#define S42 10
0 g% E2 B% n$ X- [ #define S43 15
& A- }3 H3 e' e3 [. X) Q: _2 q: } #define S44 21
4 N' ~; T. H7 }8 M* A6 e! X
: \6 e4 P9 l# |% i" ]- ostatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ _% c" j% S! G& W+ Q4 Dstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
" b. Z8 X4 |- mstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 \0 H. ?: y& M1 ?) x static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 {5 E# c0 E% D2 ?! H+ m7 Jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- z: L' x) n+ d& J, v3 D
! u6 Q) k& O; B8 h7 O( h, V) Ostatic 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
4 o0 g! T: x* C* j9 }};
/ o. o* ?+ ^, @8 P0 {
7 R# p! k1 d z+ [8 \/* F, G, H and I are basic MD5 functions.
*/
" V' L6 K* I8 r# i, W #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ u* i2 {. N2 i1 S& Q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, O: v6 M. y, n3 ?5 n#define H(x, y, z) ((x) ^ (y) ^ (z))
$ f- A, M" a$ v( l#define I(x, y, z) ((y) ^ ((x) | (~z)))
/ E5 ^! \2 L, l! z- ]$ {. c% ]
9 X6 z$ ?+ X( L5 K /* ROTATE_LEFT rotates x left n bits.
*/
$ F1 f8 c5 X7 S9 U# i+ I) ~! O #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
\4 |5 c) h; N2 @3 ^3 C! x
" m+ Y3 ^+ E9 X4 K5 L" w6 Z /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 r- j1 o. N" Y' V* g& B, P/ V' }; q) _Rotation is separate from addition to prevent recomputation.
*/
; E& K7 v! n/ ?7 q X#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. m* Y) \, \0 G+ h S #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* [- z. k2 U7 T: |! ^- F, J#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; a- w% x# ?! f; k6 t6 X #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 k) }9 g3 b8 ~. ?
0 Q$ R$ e+ J! @4 }/ W) Q3 a3 c /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; D# K4 }. m! b4 P j: ^2 [3 ` void MD5Init (context)
, c. V- k# f! O$ k3 kMD5_CTX *context; /* context */
3 T- i) h# o" Z9 |{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
( H [' J6 _! E5 q2 _ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
0 F0 s. c2 g$ ^1 E}
! M' [8 c v& |3 m
3 H$ Q# x" H9 c0 R9 M9 z7 @; X/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 X' C/ d: W3 L: c3 ^6 E6 N void MD5Update (context, input, inputLen)
, P: ?; l0 b% o6 eMD5_CTX *context; /* context */
# H2 x* f" [3 s6 M; L/ D* k unsigned char *input; /* input block */
' |+ o3 p9 C" `" o% l N. c) n. j) a unsigned int inputLen; /* length of input block */
* C! ?4 u. m4 d9 P{
unsigned int i, index, partLen;
2 A6 y( b6 C2 B# c% ~: k9 j+ y- y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% ?5 h: X7 G* f0 n6 {; J
/* 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++程序


6 y, N1 A- W( |
& p" R( y1 a$ j# V0 F
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-12 10:26 , Processed in 0.066059 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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