中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, u( V! |* E2 i3 Z0 g% e! k#include "md5.h"
9 o5 e: B( w6 n4 b. C
4 k& T. O7 x8 S /* Constants for MD5Transform routine.
*/
2 n8 e) J3 N1 o. I! u
' Z8 J8 t& S" H5 A0 R" b$ x2 ?
$ |( q4 O0 E$ E# t% x* w #define S11 7
9 D) R+ X/ U! s( V, G #define S12 12
1 `! m- m7 I% N& h8 O/ l #define S13 17
! y7 x2 t' i) ?6 R# M#define S14 22
' `& A5 J' i# N7 ?- P2 D#define S21 5
4 v1 m3 W$ v8 ^- j8 C# p) {6 ] #define S22 9
- h- Z6 Z( S; N% V #define S23 14
! m* K8 b6 W2 a* F8 M [8 W#define S24 20
6 J& M" ]: o3 e. p* @9 B#define S31 4
8 F5 ]8 h) \1 b4 X#define S32 11
- K" c, u4 w$ o6 j# x #define S33 16
- Z, V- @4 c B7 j" T( W#define S34 23
2 Q# Q! L& J' P1 D #define S41 6
; |: |3 \ y" C6 H#define S42 10
: [) {( @* }. f #define S43 15
) e0 }9 f9 x6 f: G0 @#define S44 21
9 E, V( y* g7 f: z0 Q. ~7 \
4 Q; v, R+ U( C6 Z static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ S* y) q b% o# j" N9 r7 `7 a, A* Mstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
6 ^- s8 k7 a3 ?& F0 Y1 H1 m, W static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- \, H7 w% A' y* Ostatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 x2 a$ D3 {: d# fstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% L9 y; A; \' \4 D" _- W& X# c
1 R/ E5 [0 s, o% ^+ J3 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
- c# [ Q3 R- p1 e K$ l};
+ M6 c, r* {$ t9 v$ X
" Z+ O" w/ \$ b; M4 N; b6 t/* F, G, H and I are basic MD5 functions.
*/
3 t& ?; ~- z4 b( }! w #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 J( r* W% n; v) D0 I' v/ R9 j% m #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& i! g2 K, Y7 L! E #define H(x, y, z) ((x) ^ (y) ^ (z))
8 T; ]( g& M, V! ~0 S2 D5 X Y#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 D2 H. A/ a; ^) p" K' j; [
7 G& z3 y& i3 X0 q" { /* ROTATE_LEFT rotates x left n bits.
*/
c/ n* O7 F& [) n$ e. { #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
R4 ^/ M0 p4 _! k
, D" f+ \) `7 V. @! @' ^7 j /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. g7 m. ]. @& s. Q- L9 ^9 D: }( m Rotation is separate from addition to prevent recomputation.
*/
+ {1 d3 Q$ q, U4 n: q #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ w5 A5 n n/ P5 o$ T2 T4 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); \
}
. a) \# l" X7 M0 w3 n #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* M' U4 G$ v# [) ?3 g, z #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 ?3 h- e/ F- _8 v' h# c
- K, _) i6 h; Q6 g7 A8 o! ?/ x0 H/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" s; |7 D% ?4 q0 U void MD5Init (context)
2 e& T' U- L; @: X8 t1 eMD5_CTX *context; /* context */
0 a2 ~4 {4 Z" R {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 C7 \" A8 j! ]9 E */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) r9 }2 G J h6 O7 F. s9 Y}
3 g% K; R( q% D# y; c
, ^; M f' P1 N- M! T# V, d3 ~/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 m f) E( b5 e- Q! g# G' T. ivoid MD5Update (context, input, inputLen)
# e' Y: Q& B+ N. O1 W MD5_CTX *context; /* context */
4 {8 e8 U- I7 U) Z6 l unsigned char *input; /* input block */
8 G0 u$ _' ~- q; i8 X unsigned int inputLen; /* length of input block */
/ n2 d2 }6 h+ G* n- @. O1 t3 Q0 t {
unsigned int i, index, partLen;
' j6 M1 `1 f9 ?! ^% D
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- J# B8 q; B% H/ K; y
/* 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++程序


, F8 v. I9 q' ? l$ o
) [+ ]" y2 S/ u
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-5 00:58 , Processed in 0.074595 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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