中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
9 _# A5 R: D3 a #include "md5.h"
2 d6 p$ z l* c; e' b
8 z# f0 ^5 N5 i1 c /* Constants for MD5Transform routine.
*/
* j7 r" m% O: e2 `0 U) h! I
9 P; \: u$ G' e- o2 ~. h
8 o" O" A: c" f#define S11 7
% ~- [, ~5 K: I( z #define S12 12
0 d; Q+ V3 D% f4 [; k$ i+ b#define S13 17
9 E" P' o' z3 }3 d7 L8 }8 k #define S14 22
+ {; } c* o4 {2 g$ t( d1 m, ]; N #define S21 5
7 M) @ Q6 J& I, k* ~#define S22 9
' s( D0 U: k# r& f7 l" t5 S/ V #define S23 14
1 K5 `% p' N1 |5 p: X' m& o#define S24 20
?* H7 ?; o+ O1 O& b#define S31 4
' z! W; S* f4 x4 u5 Y5 x( l8 P#define S32 11
$ N& T0 ]1 v5 k5 ^#define S33 16
# a+ _/ U* y J4 ^& b! C#define S34 23
0 `2 Q7 M/ a) c- k. h7 H8 K #define S41 6
4 \, Y: M; e% D* ~- E5 j) G ^#define S42 10
4 `3 I- w$ H' e& Y7 N#define S43 15
9 Z( m X3 I# z$ G4 N7 Y1 ?0 { #define S44 21
0 ?5 C7 R4 [' c& u/ m, ^7 @/ @
) ^5 U. s! p& V" b9 |static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
# k& [" Z) F1 Y& Sstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; |5 ^( J% o0 s% k3 X' M1 tstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- B9 q0 h! b' ]/ }+ B: `. O3 wstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 N8 p d" s( j' J6 A7 C static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
/ k7 E' k" ]& T$ X0 ?4 b
& D9 z. b; [) Q# ~0 s. wstatic 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
- u! G& g4 z) H1 o) H};
/ r5 `9 X8 W' v7 Y; g
" x+ o2 H+ C: E1 x7 W$ J) B/* F, G, H and I are basic MD5 functions.
*/
4 I2 k, T4 j# w' ]( @4 h#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- A4 k( A6 H. t: l6 N* q: d#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' n- j8 ]! a1 Q( r& @, I#define H(x, y, z) ((x) ^ (y) ^ (z))
. y: N4 ?8 w2 M6 V* T#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 x& X7 b2 q3 K+ _/ Y
% V' v* @0 G9 z- K* ^/* ROTATE_LEFT rotates x left n bits.
*/
9 s% m9 v4 S @* o/ x5 n#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: `9 I i; |/ S( P9 ?. e
3 ?% W: n, ~& p2 V0 b* D4 `" d/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( q; z5 o" a: J j$ f, M- K1 k: j Rotation is separate from addition to prevent recomputation.
*/
, N- K0 F3 s" V#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( S$ r- m0 U1 w* P- P8 W+ C9 r# 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); \
}
# S+ v2 s" Q c: [4 j& @#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 a) J$ R; V) ?1 V( { #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% E) h, {9 j) D, X! _/ e. Q. P% ~
, S% ]( t/ z3 ~) P- n" g# P /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" \ \& f4 E- I( ]4 V- ^ void MD5Init (context)
& F! R; r; I: G' X3 J MD5_CTX *context; /* context */
: t0 T7 i d5 u$ l, A{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
; P* d$ _! N; H */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ m$ H' `! q( @ Q$ @; ?7 x }
/ l, k* i' F/ x( P0 A9 x
3 E# m4 q- G* \/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& s9 K: C, B6 R3 f! zvoid MD5Update (context, input, inputLen)
1 i/ E3 g# U3 G' CMD5_CTX *context; /* context */
5 n7 v0 R% F) M4 w+ S9 w( K# F+ L) Dunsigned char *input; /* input block */
# w" f6 ]! ?% X; A+ b unsigned int inputLen; /* length of input block */
4 j6 i, X9 T4 A R4 q {
unsigned int i, index, partLen;
3 j ^+ Y9 ~+ k9 O
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& g/ K/ U5 b7 W3 n7 K
/* 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 C0 ~, K: o0 T' V
# p' o8 J8 `4 T* z3 m) v9 b0 O
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-23 07:50 , Processed in 0.060185 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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