中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' d- f& k8 A4 G2 l #include "md5.h"
t5 k5 W, ~; K5 p9 `" t
0 g2 W0 f8 z# Y( p" [ /* Constants for MD5Transform routine.
*/
0 K0 y! {1 T1 L3 \
- ?0 a+ o2 p4 h' Z/ @! f# @% O- _ C
0 O4 G3 d: ?8 N( [ #define S11 7
' u* M3 k0 h4 a- k. ]1 ~6 M #define S12 12
- ~0 Z1 D+ I- I& G; U#define S13 17
& a! ]) _) X* J, ~/ C4 `& F #define S14 22
+ [; W. Y* A% ~( {# Q- R#define S21 5
/ U) [7 K- s1 i. f1 f #define S22 9
0 Z @* X& ]" I- M- d #define S23 14
; S1 a3 d0 Y5 }% `8 s" W' |2 G4 x #define S24 20
7 E$ I8 ~! F3 j- X$ Q% ` v #define S31 4
) X/ v7 k' ^& _' F/ u/ o: s% y- X9 p #define S32 11
. h, J- I9 O0 k+ w0 U/ g0 S; u #define S33 16
9 j k$ G/ s& C+ \! N% E#define S34 23
) J( ~, L, X" X2 F) K+ _: i$ M% } #define S41 6
3 @6 _- t4 P5 ?# j& U2 o t( P! I#define S42 10
/ k' y( R% ?6 w0 M- _8 J #define S43 15
3 v3 }% j1 L% Q, n. w& g' p" T #define S44 21
0 _+ j" E6 I& |/ N k2 q: I
* V9 t- b2 T: Q' kstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) w$ g& M9 @% I! P7 r static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 K: N4 E: ^4 h b+ k! C static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 l0 b2 o: E+ J( `$ \- P static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 g2 Z7 N- e' I. G( L" L static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 ^! H; s8 d0 P7 e) {. x/ m
( c& u: O3 h* C$ {/ tstatic 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
: L! n9 V- F+ h# k! m6 q$ q/ g( v };
/ w' H* t0 ?5 x1 Y- h' [) W; M5 |
& R" F7 x) y4 ?, _, U1 c /* F, G, H and I are basic MD5 functions.
*/
3 w5 t0 t' {/ ` g2 G#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" y' _6 B4 Z% `: k j+ t \7 H #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) P3 ?1 i+ x$ `, H5 L( B#define H(x, y, z) ((x) ^ (y) ^ (z))
/ q# t2 K/ y7 u, t# \" L5 ?% I( s #define I(x, y, z) ((y) ^ ((x) | (~z)))
. a! j+ I# C% O; U* ~4 W9 J
% D# B: `2 G! K2 |5 ` z/* ROTATE_LEFT rotates x left n bits.
*/
, v5 \( N3 g% B0 Y- o2 Q#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 u8 P( E! k9 q) z2 E6 U2 Q
7 g# n$ X% O8 T1 g /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" m. b! M% e! f4 cRotation is separate from addition to prevent recomputation.
*/
# z; [. ^, V! W$ D) 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); \
}
! x" n4 W- i0 W" E #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 O9 M5 O. w' R I- S- |2 c#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 B' a! B- ~7 o% G #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 i7 q; P" L. y$ r
/ J6 R1 l" ~8 ]' O/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% E$ R( F8 c! e9 h6 H2 M7 B9 dvoid MD5Init (context)
6 F3 e( s5 t0 ]6 JMD5_CTX *context; /* context */
3 {2 N7 M- I/ A8 A6 w" `( } {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
{/ O* ^( b/ @! o- x6 h. c) Y*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ t1 w6 Z7 H& i& I( [ }
0 H1 B x. `5 ^
/ o4 w: d5 A) `6 F* p1 @ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* P0 o, F/ g4 v& g! Nvoid MD5Update (context, input, inputLen)
0 B. j' e, R/ v A6 H2 JMD5_CTX *context; /* context */
, Q+ B5 I0 j9 n& f# L: Z% Funsigned char *input; /* input block */
5 a8 L) c& G3 f* s2 zunsigned int inputLen; /* length of input block */
( D9 _; [% ]5 i2 g+ U{
unsigned int i, index, partLen;
( _& s! s% m: j, a2 \- I
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ l: K0 T* B* U& b8 y. v
/* 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++程序


4 S. e9 b3 Q: H8 a8 K: O4 y: v2 D
G3 n. @. x# u# q) ^ g) f
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-20 17:14 , Processed in 0.060773 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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