中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ A" y$ Q. b0 M) g: v9 n$ ?6 @#include "md5.h"
; c7 n4 ~6 y9 d2 }( Q$ F6 Q& ?; e
* W& R1 s6 v" i9 z- {5 v4 D9 U1 n/* Constants for MD5Transform routine.
*/
$ Q; |0 R: s* [, ~. L
i, J G+ K# t# V* R
7 y; V% a+ _* J #define S11 7
; J4 q- Z+ c( D4 m4 S#define S12 12
/ ?1 } ?! ~% s5 s#define S13 17
3 i p$ y5 |4 ~' [0 q #define S14 22
5 H* i s8 L3 A! i- h) K#define S21 5
$ W, u4 o `, }( i$ Y #define S22 9
4 N6 E* I) ^% M I0 P7 K T#define S23 14
3 n" }/ z; Z6 F1 }& b T) n. l4 P#define S24 20
, [3 r( q. k; K* a' y G8 e$ N #define S31 4
: z0 C6 u8 a2 N% W: |7 m #define S32 11
J- I. k2 \% M! n) ?2 N#define S33 16
1 ^$ K( Q; ?, j* N) Z8 I& j9 O! n#define S34 23
' C' |8 t8 D& ]0 W6 H#define S41 6
4 r0 ~) t7 }1 ~' [, Z# ] #define S42 10
* ?; `& x9 g( u- ?' U: j9 R9 a* U9 w2 A2 _ #define S43 15
/ G5 C, A2 ~" d G& W A #define S44 21
& l7 `8 _. \8 t* h J/ g
) Z5 V: H! Y" W static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- F+ p" m0 v# g7 }static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ N5 z' C- u% R! I5 U+ X# jstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) }5 h5 i0 L5 _$ R& S% b static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, i& ]% d! w5 u" Y9 d! A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
7 [( f2 ]4 x* D0 F7 N g9 w
* ]" w' e" T C0 bstatic 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
6 {4 m7 n8 Q+ A* }1 N6 O};
7 y3 k" C+ E5 p4 p7 p
- q) ?/ v8 d) Z: [4 o6 u /* F, G, H and I are basic MD5 functions.
*/
; K7 C! t) ]" |8 f M! C2 p8 F8 ~#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% g6 \9 r; P- T3 I#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 M' B& y, f/ A! E; d( f# P$ f, t" _0 e#define H(x, y, z) ((x) ^ (y) ^ (z))
. ?8 a* R. ]5 k6 |& w5 `#define I(x, y, z) ((y) ^ ((x) | (~z)))
# B; u' |$ V0 J& `" U1 {
5 V3 A: _' {5 S7 }- J; T& M /* ROTATE_LEFT rotates x left n bits.
*/
' |) ^, E/ X9 w- @#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( o# ]" C; ]+ {# P
& y' V. i7 k1 ?! K /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ N1 B9 T8 Q! D; _5 E* o3 {2 B, R Rotation is separate from addition to prevent recomputation.
*/
2 k* a+ \5 i$ @8 e" i #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. B2 Y# u3 O9 M9 T- _) 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 q, a9 i5 O+ Z+ [( ~- ~ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 X: L' Q0 @1 X" L #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 I! [$ n0 P/ U7 [6 i7 h0 A# g
7 o& T) L* s7 M; l, e( Z* X /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, k4 k& J9 C9 S2 f& Vvoid MD5Init (context)
8 p! P L5 x6 {& a; \ MD5_CTX *context; /* context */
1 p1 y0 B$ R x{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! J! w) b% i/ @: t2 R! B*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
& @% n0 m( E! w& G1 {- @' V N) t}
* U# ?: o3 u, H
9 t+ p2 ?6 ?( N$ s, e5 n' z/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* e7 B6 \# P) j E' \1 ^void MD5Update (context, input, inputLen)
; [8 I5 |0 j$ R" G9 { MD5_CTX *context; /* context */
& I- _" C4 p4 V0 v3 k" _( z unsigned char *input; /* input block */
( L5 J8 E2 R7 K/ z+ l ~3 O6 |unsigned int inputLen; /* length of input block */
( U$ V/ Z$ }7 ^7 ?{
unsigned int i, index, partLen;
6 Q* ^: H' ?- W/ p5 k% {
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
4 b1 G3 n' B+ m8 F( h+ F
/* 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 ?1 ^* S K# [6 O" V# x: P
. q5 }! b( w' Q" m3 h8 L
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-9 01:09 , Processed in 0.086846 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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