中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
6 M& Q( T, K8 a9 ^- x/ D- j) @ #include "md5.h"
* ]; W( d5 |, {; \$ k
, Y# f$ w* G, d) v- O /* Constants for MD5Transform routine.
*/
, a8 @1 J _" ?$ t( k- a! h
9 _- i) T5 `6 \' M
7 @- r- r) y9 p# q' N" y% j6 m% g #define S11 7
9 h- i: o! m9 o+ S( A' A+ ]4 f #define S12 12
) i6 m: u) k7 B% Z! k#define S13 17
5 Q1 L' N9 x1 x #define S14 22
' V- u) y' C1 t2 |% n2 @ #define S21 5
$ Y# o0 ~; {, m" f# {' b #define S22 9
+ X/ o; Z& O6 z" G0 l4 ?8 r, d' N6 N #define S23 14
$ D% k) _0 Y/ F( Z1 M' Z; u#define S24 20
3 M4 m4 H( G2 [% h#define S31 4
) x- ~1 I: U! k; G/ S8 |; N# T#define S32 11
- d+ z) s i( @( v' |; J9 F #define S33 16
" \% q* Q7 m' O% h2 ?4 l, Y, { #define S34 23
: r( e' L7 s3 N! C! s7 E: v& e #define S41 6
1 p6 b" |! G3 C0 f1 b6 i5 Q" \4 P #define S42 10
( B2 ]9 W/ ^% m, C p4 F#define S43 15
) {0 Q+ N- W8 Q) @ #define S44 21
3 U9 |( ^0 k0 f, d) F
3 U& n' Q5 X" f S( J+ q static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, [; p8 G0 ?% x# o6 ]% \static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% r/ F% i! E: b& ?6 X static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 ^1 R6 o3 E/ L static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* n) z, j6 J& }1 C' k- _) I static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
0 a ?" g8 @) V- M; D
- c5 t7 C% M8 J% ?6 ~# B* ^. i 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
1 y1 g! I) c1 ~2 x};
5 Z8 S n" a/ J/ o
( N# B! ?, y1 @. \2 I# s /* F, G, H and I are basic MD5 functions.
*/
" z" M) Z7 ?/ E- S#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
5 h' _1 n; ^7 J. L% X #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ w, ]& F2 J1 M; M5 Q #define H(x, y, z) ((x) ^ (y) ^ (z))
# w" R) c, V0 J$ K4 o0 E#define I(x, y, z) ((y) ^ ((x) | (~z)))
. @" u& M X; y" X) |% x# v' P+ p2 G) U
5 s) X0 L* q1 H/ K. W9 Z" w/* ROTATE_LEFT rotates x left n bits.
*/
& B) t0 v$ e. i! ]1 H- O$ {#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) T- w' \4 P* @1 [4 ^
' D% G( j% @; ~) F. V; |/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! C0 D' `2 v; }! v1 u Rotation is separate from addition to prevent recomputation.
*/
' s% P: q' Y+ u7 J% L! t0 \ M#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" V! X) A7 O3 n2 r! o#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
v# ?4 Q# z7 V#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' h% G. E5 |0 X+ ~1 x, L- M6 s #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) z* \: ?, v: E' V- K8 k
6 K' `. }8 u3 m G; a* i7 M1 U /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" g H7 t+ x# w9 j4 S: [ void MD5Init (context)
- |* i7 J* i0 Y5 |# d9 G2 `) } MD5_CTX *context; /* context */
2 Z- F. f7 ~, W{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 G8 |% b+ g, z6 @*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 f1 ?. G, a3 a$ Q6 `8 M/ y }
, e" d' {; G6 y9 ~' V4 h
4 A) v" I4 `" w* O5 J1 Y, L/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* I: G& s6 ~% M; ?void MD5Update (context, input, inputLen)
; ?* C/ ]3 H9 u1 P( l& R MD5_CTX *context; /* context */
" C" \, F; {3 y7 a: A7 S; x- i: w- Ounsigned char *input; /* input block */
$ u. e0 h+ n/ N. e4 Nunsigned int inputLen; /* length of input block */
7 Q1 x+ T( x. V4 @' e, C. S1 o. B {
unsigned int i, index, partLen;
! F. H1 y1 ?- S6 X# G! T+ d
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
1 O* z s( d( I) W& j1 X- ^
/* 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++程序


4 I1 a; |" s, J. } O5 t2 ~; a1 J
/ e( q$ q+ H# O6 m
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-3 04:58 , Processed in 0.059874 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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