中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, v6 h. f q- O; _6 b+ R #include "md5.h"
! g. m+ K) n0 ]
/ n2 o0 q( Q+ ?+ e* W5 z /* Constants for MD5Transform routine.
*/
0 h8 {2 L& N- f/ H; z
' R2 a' T! N0 l, n4 e
, F" Y+ Y% o% t/ J# k8 y; {9 _( D#define S11 7
, Q( A4 d& a3 K #define S12 12
: S0 V$ a# A6 I/ B" @ A #define S13 17
* Y( ]. w$ z: z( s- c6 k5 Z #define S14 22
" Y2 s: R8 r' G/ ]+ Z9 j+ \: k#define S21 5
# S+ B! j3 A2 J# ~#define S22 9
, }# J; S m& N' v#define S23 14
1 q4 I5 y8 m8 y* i4 f#define S24 20
7 V" R, ~9 N, x& E- n* b9 U #define S31 4
- E2 [+ U/ u$ H( v* _1 @% Q #define S32 11
; |( Q9 E% t7 u6 m #define S33 16
0 h% {- J0 B4 d7 P. m#define S34 23
7 {' o; c7 g! W( k7 n9 O$ z#define S41 6
1 e& \- n( H$ j0 [ #define S42 10
0 v# z9 K' O: `( O$ v#define S43 15
) z9 ~/ m6 Y6 ^: f$ f#define S44 21
2 R7 P3 u( S& W q9 ]
( t3 I$ w F( F( ?1 g: G& B static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
0 x* s. D6 }5 |5 `+ w' V static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 S% V( h$ V9 k( o2 C4 r R Y' h+ p+ e static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# z) I* L. _# g$ y% v% ?: ` static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
m, q$ ^, M; } static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
?2 e8 Z+ p/ n2 z
4 D9 p9 C* k* I0 h* i6 L* I5 pstatic 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 g* p6 a. A4 u! B3 K. u" r};
# D: M! f) _; _( @0 F7 R: T v% E
1 b, @9 S0 Q; h /* F, G, H and I are basic MD5 functions.
*/
0 s. n8 ~" |3 N+ V, k( C8 i#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 S; R, @6 w2 w3 P5 S7 m* F8 C* ~ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" ]8 Q4 c) W6 q #define H(x, y, z) ((x) ^ (y) ^ (z))
0 } W/ [4 v! x) A2 k4 K% `9 ]: _+ E #define I(x, y, z) ((y) ^ ((x) | (~z)))
8 v, \ V$ `+ ~2 [0 S: A( {4 [; n
8 I$ {2 o1 r$ M$ W6 Y/* ROTATE_LEFT rotates x left n bits.
*/
; W7 b$ a' N4 `1 W #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 ?1 K7 a% Y' _
9 h* c9 V, b6 F6 l2 l/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' D. w( L# L# z( J1 ^/ }! V$ \Rotation is separate from addition to prevent recomputation.
*/
; P! A1 P' E; T9 `( q S6 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); \
}
8 E4 h7 e X" U [#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
B5 W z- ]6 Q1 n3 { |$ h #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 p- {1 a+ I7 C- N& f1 A O6 B* p#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
b7 C8 b; A+ A# u
: B+ v6 r9 H+ S' ^% l/ D2 w /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' f, g1 E; ~; X void MD5Init (context)
- s$ b- j9 H7 r7 B8 F; v MD5_CTX *context; /* context */
* D/ v. v$ C" s {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" U$ |/ r/ x6 x' o* g: P: Y */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) V9 s2 [# ^/ c$ C v2 l3 P% [ }
4 {# W) a& U8 G/ y
" \* M9 R4 n* G* s( S5 r& I/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) {' @" Y6 z! r% u% G) hvoid MD5Update (context, input, inputLen)
! | o0 z" H% T g, R& ~7 u MD5_CTX *context; /* context */
# K6 Y3 w3 Y7 |" ]6 }( K- Yunsigned char *input; /* input block */
% `/ [- _, y. N8 ?" S0 eunsigned int inputLen; /* length of input block */
: r$ f) E2 _2 T8 X( A8 y {
unsigned int i, index, partLen;
7 k- N3 e& S8 k W0 a% t( w8 E
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
4 N8 p& C0 [! J! j$ Z1 u: 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++程序


) p6 H0 D) P% i1 ~1 b" ]
# b( f$ c9 L. a9 c% r, P% r7 |( F- H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-27 17:01 , Processed in 0.261944 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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