中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 g% S/ n( [. q! s3 N#include "md5.h"
* ~9 I' J+ Z2 R
" T9 |' H/ U! }1 d5 A. N2 |/* Constants for MD5Transform routine.
*/
7 Q; ]/ R, g/ l- A4 W2 t
1 m3 {- n4 x8 _% Q/ E# ^
( s: l0 K0 z. L. _8 M; z; J #define S11 7
" z+ u- `1 h4 E/ u" r #define S12 12
$ f4 H" G' Z5 N# O I6 a#define S13 17
8 |- y/ r/ ?0 F4 A' | `#define S14 22
. M. C; l: Y7 U4 L" R#define S21 5
- d+ Y+ P7 P7 m R% E* i, z #define S22 9
( R0 D4 ?! X9 A2 G3 S p#define S23 14
T1 Z: {- d0 Q4 r# T* m% t#define S24 20
. \2 @' O) ?: f: D5 l2 k0 z2 n#define S31 4
5 a% O, R2 ?! L- w I3 o! r #define S32 11
$ T& I* \! i- u#define S33 16
1 h% j3 o9 o; N, ?* P. ]6 e6 c #define S34 23
5 T7 e$ U T) L6 B) I- s9 q7 [* W#define S41 6
9 L/ {9 Z+ `, F# u0 T #define S42 10
7 \4 y% k7 J+ M" _# j! A# c#define S43 15
) G7 P a0 ]6 x #define S44 21
! z+ ^! ?/ G9 o% c( w( k
! f$ ?/ A. \" ^& b6 H% F* Z r static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 j/ ]3 B- ?# R6 Y3 @static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) }1 C3 Q6 B2 dstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! T& z, `3 x; V% z( l) b2 n/ T static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& @4 A! T/ `- Q4 l! vstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
0 k; t/ ^! }5 C- W, L! |% P% Y
2 H0 v1 b7 P F O( Z 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
( f) X; F( a8 \ U' U};
4 W# F% e6 _$ M) Y7 k2 t7 @
" q9 [4 f# k" i8 [/* F, G, H and I are basic MD5 functions.
*/
0 L: X2 R- O/ Q* m+ w. h, A7 d#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; @# J/ Y; A2 E#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 D: [+ ]( W) j" N% _) L( X #define H(x, y, z) ((x) ^ (y) ^ (z))
& [$ E8 |- R6 U/ x# x/ ^0 s #define I(x, y, z) ((y) ^ ((x) | (~z)))
% a* j7 h/ Q# W) ]( }( G
8 b+ p4 X1 y7 N7 D# n `" n- p/* ROTATE_LEFT rotates x left n bits.
*/
1 y8 ^! A+ @/ ~- k0 P" G# R( P #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 Y5 W7 n# h9 ?' e& S! B$ o
' h5 E6 l, g [ S5 v /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
, R- L& q/ [1 F. P) Z- u% rRotation is separate from addition to prevent recomputation.
*/
/ b0 f9 H: B. K #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 |8 {; b/ c$ N! d& H" [* i#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. g. c+ y; I; m% u* {/ w#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# f+ v& U% Y/ l- X9 K! S#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 L! u4 \! C5 i' v
) ^ g7 T! q: q /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 U3 k( J9 [( n. I5 R6 e% [void MD5Init (context)
: M. W: [7 Q- k6 T! p1 J MD5_CTX *context; /* context */
% D: u2 K- f' C9 y. ]3 U{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ ?3 p+ U2 U" a5 C7 ~*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* Q& h2 l! T3 ^4 s6 p* [! X5 }3 y; Q}
$ r p9 ?9 E6 }" ?
! r; k& F [& G* M9 @4 i /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( ^/ Y% S- o. c9 {: ~1 p( A0 E# ]# k void MD5Update (context, input, inputLen)
q' \$ _ A. XMD5_CTX *context; /* context */
- H/ Q+ r P4 ~$ eunsigned char *input; /* input block */
3 D5 K2 c) f6 q6 V9 Ounsigned int inputLen; /* length of input block */
9 F% e. k8 ^' X) G{
unsigned int i, index, partLen;
5 w% L3 q% V; _' D C( {, a& \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
. j$ O" A; Y+ `$ W& s7 r/ C
/* 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++程序


9 h$ k0 d7 v: T8 U' I
; E b' T9 y6 |1 w
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-7 15:09 , Processed in 0.056782 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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