中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 B8 t5 u; w& o9 k' f" g- T1 S$ U" q" K #include "md5.h"
2 N, t0 y' Z4 a; |' t$ ^. f+ Y
& D) ^- j* i/ ?6 A1 }5 h4 W. U/* Constants for MD5Transform routine.
*/
& X3 K' z% ^/ D0 K! n
: _( K7 `' C8 A D; E& N) E4 `4 B
. n9 _3 p, \4 v& q# T7 j#define S11 7
T$ a- _" G& U% ^/ A* v! P3 ? #define S12 12
! M8 X0 N6 @" ^& I2 X% W) | #define S13 17
/ k+ n- p9 [7 U4 u8 a: s3 [ #define S14 22
& x x, q9 U7 m#define S21 5
! h% E7 L. w- ~9 J #define S22 9
1 _& H2 k+ p9 S& p3 F% |5 I5 h; S' O; g1 ]. u#define S23 14
5 |* m8 ?7 f3 \+ O; @/ V' q #define S24 20
9 G& N: j C0 E# y' j4 w5 Z #define S31 4
* d! F5 q+ ^( a) Z5 X#define S32 11
2 {. m9 d- e, m" C$ Y: ~ #define S33 16
( K9 q5 |, P/ g; _+ ~) c: |/ W* h/ { #define S34 23
/ a+ O0 @+ p, x* s C0 x#define S41 6
/ z) w: c8 H) ^#define S42 10
' m& |9 I+ _7 w, a( h, W #define S43 15
! H" {8 z5 N+ p9 t. K8 x #define S44 21
, _. U% N' ~- w( Y
% i8 A4 H8 \! d static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* z# L" k5 f* _$ y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- C% D' ]% b6 \: S+ ustatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* R% z( \' n& V4 u& Estatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 R' B. O8 X- ]+ ^ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# P9 z1 J9 v8 G
6 u# {- x4 o4 Istatic 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! A% ]# c9 X' @( d };
0 W y* c& @! c' L$ [/ J& S; S/ l. C
" Y# R. g; Y( h* G7 _/* F, G, H and I are basic MD5 functions.
*/
0 M. p) s* [1 ?6 Q2 t. \' v: U #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
: b5 V; g" S( D! T2 V0 z#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% L1 R' n, M7 c4 X$ A% W #define H(x, y, z) ((x) ^ (y) ^ (z))
- m4 t3 R& T# z3 p2 O#define I(x, y, z) ((y) ^ ((x) | (~z)))
2 ^7 u# G5 u3 `
7 I) _- {' A7 H /* ROTATE_LEFT rotates x left n bits.
*/
9 G p/ ]0 R" v2 ]! {" q: n* Z' }5 ]#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 L4 h9 z0 C; ~. D) b
+ J, u" h# I, |/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 ~1 }5 |" h1 l0 o/ P! S' d8 L Rotation is separate from addition to prevent recomputation.
*/
6 G( g0 I# _+ Y #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; c- ]/ D4 p/ J! j#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 S! n% m1 \, ]+ m. [0 {* w#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& X9 J1 Q/ a+ y* h& h: z) b+ x#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 ^8 a# H5 n4 a3 i+ o
. n* z# e d: B/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 @5 M* ?- q4 H& u7 h7 P2 avoid MD5Init (context)
1 `% k2 d) r, {( _3 {0 xMD5_CTX *context; /* context */
% w2 h* \+ x: e+ A' h- N0 e {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 s% e; \3 k* I0 |. S2 X*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
f% I0 e3 U7 N5 g& K5 l; T }
5 h( [, t3 [/ l# l
' {$ o9 w g/ o& y8 y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 s6 y( X, Y" t* s! p% J$ q9 uvoid MD5Update (context, input, inputLen)
6 V7 S" T8 v" Q5 P8 uMD5_CTX *context; /* context */
N( m2 Y% {4 j! S' c: y unsigned char *input; /* input block */
' `8 k, L, Q4 \9 z unsigned int inputLen; /* length of input block */
8 t3 d: Y6 u& C6 r% d{
unsigned int i, index, partLen;
! ]3 ]" F5 r5 d8 R: ?
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" @, w2 L" K! 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++程序


3 ~' n, q1 h$ v5 x6 b
7 Z( y6 ^: a" L r: i+ Y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-18 03:41 , Processed in 0.065121 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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