中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# f0 J9 e$ i: m9 m#include "md5.h"
' X* q3 I* n. R0 ^& e: \8 \3 J4 s7 L0 R
" a3 Q$ b* v& U" _ /* Constants for MD5Transform routine.
*/
7 d! e) }& ?2 w8 M; C5 l
+ ~1 N9 D# K7 ?5 K
" @4 C' m5 o% {% W #define S11 7
6 [6 Y1 Z$ g0 x2 i; w6 I#define S12 12
7 H* R2 z& P. k! \- C4 U& f0 M ?#define S13 17
1 g4 A l0 R) P ^+ T #define S14 22
, z$ B& f! K+ M; C" m0 _ #define S21 5
* {& r+ M' R# v. W" w' r( { #define S22 9
) |; S/ d0 Y, G" Z #define S23 14
* q0 b% W7 f2 n# v/ l9 S: z5 k#define S24 20
" m1 b' U1 j2 c n3 z8 r7 ]" ?# x#define S31 4
9 z0 x0 Z* |, }& C+ p, A+ d #define S32 11
5 t% T* E) c8 g$ {# F n#define S33 16
. E4 H1 v# r- j @/ x- g& g0 F #define S34 23
3 c. _: v3 G j$ i8 M2 P6 @ #define S41 6
4 S, s7 b; v) H% V) @+ j#define S42 10
0 ]2 ^0 E; ?$ j* G/ e% x- Q5 H$ Q#define S43 15
8 i1 D% k! w% S' k9 T( P$ D #define S44 21
3 A& _8 K$ k7 ]4 e; e& e
& @1 G1 V4 M$ r8 j+ r, \0 f static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 ?; _ I" n7 B! L$ K6 N5 xstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
3 {3 h9 C/ ~2 J7 J' { static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 ~2 u* B0 B( G+ B: V3 bstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 ?9 E$ `/ S, @8 @+ s static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. C9 c5 n( `- D) a; K
1 i: q& c1 \* j, L* K( W( A3 ~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 Y6 O1 H# J" y* A: f };
0 g2 e- R2 d: M+ \4 D6 l
5 O# |2 ~6 V1 I a/* F, G, H and I are basic MD5 functions.
*/
7 \* I' e; G0 a" f% x #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) r! ^, w+ q: d7 V& G9 q #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% V' |* Y" v b5 l5 \#define H(x, y, z) ((x) ^ (y) ^ (z))
5 I3 d. j% \- [6 f) y #define I(x, y, z) ((y) ^ ((x) | (~z)))
( Q! _" M+ G7 _) s6 M
( A! A: D4 i0 P* ~, [ /* ROTATE_LEFT rotates x left n bits.
*/
$ F6 ~! Y/ M+ I! j: Z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
4 D8 C8 U% [3 w/ O
$ M2 H9 V, l: x# B3 B /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
2 r3 D1 g Z6 Z) eRotation is separate from addition to prevent recomputation.
*/
7 v y: |; Z9 x x! O9 j/ ]/ L #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ `4 ^ K+ M; 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); \
}
( @9 h: p3 s1 b #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 ^1 A+ E6 N4 K# ]/ `7 } #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 ?9 _( I# q) o" h! |* x; U
; g2 J- }; K2 z /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 K7 X* `8 i+ O J void MD5Init (context)
0 U* ]6 `2 f. f5 A& U1 G* l MD5_CTX *context; /* context */
- F) J4 O2 z! N {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
; S5 m; w/ `0 ? */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ h6 p. Z2 P) U }
9 S; n" h; m4 g6 P# m! D
; _$ T" s! _2 Q" m7 J q. ] /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; q' ?6 K3 Q& s- P! b; ] void MD5Update (context, input, inputLen)
+ e( Y8 L4 A5 nMD5_CTX *context; /* context */
4 U! n& J. w7 J- s3 { unsigned char *input; /* input block */
4 Z' d' l' X& F$ H2 gunsigned int inputLen; /* length of input block */
6 e0 p: ]$ `) E! n* |- d. q {
unsigned int i, index, partLen;
' b. W6 y: o6 `5 s P, C, i+ f) u6 E
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 E" h Q* |7 N! r0 h9 [* M
/* 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++程序


# w# s* I7 m& R5 g# F/ e
, a" l# i- v- R6 _
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-19 06:33 , Processed in 0.056260 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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