中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 k* Z! R" ~6 m: `5 ~ #include "md5.h"
2 _; ?" F3 b! y. X
/ U' m% [* U6 [9 A/* Constants for MD5Transform routine.
*/
0 q9 Q0 `2 t7 d/ y8 b' M- f
+ G$ r+ p) u4 n8 R Y2 ~0 v0 v! M# N7 T
, l' n6 R9 u$ @% m \ #define S11 7
$ Q5 { G5 v$ s1 R L* {9 p #define S12 12
: K: z" A& I+ h3 s) G! R #define S13 17
+ z/ l' |8 M6 O; l. a' v! A% C #define S14 22
0 b6 B1 O" }4 w$ K" n f#define S21 5
) B( a7 w& M4 }8 V8 W#define S22 9
# V& Q5 ~& y" y3 X* n, T#define S23 14
2 I+ W! i7 T& O2 y, z$ G #define S24 20
* y, \6 u0 o- a1 {* f#define S31 4
" S/ X; L7 d5 G, l #define S32 11
- j) F. Z/ f9 G #define S33 16
9 {" |# u5 i4 E#define S34 23
# K% l5 M' V7 v- P$ u #define S41 6
- q, B. l3 T+ g& @; _#define S42 10
7 P9 G( p, N |/ m" V) C#define S43 15
) S/ X4 V% D2 S#define S44 21
$ W2 m& q2 N4 z& I) T' c: B+ l
2 D3 T& P6 }0 n: M5 j3 m4 `6 N+ astatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 }9 m6 D8 z5 F# ?static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# ?: P9 H, o5 w2 A. Q static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 ]( G: n( v, L/ c$ i q+ V static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! [# T Q! @- e& v2 Z8 lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 F* U6 D$ c Q3 h. X! I
0 `7 P2 y; M8 y8 Z2 B4 i6 L @5 V 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
) K- o( c7 C% j3 {4 R* j" M" z6 ^- C };
" O- R0 I$ U* J: y! ~# h+ L2 X @
! L& Q7 ^3 N% |& T8 v4 r( a1 X/* F, G, H and I are basic MD5 functions.
*/
( h, X( S, h$ M#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; l1 U) j6 b8 U; \#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ |$ q) E5 \, @3 k. ~! @#define H(x, y, z) ((x) ^ (y) ^ (z))
q9 E, s0 V( \& x #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 M/ F; D% G; b, h
% n! X4 D$ B% e* ?$ b9 ^/* ROTATE_LEFT rotates x left n bits.
*/
0 _+ v% S" `/ k# m' @ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ b9 X `7 }8 o1 l
- A0 M1 j9 e0 B: K; g/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( T+ y0 K# v, {# Q/ D2 \) kRotation is separate from addition to prevent recomputation.
*/
# z: W: F3 [0 B5 x1 J4 w #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( A% q% P2 \, J% }$ Y #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 ~" M" ?) B: U' E" T #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% b* O3 e8 r" w5 v! z$ 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); \
}
, a \2 e& o- k. g y
! u; H2 w7 p' s" n' b5 \/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 L8 S# T$ l6 \( k0 O$ G1 }4 v void MD5Init (context)
8 x) |1 p' ?1 [" B- D MD5_CTX *context; /* context */
9 c- v ?2 R+ Q& [) T{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
3 _4 o+ J3 ?( b2 }5 e1 v*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. b; ]8 p8 a2 t3 r9 z }
- T1 [9 K% N' M: N" T* R2 R' v
0 Z8 z* F2 h. a, E/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 `$ O t- s7 `+ h# g; t- I2 Vvoid MD5Update (context, input, inputLen)
' Y; t$ a( o' R4 c$ J. Q6 ?' N MD5_CTX *context; /* context */
+ A3 ]' `3 a+ J$ H3 T8 m unsigned char *input; /* input block */
+ V7 M+ Z5 K9 D# l unsigned int inputLen; /* length of input block */
) b' v B& `+ | \8 q/ S. M{
unsigned int i, index, partLen;
' d2 @- B( J7 o' C9 {
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 `9 s4 G& R4 Q, I
/* 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++程序


- U- k/ b% X" ^1 q1 g! \) P2 d
/ G- E, J/ y* f' |; f0 W0 `4 H B4 k
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-16 09:30 , Processed in 0.284306 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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