中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( L. R- a% [( X6 c0 W$ e! h #include "md5.h"
3 J1 O, C) ]* W3 i, N/ P# P
' I! [$ p% ^" o, m/* Constants for MD5Transform routine.
*/
" a I+ m7 [: T
( P) P" | `% T6 K
$ }! v: o4 I6 G9 ]( W y3 S #define S11 7
+ F8 k% t* J- l W#define S12 12
' f$ G& B5 o$ S( ~( N" | #define S13 17
u+ S( C0 J6 `) I3 j #define S14 22
* ^0 t$ _" { P+ | b #define S21 5
* ?, ~0 Z5 \7 d# | #define S22 9
! [0 c6 F6 Y1 z2 H9 P #define S23 14
' K, ~! W8 v+ |4 S# D #define S24 20
# s7 D% [. s1 l: l; ]& ~" T#define S31 4
9 W& y: ~3 l( X #define S32 11
/ t6 O0 ?: l( S }' i' b, t) ], u* V#define S33 16
$ h# ?& l- R7 q& R#define S34 23
6 v: a; p2 t& @5 A9 N. q* z5 ] #define S41 6
C9 C$ Z5 N# e* v5 G#define S42 10
% n& U+ ^4 ?7 _* ]7 l #define S43 15
y }3 R% D. d/ m+ e- ? #define S44 21
" O" g! T9 Q1 S- }2 i
: H# Q, ?' V: O static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
' i: x9 T& o/ q {, ]static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
" @5 {/ U% l! o: B) n% t$ @ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 R# \& g$ x8 j2 f& P. fstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; x2 Q, ^: B3 z4 A: |static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 p) |! @# ^! F: O1 z
0 ?7 T& I1 w- ^( Q% f& \! w static 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
( X9 |% O2 k- c( f9 s };
. G4 q* O% X+ o, q/ a- c- j
' W3 X- q" \9 a4 F1 D- S/* F, G, H and I are basic MD5 functions.
*/
" k' o9 d& n' e c#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- l0 i7 J' K5 q' s5 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, Y" N2 v% O% {, l. X8 |0 v, C& j #define H(x, y, z) ((x) ^ (y) ^ (z))
( Y, A/ y1 i4 j* Y+ c' d" p" c #define I(x, y, z) ((y) ^ ((x) | (~z)))
- x' g. z6 s9 @" F: K7 R8 q
/ ~9 _+ _/ `& y2 | m4 _0 _ /* ROTATE_LEFT rotates x left n bits.
*/
S% Z1 O! P: m( F" G6 Q#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! ?- b9 o* _' [2 {0 G. w
( G1 C. x0 H" w# b+ B /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3 ~$ o1 z1 h, `2 u" ]Rotation is separate from addition to prevent recomputation.
*/
0 d3 t' D6 Y9 I. {- r9 ^#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! Y$ C" V* C8 @; ~4 U7 w#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 c ~ @' e( t#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ w% G) [% v+ p3 c1 d #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 @ E7 k! @+ w( R/ i+ `
0 ]8 c% v6 w1 I3 ], g/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
/ |3 Q4 @. v& [$ Y/ j0 a; \ void MD5Init (context)
+ T, Z- t) s: l3 dMD5_CTX *context; /* context */
- ^% p! r7 A. e K& {. ^{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 f3 i/ E0 N7 }- U2 _% O; s' T' G*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- g G- p$ V- A; b. k9 v# @}
* e4 L0 d- c) X0 F1 u& [' ]
6 O3 K" L0 o/ a) R! G) ?. J/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. Y' e2 u: ?( e: i5 Avoid MD5Update (context, input, inputLen)
% @% w3 p) ^) [ Q& } MD5_CTX *context; /* context */
& \* Y5 _, x& S* j5 @0 ? unsigned char *input; /* input block */
5 b7 x; a3 n( d unsigned int inputLen; /* length of input block */
e- E% R) d% w7 c" k; x{
unsigned int i, index, partLen;
$ `5 D5 z2 n e
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: z5 [0 n& t% f) H, I0 m) H' I, ^
/* 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++程序


" _5 E5 r, W# o$ m4 i6 i J( Z9 V
5 w" o7 \' i! A5 w L* S
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-1 10:49 , Processed in 0.057976 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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