中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, d! ^" L3 d( o# a1 s% a' t #include "md5.h"
1 G) S' p% ]! i0 J2 H& a6 R" X
b/ I4 b; p: E3 i0 A: V4 U/* Constants for MD5Transform routine.
*/
- q5 o/ \2 z& Z* c6 M. ]
h" Y0 g/ K% E0 z0 [0 X Y2 L
/ N/ U# y; ], k; v#define S11 7
! P) s: S/ E+ V! O/ h #define S12 12
# h+ j) _* w+ i, {* k- @* u$ e0 W2 K% o #define S13 17
) G( R! l9 t9 l( }* m9 j3 _; ]#define S14 22
+ t' G( w% q0 i: h9 \6 n#define S21 5
5 V4 ]! Y) c* P6 b3 n+ x#define S22 9
# E/ J7 m0 F$ Y% V+ W8 h$ ?7 g#define S23 14
' D; \3 L3 x2 Q& g! { #define S24 20
% X+ p. h$ b7 C #define S31 4
' r$ X! J1 K3 {+ j #define S32 11
2 n& I( P4 y* E5 U+ f5 K' X( l#define S33 16
% Y8 a6 i9 [6 r$ y0 H #define S34 23
0 J4 O5 ` I' Z- O#define S41 6
9 N4 y6 Z7 Y$ n4 E9 ~! r#define S42 10
2 Q7 z6 R$ }: C0 d. d, g#define S43 15
4 f. F% h5 M/ {% h) @8 r* a #define S44 21
0 ]0 A7 g8 S) V/ `- x
6 {5 Y* i9 Q, B static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) M( f' O! S. F/ Astatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 l9 e5 i y% istatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: |: K" G# \7 W, w# j3 q static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- l' I. a7 \8 B) c' T/ E, jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
7 ^$ W9 o/ v! Y3 m! _
; M1 W& G [' Vstatic 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
' V$ [% M3 i) Y8 {- X% {2 ^) ` };
3 a0 q' N3 h* c; s9 j2 E1 w
4 w0 d" d0 S( d1 I4 F. s% F/* F, G, H and I are basic MD5 functions.
*/
$ c! \" v) L' K7 S: N#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
7 q# f, \/ c. x4 r #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, ^2 [# @ q+ d9 X+ e#define H(x, y, z) ((x) ^ (y) ^ (z))
" a2 Y7 N3 n4 ?% k& r #define I(x, y, z) ((y) ^ ((x) | (~z)))
6 l" j8 Z" J9 q& H" H8 P, g
0 s. \2 D5 G4 u; e/ p2 b" g, x /* ROTATE_LEFT rotates x left n bits.
*/
8 B7 V* U0 \! Y6 {#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
4 A. u/ r4 F& D n9 ~; b0 R% G
+ I% z, F- X( A9 \( s4 l/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 p% U% t% h6 aRotation is separate from addition to prevent recomputation.
*/
% |4 H5 W% p. f- I i, r7 v #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 T& s% f) z7 J' ] Q#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. _9 U/ c6 S9 I V( 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); \
}
$ _/ C9 x" g% S# t( q9 O/ ~2 @#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* ]6 _& u- D z5 f
. F7 p v; ?, S [/ m/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. r' n1 y4 r! x3 f! I* {3 w) { void MD5Init (context)
0 r* N* A& @8 n9 m, F% b8 Y MD5_CTX *context; /* context */
' s9 P" l6 n4 }7 [1 C {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 f- j$ S# ~. F1 V- U5 Z. t* _7 N*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 z4 X. k) _$ g/ T3 `}
& M) ?2 @( B/ s) d; N L8 M
/ M+ B# G3 }& l /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* T0 Y3 H3 C# m5 ]: s6 x void MD5Update (context, input, inputLen)
S8 u$ R% @* D; c& R& h MD5_CTX *context; /* context */
2 b) d1 |& f3 u. m9 b0 C unsigned char *input; /* input block */
9 {6 m3 [( c6 Q7 ^ unsigned int inputLen; /* length of input block */
' I& @: S* U6 F% E/ A{
unsigned int i, index, partLen;
8 \- G' Y2 J+ }+ F' Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
. k" u. B1 d, {- R5 c
/* 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++程序


% X; a O5 I# W7 g9 O% c
4 s7 l! J+ T2 }3 [( B) o
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-26 09:43 , Processed in 0.079046 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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