中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! Z0 e' c- K" L8 l) N+ p#include "md5.h"
# ^) \( x% T: _* Z
, _' R. z( I; h" r# [7 v. o/* Constants for MD5Transform routine.
*/
1 }1 N/ k; ]0 s& k
; W3 e3 c j/ ]" I- |2 U6 r
% c8 l/ [6 J( a) S9 t; S+ p5 ~4 R- e7 b #define S11 7
( ?: t0 a: {2 H8 ^5 d1 ^* g2 l#define S12 12
5 V! m' X$ X& ~" w! ~7 U #define S13 17
# O' t) t; ]; ]' U#define S14 22
- [7 H( L3 n$ p0 y2 V: ] #define S21 5
0 P2 G( }5 [6 `$ _; H2 ^5 { #define S22 9
3 B$ ~2 _* [; r: H( P1 _ #define S23 14
: T9 x9 ~' @. u#define S24 20
; D, W& j- ]2 A( |9 X" }8 g#define S31 4
4 G7 r2 a7 g# S* c! U$ ?9 e #define S32 11
5 z9 |$ T* ]5 m9 B7 D8 J; v #define S33 16
3 ?$ Q+ z; |5 o0 \4 [7 ?6 M$ \ #define S34 23
# _/ K1 q8 S7 ^4 H) i, z#define S41 6
y. U& W3 ?$ H% _ #define S42 10
1 ]* U. a2 s+ W #define S43 15
" G' \1 A$ k/ ? #define S44 21
; }; T" s0 [" L/ ~' G1 q S
$ i% S; {8 F" i Y$ `/ Fstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ S$ w4 q; ^! r5 ]0 Rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
9 o. z$ Z6 D2 H' U4 ]6 h) astatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" `2 a; {; ?% j- |7 kstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 H2 ^7 k" ]' N( L0 Lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# [; j- p8 O& j0 \
7 }" C1 D) g$ b# B8 U! Nstatic 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
* V4 \4 F$ K! {. y6 l' o };
) G2 J ?+ ~/ k$ d6 ?
4 H% R; y/ v7 ?# `& I6 Q /* F, G, H and I are basic MD5 functions.
*/
4 T$ M4 x% b- Q: N% D #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 c/ O/ C7 D1 f' {#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' n4 f8 \% ~; D/ B3 n x& F #define H(x, y, z) ((x) ^ (y) ^ (z))
+ |+ ^ U1 Y: p; n6 C #define I(x, y, z) ((y) ^ ((x) | (~z)))
% U/ V2 v3 P! R @' I" b
) \1 O- |6 h6 Z S1 Y /* ROTATE_LEFT rotates x left n bits.
*/
5 g# A5 c8 K; n M7 @' q- Y #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 N# E7 G! u: n, M' N
) S" x3 v- H. y) V& o; Q$ X /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" |/ r. [; C' Z5 j8 zRotation is separate from addition to prevent recomputation.
*/
$ I/ h* H$ }3 D& D9 L8 t#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& M2 N. A( T+ n #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" t. H ?5 k3 G+ v #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! B0 E. H" @2 M- _#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 R9 p& b |/ l$ a5 _3 w
\3 R# f' C, P+ V% O1 C5 h2 z /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 J* b# Q R2 L3 S. v' A void MD5Init (context)
. H" p( @7 W+ m) }3 X' j MD5_CTX *context; /* context */
' M7 `, X) R P% l {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! e( r2 R; F/ F, B7 G9 O: C*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ K, r/ i# b; A5 X5 ?$ x }
& z+ p7 C( d' E! q6 S7 o, n
) b; X/ X" _& t% s& v- s7 V& _7 J/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 [2 ?9 a% `1 S8 G void MD5Update (context, input, inputLen)
, `; ~4 V- i5 U, {* NMD5_CTX *context; /* context */
1 J, { X% i) o* U* V& T2 U unsigned char *input; /* input block */
' L3 O2 X3 P( a unsigned int inputLen; /* length of input block */
) y! j! q1 m+ b# w{
unsigned int i, index, partLen;
7 p! ]( D) z4 K1 Z2 S. L) @
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: t z! u" d I0 R$ P: t, |/ e: \: R
/* 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++程序


' V1 s( l; P7 q
5 q7 k9 ~% J8 d" t& C0 f# B
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-18 07:26 , Processed in 0.090249 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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