中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 {/ L |, V5 j$ n7 z! s#include "md5.h"
" o2 l$ \7 G3 L8 o/ E
6 o2 e7 m7 u/ Q C! m+ n! ?4 \ /* Constants for MD5Transform routine.
*/
6 E+ k6 m7 R3 E% r0 I4 N
& T) m8 q7 D I( M7 N6 z
* I* G$ I+ l9 e- N% n3 G. D#define S11 7
9 }- j+ {/ u0 _% Z: `2 _#define S12 12
5 Y {1 q. ` t W+ G. n #define S13 17
8 W$ B0 S# C& o5 E1 J2 u #define S14 22
/ z& ^7 q& \- e3 o2 z2 A#define S21 5
8 D: ^" p8 c, g( _#define S22 9
3 I- |. d1 H* k( I4 G# e#define S23 14
6 F* G7 g" T& O- T #define S24 20
( [# [9 q5 D: b+ n6 P- x #define S31 4
) `' s+ @, m2 m: |. b #define S32 11
9 c V. `; n x8 o" q #define S33 16
7 z9 U* P, x% ^) F$ C v #define S34 23
: E; a& S- u# {$ D* b#define S41 6
T. x$ n7 I. C1 H2 _; W #define S42 10
: B7 P6 `! h d3 @#define S43 15
; {: r2 h) D* u#define S44 21
& h! ^1 G9 o. r$ T% }' z# ^# L: E
4 c6 Q4 b1 }, \: {! F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 _7 \9 Z6 h# pstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) T5 J+ s- ^+ p static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 `; z; G0 W* ] F- f static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 C. F- P9 p# w( s3 L- F5 p6 A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ m( b' D4 ^* y/ c
+ w# D: D! ~0 {! E0 q: r' M 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 e' K" S; z" A};
$ {0 t& p. A2 E9 l' a
8 m; z5 Y! m3 U+ W' E /* F, G, H and I are basic MD5 functions.
*/
! t( g p- a- J2 T8 d# e#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, F$ v, B9 |# O! C6 F#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
4 q3 b8 t$ n. m9 Z% [2 O #define H(x, y, z) ((x) ^ (y) ^ (z))
3 Q/ O ^. g; z2 `/ H# N #define I(x, y, z) ((y) ^ ((x) | (~z)))
1 k. n* Q) ~7 R
& i2 e+ B4 J& M- [- U+ y( o9 q/* ROTATE_LEFT rotates x left n bits.
*/
8 }/ W; ?0 A( H# y* I #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 b# P2 u/ |! H/ q7 |0 k* @' g# [% w
0 d) ^ C9 O/ D' ^; W/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 k! h7 g2 ?8 i) k/ z9 PRotation is separate from addition to prevent recomputation.
*/
' y# S% O5 z- Y/ j7 R# m) W5 Y #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ ]0 T5 H9 {' N# ~5 @& i; F #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 A3 [" [: U, I* S/ g#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* e$ o1 E* C1 ~! [/ } #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ ~& e4 c& b3 ?4 s: n m. {; n) D
$ _+ H3 d0 U- l /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 K0 V9 @9 _; `( B1 c1 f9 K2 Svoid MD5Init (context)
5 C' h, k& R* UMD5_CTX *context; /* context */
* @- u9 B9 R2 x3 W- u* [{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 s0 p# A4 s- |, w( a& o" S*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 } w- ^" ~" `" i5 z4 P }
5 ?1 r! H- t ~8 O* A: |2 x
& V9 k( {: R' c4 E9 }/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ X5 e3 P; P" D: lvoid MD5Update (context, input, inputLen)
0 m. r4 g0 a- G4 G" |/ u, nMD5_CTX *context; /* context */
1 `% x/ s) G' ]$ u% C9 M! bunsigned char *input; /* input block */
1 \2 a0 a8 r. p3 M' \0 W/ junsigned int inputLen; /* length of input block */
" ?6 D" D- {+ k. A* V{
unsigned int i, index, partLen;
% q- M4 U6 p' g* L( ~! h
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
4 C8 D! N8 M0 \" o1 g
/* 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++程序


) E: R) R/ g6 L; j) Y
, N9 V; ^9 w! V9 H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-28 09:35 , Processed in 0.074763 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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