中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% n2 H; {! u* G #include "md5.h"
3 w1 I6 V: \ x5 ?, }! R
5 e: y9 E: M' V# q9 Q J/* Constants for MD5Transform routine.
*/
% l3 }( w+ H% B9 D$ }" v7 h
, e6 v' a$ C" F& o# x: R4 u
7 ?6 ^: ^; _& Q2 p) p #define S11 7
6 b& r* b, j5 u#define S12 12
' E m; Z; G! T5 h9 c7 N7 v3 ^* E #define S13 17
/ i! I5 R+ s w* c2 l! L; E #define S14 22
! H+ }9 ]2 d3 Q #define S21 5
; N$ ]7 | x; I#define S22 9
3 ?7 |" p; i' e+ {" j( Z#define S23 14
: ^/ q3 p3 S6 e+ ~4 o( g* f#define S24 20
- ~1 m& d7 i. u) I #define S31 4
* n& W6 X! R. |: ]+ w #define S32 11
- l# P' b% r$ l& B+ I#define S33 16
7 L, ]" E; T$ X1 }, [ ?8 d! ]! V# I$ q#define S34 23
" a' W" K6 O: `" d5 R$ k* y! }5 [7 P #define S41 6
# }) |' g! e$ R #define S42 10
. M+ O9 U' _2 {$ ~# |6 C: @ #define S43 15
% G& Y2 k$ x/ | d1 r#define S44 21
! g+ N% H6 V7 Q' x- e9 X* ^* r
8 a* J6 h1 I& h' K static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
& i3 \7 A9 S% _ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, A$ E5 w2 j; @9 f/ b9 \5 k: }# k" t static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, F( t' O9 G+ k static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% b' h0 {! T9 H" p; {7 x static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 @- a# r$ P: T* _, S* T
5 ~& A) x! r3 w9 J9 ?5 y3 J' Estatic 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
9 j7 @9 t$ q8 }' L* S};
1 b% L1 y! `4 L; G$ B# L" D
" M7 ^" R S% s7 L /* F, G, H and I are basic MD5 functions.
*/
3 a0 }. c9 L/ g' b1 i* U" N9 w8 f#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 H- @' J6 [) d* }3 A! X9 _#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" Q |, H0 b6 b3 k w#define H(x, y, z) ((x) ^ (y) ^ (z))
( O# p$ U3 ^: |* t _$ S! I #define I(x, y, z) ((y) ^ ((x) | (~z)))
& \; ?: E6 j1 C0 V! K d, j/ L
$ U3 ~3 ^- k2 k. e q- u9 z2 \ /* ROTATE_LEFT rotates x left n bits.
*/
6 b; U! `) H) z* _#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" L. r" p0 d+ [, B4 f; V2 p
, s! l5 L1 F) [. t* L1 E$ M /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 R6 j# O6 W6 m6 |) C- A) L* C4 PRotation is separate from addition to prevent recomputation.
*/
! I0 ?* _8 D$ R0 H% G9 z #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 u' m- U* K# ^' C#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* i" Q$ ?# 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); \
}
4 ?% o4 v: C8 s; p" s#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% w, q @6 H( T0 p/ n7 v2 q8 P
/ }1 S& v/ S+ s /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 {3 i3 Y2 ]7 I) S$ H1 H& p0 Zvoid MD5Init (context)
" ^1 L) H8 q+ t( {& f) D MD5_CTX *context; /* context */
% A! z3 R# R+ `+ F0 I/ i( s{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
- d/ C8 X* W+ z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 Q! g g# N m8 D }
: N5 @+ n; [- n, w! h# R# Z1 l
% B) S" o" X3 z! O /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 \( C: a: r* r6 f9 `$ Y void MD5Update (context, input, inputLen)
3 J6 Y( K) u* B MD5_CTX *context; /* context */
$ d6 \# `. l0 k7 B, T, { unsigned char *input; /* input block */
& K3 `6 i2 u9 { Z unsigned int inputLen; /* length of input block */
/ {+ y% N6 @6 @$ R4 g{
unsigned int i, index, partLen;
, c2 u) ~! F/ b: O
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 }& z' _; g. ~& ]
/* 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++程序


& `( O; u1 k; u5 K1 g
6 M1 Q% p* ]) N! g/ K
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-5 22:04 , Processed in 0.079697 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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