中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
- @# I: S* Q9 ]* ]* G1 Z#include "md5.h"
1 J2 }, } z; r
- B0 L1 I( J$ u D6 r6 K6 e; m0 b/* Constants for MD5Transform routine.
*/
# l/ e1 d3 D1 J b" ^ D: u
& g) _ g2 Y. D1 b. a; J
5 t( r$ @( H7 ~+ i) p' @ #define S11 7
1 t$ v& J' h* h2 b& S#define S12 12
( H4 o& Q5 F& J, c( ~, n& l #define S13 17
) u" f0 s; @' G, b% L* d# S) V#define S14 22
0 R1 `$ L1 }) P' b; B #define S21 5
! \! ]6 S8 u% m# D( ~( B/ W#define S22 9
: O) V, d0 G' D8 O* K6 `) Y4 O) }#define S23 14
- U7 _0 r- x+ v8 c #define S24 20
; S: n% z: z& r; c2 r# g#define S31 4
1 L( Y1 Y9 v1 @& p% v2 b6 t: ~ #define S32 11
0 T3 ~9 E8 }$ C2 E #define S33 16
& L% @+ K) k5 h #define S34 23
) B% h6 b8 T( x0 K& }1 c1 s% D #define S41 6
% ~) V: Q9 [! u' M #define S42 10
5 ^. Y2 h' J8 Q7 Q/ o) G% s( x7 B3 p #define S43 15
0 N7 R& p# ~8 I+ m0 n#define S44 21
9 v# ^4 j1 T" E, f6 J, Q; u8 h! P
O v/ n( u, ?, N* Ustatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- p6 [. d' t4 ^: @* ^2 q- Cstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- [( f: k" J% j6 w8 e$ C$ G static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# z9 _. |) q3 H5 _) S+ t8 U- Nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% }7 _. O, u) v1 O ^static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- D6 g! h+ Z o( A" K3 ?
" p! u# D. }% e% n* E o; Cstatic 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
" R4 F: S, h& e};
) J' S0 c9 @/ n0 X) g! W, e% L
6 j% N2 B+ S, t7 t$ E% ~. ]) E/* F, G, H and I are basic MD5 functions.
*/
* Y! e% m+ {0 d8 a8 P& u #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' p2 s" m/ }* f5 _) n6 ^4 r( I #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; ~7 n" p! ^& P {: W#define H(x, y, z) ((x) ^ (y) ^ (z))
# M. m- R$ R, ^: _& b) g1 x#define I(x, y, z) ((y) ^ ((x) | (~z)))
% p1 K n% L$ F! k7 U" l2 S; I
, d1 t. V$ P. Q: T, a5 h1 ^ f( S /* ROTATE_LEFT rotates x left n bits.
*/
( O# N) A3 m+ {1 f( {#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" m7 u; O6 F& G" H/ G% G
1 w$ h' Z+ D% z( z1 R/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 Z' B0 a9 h& ]- w: v# O+ @Rotation is separate from addition to prevent recomputation.
*/
7 n6 f. Y0 ]8 X5 J5 o #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& Q( r$ X( k: 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); \
}
4 @( Y! I7 c* D% 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); \
}
( X3 C6 C) m) P% I# p6 t+ ?#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 Z- J X; m! F/ R1 H& i
) J2 w" y. W% g m2 r6 k/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' }1 d6 P" ^! P! s! bvoid MD5Init (context)
" t, d( ^- ~/ Z5 S MD5_CTX *context; /* context */
/ t4 w. L3 O) A' b{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
7 L" R& K2 q$ }( ^& \; a*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- C( p1 R2 m, ?1 G. Q1 E0 i}
: _* l9 u' y c" S4 i" @
& s- s1 o" @7 Y8 M/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* x8 e) w& f6 T. zvoid MD5Update (context, input, inputLen)
8 q; P: Z9 `6 y* w8 k- vMD5_CTX *context; /* context */
+ J) W) l+ T6 P! ~- punsigned char *input; /* input block */
# ]" A& m* j- Y unsigned int inputLen; /* length of input block */
/ _5 n& ]4 p+ X5 c3 D6 U' u$ |2 Y{
unsigned int i, index, partLen;
9 N6 u- \% c( @5 {
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ i: `! K# a; U& ?) t% j
/* 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++程序


' l$ T' t; E' _- r# u
- E7 T, W1 C7 g
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-17 01:28 , Processed in 0.102131 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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