中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 ~1 ?& @4 F' Z. O5 z #include "md5.h"
; [! P! M# Z/ l0 J+ _ Y
% q: E; s7 ]# Z7 R" u" ]+ }. Z/* Constants for MD5Transform routine.
*/
4 X3 |; z6 f' ]2 ^& X: V, E
, C9 D- \4 w7 H
& f0 z0 |, i6 B#define S11 7
5 C+ u/ w1 |6 M0 l #define S12 12
2 Z4 G2 d' z+ X ]# P3 K% x #define S13 17
& G$ Y* x3 K$ b, I" n6 r" D! j#define S14 22
. b' m6 e+ R9 W! V# t#define S21 5
* P" T) X% p) [ #define S22 9
6 E q/ e$ K, h( U#define S23 14
( S E: n; S5 D3 h: A) Y3 O% F #define S24 20
) b) [! Q: N+ f6 ?/ D, ~& b% o #define S31 4
/ n- i, M4 v: f! V: a #define S32 11
+ Z6 c" K/ P6 J1 ^ X$ z #define S33 16
- v9 P" G X: b* p. W4 c#define S34 23
. ]1 M; y* K5 H0 T#define S41 6
8 H3 L2 O# p4 K/ ~2 o #define S42 10
: Z- q- B" w- w/ E# a& u# L( N#define S43 15
6 G# p7 W5 l5 Q/ g6 H* ?1 h#define S44 21
7 a! R. S' z8 Y) a# z( A& x% E
! \- I2 B/ x* f4 r2 w# E j8 }static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 w" {3 } e% b) R/ |& f' e/ ~: ustatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
. L3 ^2 D# K1 { X8 S* T static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* z2 U3 w8 x+ k! ~& `& k static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 M6 {' G7 B' ]6 m static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
: Y3 ]% @4 U+ m+ a' v+ [
) I& N. v8 a$ D; G. ~ 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
e! S b5 k- v h8 g };
( w T* Y( w8 ^' u. Q
2 }: x* W+ V& {9 r1 m% Z /* F, G, H and I are basic MD5 functions.
*/
: t* y/ D, Z8 D1 K% E ]) x#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 B* t. y. r$ \- k) T9 c#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ G4 X A+ Y& J3 T+ F1 f U#define H(x, y, z) ((x) ^ (y) ^ (z))
, ^7 x6 O- K8 b #define I(x, y, z) ((y) ^ ((x) | (~z)))
0 A; F$ L8 |8 k; k0 l! k( [
) J* c$ Q# [5 q- z2 P) n /* ROTATE_LEFT rotates x left n bits.
*/
& [. v- |7 q& v1 P8 B+ _/ \. w #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" y1 j B, }( [& a7 G* J" h$ p5 |/ [, T
9 w. U% F" Q1 t# I) Z, ` /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ v( H2 q# M% eRotation is separate from addition to prevent recomputation.
*/
5 P3 q) L' [! 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); \
}
. A' N/ g2 n5 K! {1 {3 X& H" H/ \#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& C0 e( X$ h6 F) U#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. u& M6 ^8 I7 ?! @3 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); \
}
$ @- Y0 b$ k4 e
+ a6 O9 Y y) N2 ` i7 d: @. f- ~! I3 v6 |1 ]/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* ^7 @4 @2 R/ N zvoid MD5Init (context)
4 [5 O; H& d- CMD5_CTX *context; /* context */
( P: l( D0 X! a{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, t2 t& K7 `/ m; X( {*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* q( i$ ~4 r# E3 G; y1 e& x }
9 {# s- |+ ^$ x* u
7 F% A* D5 ]# }. p) O /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! y+ x" h* N( S/ g void MD5Update (context, input, inputLen)
$ P$ M' i6 c! U. x% M1 c" OMD5_CTX *context; /* context */
0 \/ p4 }6 r! Y* q) Z$ I0 D9 Uunsigned char *input; /* input block */
* I$ R/ e* N8 O. J' w% p5 Nunsigned int inputLen; /* length of input block */
4 L( [# _$ R9 ^3 `6 u2 z) ? {
unsigned int i, index, partLen;
3 P& A: a( D$ z1 d7 |2 \0 G
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 S( [# U6 l5 v; @8 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 {$ o. P7 X
& U f, }! ?, H/ B1 i# n
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-31 21:34 , Processed in 0.097149 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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