中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' v% Z, Y" ?5 P( s2 v! V#include "md5.h"
( t3 j7 j/ l" Y) o
- t' a* Q7 a1 j /* Constants for MD5Transform routine.
*/
: N$ a6 j0 L( s0 C; N' K, Z8 K y
. L+ q0 q( ^' [6 u) U
" A: G. h" z, q#define S11 7
$ u2 f" g$ I: `$ U0 f #define S12 12
" U$ u9 j$ l& Z" y#define S13 17
9 }9 v7 y+ D' `( B6 T' H) k#define S14 22
: s% E8 m. P3 {5 {, S E6 n# Z" M! ?#define S21 5
- V+ I/ u7 P* f#define S22 9
* h/ b1 i! @) B#define S23 14
( V/ I3 t( C0 u- E+ @2 ^! V8 g" o #define S24 20
$ A: t; N) u! h #define S31 4
. F: r9 M0 D e% k- p#define S32 11
8 W/ G3 H) |% | #define S33 16
) Y# O+ q' ~8 S, _4 }#define S34 23
0 Q, a. j4 P# t, F& a #define S41 6
! i7 _# k3 H. j5 C#define S42 10
- H2 X) Y7 c9 n+ `# P( N, v #define S43 15
" x. s1 J0 k( K% R) k#define S44 21
( f/ q, _( ?+ d# d3 k
* c4 b* N3 ?$ L' ^) [0 j/ ? static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! \& I% v* n" @static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! g; O- x; T8 V: w& u4 N- E static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) d4 Y: _9 F# u8 L3 M static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
( v$ u5 d. ?3 |, H static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( i3 q$ V+ r9 ~9 h4 q
8 ]% Q- T6 n W! O0 Dstatic 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
7 D% F3 g& H8 Y& r};
Y6 ?% s7 y3 O: H
, Y- X9 J5 \- `6 S /* F, G, H and I are basic MD5 functions.
*/
. R8 J! j* P$ m) \: y8 `#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, Q4 j& ?7 m2 N, U3 [8 S, q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: T2 z7 ?: O3 z9 @5 ^#define H(x, y, z) ((x) ^ (y) ^ (z))
/ H- {/ h. v) H7 ~5 H#define I(x, y, z) ((y) ^ ((x) | (~z)))
' E" w* e V- P7 z+ s. n$ y3 M
( D9 i1 I' R/ @! c5 [+ ~/* ROTATE_LEFT rotates x left n bits.
*/
4 [' l& E, ?5 B" `% f# B3 N+ c% X #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. q D& R7 ~, V
: B; ~ a- C. p4 g$ P: _ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: ~9 f' b7 K* f) L6 V Rotation is separate from addition to prevent recomputation.
*/
- S/ y8 @( \% @' P #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 k2 q E. i9 g% N #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ |8 r, i5 R. d1 i- @( K# q! P' U #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! ?* |9 d0 K: o& O/ G5 O7 F$ 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); \
}
% V2 ?3 _0 a& l& o* O9 b& k8 l
" j2 Q9 [7 I0 l /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, A0 ?8 t4 v) m. Ovoid MD5Init (context)
8 T+ Z, T. |* |/ _9 s0 k MD5_CTX *context; /* context */
2 g# l' a5 x2 M$ `4 k- M7 r{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
( h! {5 p& R9 l2 ?/ U X/ n2 o */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# M n. |; Y0 P8 U}
' z" ]7 |9 L2 a1 e" Z
5 F) l$ s0 \ I0 W3 v0 o /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ b; O% d' L2 Z$ P+ r+ \ void MD5Update (context, input, inputLen)
% ~% v: s% Z: h, I, T# C# |MD5_CTX *context; /* context */
+ a W$ M# R2 _4 K1 b+ s6 k unsigned char *input; /* input block */
! U- h- p7 X+ _+ [% R! m9 d/ junsigned int inputLen; /* length of input block */
- x3 P9 ^/ w% V, \ {
unsigned int i, index, partLen;
: }; [% N& U6 g
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 I3 @$ ^% a) M- 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++程序


# L: g9 P% ]% c$ o( Y6 t5 b, g3 W
2 Q) V2 M/ O9 J" w/ e6 w
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-2 11:03 , Processed in 0.210706 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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