中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( ~5 d% S8 [- D: Y: U& ], V7 A #include "md5.h"
6 ]4 v- u* K4 K Y- c
( p3 j/ r- C- _ /* Constants for MD5Transform routine.
*/
' T0 z4 V/ ]/ l: J
* n; W9 Y" a4 S+ d, _
) y: J# e- Q5 R6 R/ U% w6 J6 ] q #define S11 7
) X) v% \) K8 r) @ r; v V #define S12 12
3 q- \9 G( C6 N. w; f#define S13 17
( x0 b( n' v: j' v8 `#define S14 22
) ]: R" L! I: D g! j #define S21 5
% N, c H) W( v2 j. i$ R/ q o#define S22 9
; f* u M7 u P% E& R+ n' J #define S23 14
* f( M8 ^# [' {2 @/ t #define S24 20
7 g; d9 B k1 _2 r8 ^. h5 }! ] #define S31 4
4 P, D/ |& J( Y! h3 X) J4 z #define S32 11
& t) e; y5 H% u* ^. ^4 J #define S33 16
. g4 O0 |% L& e( Y$ Y#define S34 23
% F6 T4 E& f2 v4 r4 u #define S41 6
+ ]9 m9 s+ L3 d. r #define S42 10
/ Y- [7 b# ]3 D* H8 E$ T#define S43 15
% m) {. U0 Z# I( i% H! W! I; |8 C#define S44 21
- U; A) U! b# `; e2 R( T
: ^ `7 L9 H% F2 ] static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 j, n; X$ Q4 o& H* J static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- s( T$ Y3 n4 Rstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, e4 v! F4 ?8 C) U# B static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 b* q4 ]9 j$ _8 u static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. K" |9 f/ ?1 i7 q
3 b8 [5 o$ r( R' pstatic 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
0 w/ F# A% Y$ O/ l5 Y: v5 h };
* g0 f9 Z: W) J: {( ]# d
3 V( E$ d( u" O" M /* F, G, H and I are basic MD5 functions.
*/
1 O* J i& i; j% X" [2 ] #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 u! s, s T+ H, s) ^6 i#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 J% y0 Q e1 `9 n3 D #define H(x, y, z) ((x) ^ (y) ^ (z))
; j: Y& F7 n. z" I+ u( k" Y#define I(x, y, z) ((y) ^ ((x) | (~z)))
- i/ A6 b$ X) H5 c! s# i M
$ a+ T: o7 r# R/ B) _) _$ x3 K/* ROTATE_LEFT rotates x left n bits.
*/
: c& }* @" z0 ?6 i1 j+ C #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' I1 e& a$ O0 K& K" a) U+ C
, f0 u/ z( n5 ?8 x/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( Q* x# m# P) u% |2 h9 i; K4 i4 `Rotation is separate from addition to prevent recomputation.
*/
& S. K* e# i1 p2 n; 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); \
}
% Z8 R7 f& R( u1 e" a #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 x# F8 r% i. b5 z0 q#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( o& c! i9 m5 C s* B #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- J- W5 ` ]7 X; l" o' V, J
: C9 B6 D% j4 i3 ?( s5 g9 \9 l /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
0 X ^7 J! C6 h# _/ B+ Dvoid MD5Init (context)
+ x7 p+ L; f, H4 V MD5_CTX *context; /* context */
1 S6 [5 i: n. T& o2 N% |; ~3 h{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
- ^+ u7 x0 q. ]$ V1 C7 v: l */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 n$ G9 C5 g# X4 p }
& Z. U2 S. v* I$ M4 F6 N6 h5 ^0 O
; V* ^8 P& u, P7 ~( d: O% o/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' \5 T3 Z, u1 T7 A' `" Qvoid MD5Update (context, input, inputLen)
}; E. P, M' a) [4 t( | MD5_CTX *context; /* context */
, H+ Q9 v* N/ i% K$ U4 Z. L unsigned char *input; /* input block */
5 y/ [1 c, C0 {) [2 v; E unsigned int inputLen; /* length of input block */
/ n2 M! b; k$ {8 s {
unsigned int i, index, partLen;
4 a# ?4 j& z' V$ ^! h
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- o, X, {% ~& x- A) I
/* 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++程序


: h/ [+ g: z9 T) X6 l a
1 i+ z4 H4 v1 r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-2 07:15 , Processed in 0.058643 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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