中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% J4 V6 X: q% o4 E# R #include "md5.h"
1 Y; k* D/ E9 U: V; |7 ]3 u, [1 n
8 b s: o' K7 S9 l( ]6 u5 e /* Constants for MD5Transform routine.
*/
/ P2 _; |3 p1 ~
7 N) P F# n5 j& ^2 M' F
4 s5 } C# ~4 n0 f #define S11 7
9 M+ p4 n% c1 K/ B9 s$ ~#define S12 12
- W1 @5 u% e: L ~4 m#define S13 17
: j9 o! i- a) g( z# u0 P #define S14 22
# U0 ?, O- y6 g2 x#define S21 5
% ]5 Q! ^0 p, [ W$ L#define S22 9
8 [7 n! ~6 M7 x0 o- z#define S23 14
7 V. }9 B& g. F8 ]#define S24 20
7 C! A- e; E% H" {7 b+ f1 j2 w#define S31 4
, j) A, w, b; J8 Y# ]5 |1 U9 x #define S32 11
* y$ O& W( x3 x; Z#define S33 16
" q, `; H# |$ o y- M& ^#define S34 23
6 Z" C6 O9 O' r7 s- C! s; T& V4 g#define S41 6
! R) K2 s) X0 E3 i7 h% o# }#define S42 10
! [) ?6 j* Q" t1 A) `4 j# g #define S43 15
' z% z3 @/ U" i/ l$ |/ k* a #define S44 21
& x3 X O8 o2 i6 e' m& J' D8 p. W
# t5 |9 P i' ]( J% H1 v1 Istatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: \* a3 c. H7 K. b2 q0 z N static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# M4 V5 W% a2 j8 S9 `static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 {6 _" K8 o" Q* S' O. ~& _8 lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, A/ c, O# B i7 v S static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 T4 Z% a7 X- s' N( o* Z( p
2 {: r7 n1 I v, }) ustatic 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
. o* s4 a! B( f1 \ };
; S8 I, b2 |: p+ [3 G7 @
4 T+ \0 d7 a, |" V& H/* F, G, H and I are basic MD5 functions.
*/
* J) k( X& Y- _3 m. { W#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* i3 H7 A+ E7 a) [" B( `" p #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 w2 Y" k9 u( c+ U# X0 a T# w6 I#define H(x, y, z) ((x) ^ (y) ^ (z))
- {3 r7 K5 l4 c' s6 e+ a #define I(x, y, z) ((y) ^ ((x) | (~z)))
* f* G: b# j7 r5 X& B* d
s# v. E, g+ T8 S5 u/* ROTATE_LEFT rotates x left n bits.
*/
; W& q& X) s. r1 ~) I; r#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 ]% E6 C( ^/ W4 \" ^
6 j9 K* u0 n7 B- o /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; A b& {1 j$ t: C+ m Rotation is separate from addition to prevent recomputation.
*/
2 k" W3 `4 H& L5 M. M2 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); \
}
- w$ b4 n' [ P8 D#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
S) `' I( E9 |/ ~* ]2 ]+ G #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 R7 j; m9 a1 J3 U, { #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. z# ?4 i% T1 {
. @- t5 D6 ]; e+ U /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 v- ?( u$ Y$ G- X( }2 N void MD5Init (context)
& R4 u& @; r2 [% ~ MD5_CTX *context; /* context */
9 H5 o' W2 d7 K- G2 R& _{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" S% N8 I4 {% K" T" j9 u4 X */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 Y* N" k9 j# f, X6 G }
' w! K% ]; R1 Q% L8 f U" T6 S
! H/ Z9 P- Y1 l( @; P8 ~ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" V4 f9 p; H3 o6 D6 }% Xvoid MD5Update (context, input, inputLen)
6 g; c6 p' v: S MD5_CTX *context; /* context */
/ B w9 K& ]' _. ]; Xunsigned char *input; /* input block */
6 i3 A6 |4 n. H unsigned int inputLen; /* length of input block */
. f) p% ^" b+ p' a8 `' m{
unsigned int i, index, partLen;
1 r! U6 s9 M6 h9 ?- \# w# Y& D
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% }+ l# d; B) |6 ^. r. {7 ]
/* 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 f5 q8 N! M2 d; h I
5 c/ f- p& l+ j6 ~4 R% Y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-28 20:54 , Processed in 0.057770 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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