中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# d7 r; X. p# T$ M) D( \# h& | #include "md5.h"
5 h7 S _3 I$ i; p( X
! f; g/ G# D8 z7 e /* Constants for MD5Transform routine.
*/
! W* j2 V% m; A3 ~+ j- n* U" K6 T
3 M! m' D8 [# i7 `) R. c
# J: J; P* C. M* F #define S11 7
; o" M ?4 i, j& _- u8 H0 e/ f+ |* ]#define S12 12
" ?1 v, s9 ~' w #define S13 17
0 B, y! z) j) v2 [ u! S4 x #define S14 22
: _" t, @* o: R, V4 D; ^% D #define S21 5
2 A. c* M# m" z) s) |#define S22 9
" n. q, S- `0 A; g5 n# ~4 { #define S23 14
: [9 O- X: m6 R6 z+ b$ C #define S24 20
6 h8 `& c$ R" w, N6 v3 { #define S31 4
, `; y( M8 [) U& x# ` #define S32 11
- Q& d6 O0 Z% c #define S33 16
. l B0 J7 K4 U8 a+ ^9 h9 y #define S34 23
7 ^1 Y0 E* }. F) e% X& ]#define S41 6
8 Y3 p& c! J6 @9 [ Q #define S42 10
3 a. l6 {8 u( I: D; c#define S43 15
9 e: C9 f i5 \: _; h; s #define S44 21
7 c+ ~& N' M' |! u* M
1 m g0 Z. B, l& w9 qstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
o3 Z- `+ l1 Q) @, g& p8 e7 r* g static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# a0 d3 j8 |. E* V static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 x$ N. }& [$ \' ^ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
1 A, g$ Q, q# K& _' x. a+ t2 lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, F. m/ j" l2 X" d6 J
: o" @" U5 u+ v2 a# d! y 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
0 }: N9 k; F. _};
; e V% e0 m( X B
# ^! J7 g0 l: k ^ /* F, G, H and I are basic MD5 functions.
*/
: C' O& |% Q: x* }3 z# G#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
7 d1 z, C6 M6 X#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 k% r1 g; P3 z; v/ |/ H" L4 X#define H(x, y, z) ((x) ^ (y) ^ (z))
s( }# `6 D& D7 n* j0 z/ f9 o #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 K* R. b8 }& W+ s
. z6 ^" w# V/ a0 S' g' k+ D/* ROTATE_LEFT rotates x left n bits.
*/
! b5 z4 g/ n+ T+ b2 z: s: g v0 t R#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
?$ m. O0 d) @ k* n
6 }' F. u( `/ e7 R7 b /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 d" `# ~6 d: Q8 U, ^3 l' b Rotation is separate from addition to prevent recomputation.
*/
2 g: b/ H5 p2 Y3 C #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" G# c1 k8 j( t+ c#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 I: y$ u- P4 U) U" H#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; [. j. `7 z. k# y( E% k' X$ n" 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); \
}
+ M* C4 l: b( Z; o2 S7 f6 L. J
Z2 s6 k& ~- E' g3 r1 U( E /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. L6 D( O8 }7 x; u$ C- Z. P void MD5Init (context)
5 V; R% J8 e. i# AMD5_CTX *context; /* context */
* t9 J. J9 L7 d% e7 B{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" K7 k. I! _2 _0 y& z/ W) O1 l8 C# b */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ H# c: k# l) o, t3 g- W& U }
! o7 }2 R0 x m3 I
G' B& U# F, w& r& I$ x/ Q/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 S( ]' N+ @# i! F" s/ @1 s! Bvoid MD5Update (context, input, inputLen)
' w/ @$ o- ?+ G/ }MD5_CTX *context; /* context */
& y8 p, d. I) S3 q9 m; n unsigned char *input; /* input block */
: e/ j" h4 N0 A8 `9 _6 ^ unsigned int inputLen; /* length of input block */
9 A. m4 l" W8 m' r# o& ?& G \ w{
unsigned int i, index, partLen;
6 G1 x/ p0 z. w/ x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 j( g2 F0 j$ S" K
/* 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++程序


( v/ K2 |) E. t, n
- C" F5 J+ A* I5 v- O
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-13 10:22 , Processed in 0.076961 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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