中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
T, Y! s, B6 v: @8 m #include "md5.h"
! o: n& k" v; Y( Z
7 C/ ^: z S4 ^, i6 m3 t. V /* Constants for MD5Transform routine.
*/
& C8 I1 b6 x! r+ ]* D' ~, T" y
9 W# N2 s* C( m: G
/ z# w" \6 D% T/ U #define S11 7
: a7 G0 D% d6 w: @! J4 l8 K #define S12 12
3 F' R0 z; p/ F6 n6 |& Z. @9 L #define S13 17
8 X0 r! |9 M0 Z+ X% o#define S14 22
; l0 }) k! d) R: @, A1 W #define S21 5
- I' E/ M- S7 m8 H9 i8 ? #define S22 9
5 I0 L% p- f6 D4 Q#define S23 14
" W0 L3 c8 p) g5 ?/ H# K5 L5 J#define S24 20
0 ^, L7 }" i/ {1 J% n" p* O3 c #define S31 4
$ ^, O, U5 o4 E6 b #define S32 11
: r2 W' ?9 n" y I+ [ #define S33 16
$ L/ r$ U5 F- Y#define S34 23
3 I1 R4 ^" D" |7 G5 y #define S41 6
8 _& B, m7 g: k! F/ J #define S42 10
7 S" ^" R; D8 ?: L: U6 P5 v5 ^#define S43 15
% |7 O, s1 [3 W9 p0 H#define S44 21
/ E, j& P+ a3 s/ H4 m% I
! T `; ? l& l* s) R static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
S" H l7 C+ i1 z) f- rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; L: n) D2 \- C1 S7 a9 j/ F static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. \( K* f" h/ c1 T [8 D8 A1 Bstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 |- N7 ^) E' ystatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+ W6 I! r5 n! ?: w
/ O& o/ |3 ~( @9 w# u$ E 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
) y5 q9 W6 W% R- g! q5 i) b };
( j( v1 }, j- F% {8 L
9 a, \9 F7 ]# G/* F, G, H and I are basic MD5 functions.
*/
6 _6 T% L& D, I1 e# ` #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ z$ {2 K& A+ ~/ M& C3 t#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 D2 K& a; n0 u8 a" }( t% d #define H(x, y, z) ((x) ^ (y) ^ (z))
I2 `4 X. ]4 B* p3 S$ n#define I(x, y, z) ((y) ^ ((x) | (~z)))
U }6 B0 x5 _+ I9 \( O6 `' e4 \/ s
- i) s, {. c% S% G+ v1 U U1 y /* ROTATE_LEFT rotates x left n bits.
*/
% X/ _1 G1 H6 J; P0 \#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& K4 I, `! [/ j1 l
* m1 M2 A$ X) z* {! t6 Z/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. |& X5 Z3 D+ ^2 ?6 t8 V; J# yRotation is separate from addition to prevent recomputation.
*/
! v0 H S& G& C! I" ^1 M! O#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* u. a% J8 Q- N6 [9 E#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ P+ L! }& m: S+ [#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 I' U/ Z" z; Q k5 d7 j" E6 N#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 q9 A: C6 J# h# z; o
- y# ]5 Y6 q. g /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) z3 m& b- b9 E7 Fvoid MD5Init (context)
, M2 U9 `) f& g# T' B* tMD5_CTX *context; /* context */
: i% U# f6 e& C8 w{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) u( X; X/ m! r/ M3 }# W% z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 I& Q+ W3 [7 E; X# Q4 p6 f R}
' N' F) t% u4 T+ n0 u& D
$ Y9 w( Q, i" U( K /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 W }. z- X: i9 e void MD5Update (context, input, inputLen)
- _) `2 Z$ m' |) t C& zMD5_CTX *context; /* context */
/ o( e) J5 r9 H' h$ X unsigned char *input; /* input block */
& d$ \# f& N: V, H# Uunsigned int inputLen; /* length of input block */
5 d+ d. ]- ~3 m# _ {
unsigned int i, index, partLen;
$ ^: y! q- F, h" x) S K- G
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% r! V8 }8 T8 }# |8 g$ `1 w! }
/* 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++程序


4 @% v) @$ P: J3 d
2 x# h! L0 i! k0 ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-22 08:00 , Processed in 0.080953 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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