中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' M0 m* m4 E3 y: ~+ m9 e6 | #include "md5.h"
/ Q. h8 x8 G, k3 X2 ?0 A. G4 b9 N! g
! J2 k0 ?1 D4 N; k! m" M, v/* Constants for MD5Transform routine.
*/
8 v/ B% [& y9 ?8 D9 S
$ B( v; W7 P _0 j( K) Z
2 n7 \4 |( q( W; R: g #define S11 7
/ `3 C$ ?: E- w7 Q; L#define S12 12
~ [/ g( |# e @ #define S13 17
0 L' \3 K8 B. B% d2 H #define S14 22
9 J/ Q! u7 R! J5 ~/ h( \ #define S21 5
- G3 l# y* b! {2 `#define S22 9
/ O5 ]2 {0 r" Q* }" ?6 H#define S23 14
' O+ t8 a v2 w2 p$ _+ }6 ]! L#define S24 20
+ F- |4 }* A, S5 l/ F9 f% N! Y #define S31 4
, K3 q8 W D2 e) j #define S32 11
2 t+ C7 Q& [$ C #define S33 16
. r/ R: R- S3 ^( D% I, D#define S34 23
& C) B& G' q$ ^1 N/ I7 } #define S41 6
& X0 b9 `- i$ T( }( x; }) @2 l) R#define S42 10
# U) l* I2 r6 p2 ]0 ~" N! o#define S43 15
# [/ _/ m, L5 G: t# X#define S44 21
% Z9 k3 O. |0 J& l, _4 `
9 [/ U0 K* t- g2 } static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; T, g, J& r" f; V6 Lstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 l: [( U2 D9 d# X/ C, z! j3 a, P; zstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 j! W6 }5 f3 }8 Z$ V5 S2 v) \ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
: a& ~ w2 r7 Y4 y) Xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- ^& ?0 ~- m% |- U( l9 M
6 Z! S- h2 e. T+ o* Fstatic 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
! {* I8 z5 v2 q" U };
! \: u3 W3 i/ a- P" Z
( b4 o3 ^& l! K% v0 _/* F, G, H and I are basic MD5 functions.
*/
& t, Z- K7 q3 j p #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, d r' w G `2 V$ T- X2 h, ]#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 |% `0 ~3 k3 R% G* E2 b #define H(x, y, z) ((x) ^ (y) ^ (z))
1 k% Q+ A7 g. ? w1 Z+ R7 n #define I(x, y, z) ((y) ^ ((x) | (~z)))
0 u, x/ o2 \- n4 G
6 y7 l' U& [* r" Y/* ROTATE_LEFT rotates x left n bits.
*/
2 ]) K0 Z, E% d% V9 D4 y #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' ^* Q$ d! X! ^" `
/ Q& L" a9 i" Q2 T9 w /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3 ]* g" P) G+ g8 v( `* x! T. D Rotation is separate from addition to prevent recomputation.
*/
! P2 D0 ]8 b5 v. g3 s2 A5 z#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 `2 J, `8 V7 r0 U/ a #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: E* Y2 E8 w' h W: e8 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); \
}
a& c6 g: K; J& q#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ W1 p: S% L2 x9 U1 u
" z( r4 B# c( w. k/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. `8 e! G2 B2 A4 b% P4 G% L void MD5Init (context)
. Y$ B1 E" a/ _2 KMD5_CTX *context; /* context */
( e) }, T. W P( s; T% J# E{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ t. o$ P0 n9 X) m( b4 B( y& I */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
|! @+ f$ s& U3 G}
# Q3 H) Z4 @: N* A" @ x
) {0 B1 m8 j. h/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 ?' A J n" E6 S w3 |* W void MD5Update (context, input, inputLen)
( D5 \; p3 X4 z1 W# @4 j) e$ } MD5_CTX *context; /* context */
- B0 ^3 Z$ s& t% N* x( {unsigned char *input; /* input block */
7 X0 D/ q# e; t1 z2 } unsigned int inputLen; /* length of input block */
2 y5 N' c- J* n4 @; O {
unsigned int i, index, partLen;
3 T* |0 D. q2 W% h; W9 z3 j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& l' N/ {3 j9 w- Z6 i- U
/* 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++程序


0 K1 f1 J5 n, b
+ H7 a0 c6 L5 n. W$ w z. s3 P' O
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-21 08:01 , Processed in 0.597425 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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