中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% e& {9 Y2 t6 _* h1 R #include "md5.h"
! y8 S4 B' J/ p% Z% b
; Y% h9 b$ J& ^2 [) _% s( H /* Constants for MD5Transform routine.
*/
2 o0 K" w" s$ I5 _* i$ t1 I/ h1 p
U0 S( v' A$ a' @# q
; ~ H( i) A3 p#define S11 7
) @. i j$ l! X. f; `9 z/ h" {% W) q9 M#define S12 12
1 { S$ Q: G4 U, p! X+ f9 N#define S13 17
! E8 W& u+ T6 v3 R% G #define S14 22
. ^4 n' E& H. @ #define S21 5
* A* k5 s& g+ S& d# g$ |7 U9 a8 Q#define S22 9
; y8 n5 }, D2 r" v4 L #define S23 14
$ N: H @) b8 C6 M. o3 S #define S24 20
6 E' }) U4 m8 D1 H6 ~) f. N' W#define S31 4
" I! k, M0 M* N0 n; n& p" a#define S32 11
7 x- t a; V8 i0 [#define S33 16
- E4 g. A1 [, F; W4 q #define S34 23
/ k4 @. J8 I0 w1 G+ B #define S41 6
3 u4 o$ I$ I% H9 v#define S42 10
3 r/ b/ P- G( p#define S43 15
0 u4 w7 G" b0 w6 S: X' y$ M4 [#define S44 21
0 W4 z( A3 _. X5 x7 B( |5 |
6 l& W# `5 G# _/ r0 j static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 d% Q. J6 F5 n/ ?8 s static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 ]" r0 _6 _3 Q$ H static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& Z0 a" _. I' H3 B* Pstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" m# T( Y- k. {. I9 Lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 l8 ^0 y# }+ ?7 u5 p$ x/ U8 R
! \: Y4 ^+ e& x3 Dstatic 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
4 G, N! v) Z* j6 p: u; d};
) b2 W) ~% ]8 S9 c+ r
2 @# u. b2 m+ W9 G3 k+ k /* F, G, H and I are basic MD5 functions.
*/
+ c* m6 b: `& T& ~8 l% r" y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" V4 b/ p( T% z+ ]1 w5 [#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" Z* d: D- V4 L& D; D. e' q #define H(x, y, z) ((x) ^ (y) ^ (z))
# T( z! o3 \3 F- x#define I(x, y, z) ((y) ^ ((x) | (~z)))
* H6 i# r6 Y: I# G' g1 o: d
3 O9 e) E. `/ i% j& Z /* ROTATE_LEFT rotates x left n bits.
*/
* n( b, _& t2 L2 x #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( J3 ~2 j, u& I
7 n1 q9 O5 _# G9 x& m /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 h8 ` I1 _, S# d1 ?$ M Rotation is separate from addition to prevent recomputation.
*/
; L& F4 a# n" R& Z' M#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' h$ r6 t* W2 O* y4 N* \3 m#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- b, J4 ~ D; Q7 O( s3 H5 Q! o! 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); \
}
1 J. \ i/ v x- f) O #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 E" w I7 v2 x! n$ e- w
9 Z$ }! C- Z7 t; f/ |4 k, |/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# `6 [! W% | r# uvoid MD5Init (context)
0 U' x f4 y& ^+ }6 WMD5_CTX *context; /* context */
7 }( ~- x- T6 O/ }5 }{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" l1 `! L) f: e5 X+ ]. E6 r */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 K" \! Q: }0 \1 c7 A5 n}
- A" G( t: B! h+ T [5 _" F
; R% ^* ^. M' A4 g/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* ~" H; ~& _ y: c: y# a1 I. W7 A: v# Hvoid MD5Update (context, input, inputLen)
! K$ j. Z% W: E+ w* P! L1 B MD5_CTX *context; /* context */
2 H& d; N7 R9 ]! l5 ~ unsigned char *input; /* input block */
2 E Y# j7 S, k2 ~* k: ?unsigned int inputLen; /* length of input block */
+ Z& Q3 L+ R8 n; N {
unsigned int i, index, partLen;
" m5 m) Q, ~, h1 d, p( M
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ j* j, Y K7 |% J
/* 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++程序


, I& Q! J8 N' R7 h9 Q
2 F: T* v: l1 D& g) g4 ?) R
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-9 11:50 , Processed in 0.063806 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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