中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 D* S' p1 L i' c0 A. ~#include "md5.h"
* ]* t6 Z7 d ~7 D
7 T$ ~! I2 n' j! g. C1 M7 ~6 D2 M/* Constants for MD5Transform routine.
*/
! N5 S: ~' _: N
3 L1 `* m2 I+ f* t" i
/ Z" W2 N% P6 [# K/ Z: `0 S#define S11 7
' p# S& h+ p9 l2 s* Z0 M$ P# K #define S12 12
; q% @1 R9 X8 U, s#define S13 17
$ a# z, ]# m* w$ Q- D$ J2 n#define S14 22
% T0 a' @: ]; T. I& w #define S21 5
# F- q: T- U- w# V: M/ e #define S22 9
4 B1 f# p! e+ q( G #define S23 14
! n4 v- d( S' t5 ^( @& ^ #define S24 20
/ T3 d4 }# ^7 o" y: T; s2 t1 \) G#define S31 4
; M) a, Y, [ h/ p3 b- Q #define S32 11
6 e$ F* E( ^0 [6 t. c& j3 U* ]0 | #define S33 16
2 e. m$ m4 M9 \- Y' k! H#define S34 23
3 I( G$ b6 W% W#define S41 6
& w9 Z f% Q( W% H$ _9 t4 F#define S42 10
" s& \( K% U: D9 h9 Z2 ^$ m7 F#define S43 15
6 Y8 o- H% O0 B' I#define S44 21
' c/ t# [7 D0 ?: X$ X$ o
8 L9 T% j6 {: B% c+ {* } static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 H( m. \8 Z8 x) |& qstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 ]& e3 `. G7 A e! Tstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 \8 i& A0 f7 |! b/ U% d- z1 nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' e7 P( ]2 R, {- Y static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 }+ Y! b" I! v0 N `) ?
! j2 g {5 m' O. z- Bstatic 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) `2 B: h0 x" g5 q5 L };
' A! U* \7 Y) p3 d
7 p i% J- Z) D6 m5 V K- q4 D/* F, G, H and I are basic MD5 functions.
*/
8 l) c7 b' h. C& T#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ j. e$ n$ u) ~: [, J: r( J#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 w5 S6 F9 w" e- d) P$ a( n#define H(x, y, z) ((x) ^ (y) ^ (z))
? \" f# b$ i- Y$ P #define I(x, y, z) ((y) ^ ((x) | (~z)))
& b9 O% z$ v6 K2 I
9 m/ z. u5 t' Z /* ROTATE_LEFT rotates x left n bits.
*/
4 m2 c& C% ?: u. T, Q#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' b) d5 ~, ~. T [; `9 M
; h7 ~9 m. }7 n: d5 u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ H8 k2 M: b; ^9 {: V. B Rotation is separate from addition to prevent recomputation.
*/
. Q5 j1 {3 M* T" f' c7 A5 N #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* \5 m% o. A6 |; V1 h: V9 G#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' r5 `# x" h* T. t3 m& @7 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); \
}
4 y! W# Z. c7 z/ c& I& W' 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); \
}
3 U* g: i0 n X
+ J4 y: f3 S) q! l/ I /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& J. o$ }9 L4 b) ]( ]) U2 I4 Z1 `void MD5Init (context)
1 ?$ s4 z5 @: m3 C! L1 NMD5_CTX *context; /* context */
3 K; h1 c) n$ L3 i9 k {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ q5 k4 F: W' n& K8 w*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# v9 a* e8 ^* f U9 `# @}
7 `/ P. v/ u x: _& g8 ?' l2 x
6 C2 ?9 [" @4 c' N /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ J9 S$ ]' Q8 {8 h. ?) dvoid MD5Update (context, input, inputLen)
G+ f+ V8 D" _ z5 y MD5_CTX *context; /* context */
?! k d7 j% G, K, F5 x' f5 uunsigned char *input; /* input block */
s8 p) c( L8 ]2 X8 P; G2 A% B, kunsigned int inputLen; /* length of input block */
7 Y1 z3 ^: f, O2 a; |{
unsigned int i, index, partLen;
' C9 f( s1 H4 @3 X6 T
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 F+ K" b. s; W0 a9 V
/* 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++程序


6 O4 `; c8 C/ d$ I
: p$ Q! \2 g X/ L$ n6 E" M6 P
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-19 14:09 , Processed in 0.060591 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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