中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 e1 }& p/ C1 n+ y#include "md5.h"
3 l; L( `* H7 b. F4 M8 f4 n8 v
7 R/ y! f) r+ U* p/* Constants for MD5Transform routine.
*/
: x7 q& A2 \) t& H
7 G, ^) x0 L8 C( u. V$ V9 c
( O5 r, i d# l1 T! g8 ] #define S11 7
6 k5 {2 h+ |+ f7 y#define S12 12
& C$ c: N. R% u3 ]! s9 ]#define S13 17
% b# ]4 ^4 k8 M7 a #define S14 22
) D' \9 O; z# M- z7 U* }& [9 R! O. h #define S21 5
, {- A" q8 |* D) c5 D5 v7 V _. r: O #define S22 9
$ n) |7 ^2 ]) O& j M$ c #define S23 14
, u9 V. G2 x% f% {5 G0 Z#define S24 20
) U0 Y$ m% N% \/ ~3 l) w" v, A#define S31 4
' \1 B, `% v* {- G #define S32 11
( U: T, C4 P O #define S33 16
" ?7 j4 ]7 E3 d7 m# A#define S34 23
" y) \. ~3 F# F' P% ~8 O#define S41 6
8 `! c& K5 V% F6 s4 ]5 q% j' e#define S42 10
8 [4 ?0 x* O6 Z" ^& m8 R* h #define S43 15
6 j& f& W: r+ X$ R1 l2 S: }" x6 @#define S44 21
5 z9 A# o6 r4 k2 k5 Z, I
" W' X& ^2 S: R5 v, sstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 V P! P; i6 v( u* Ystatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; w0 w% j( v* L- `) i8 `( V( T static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* ^+ w: C6 s2 f7 @6 m' Q: Hstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, W1 p+ P6 ]$ h; ~/ \- m: p& Q9 c static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& Z2 [! q1 M# P* |
( S7 l/ L- f8 F! G! i7 U& Q: Vstatic 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
# x; M- ` u+ @% k! u, m };
( _% X. g/ w# o+ Z- F) |6 F
; ?2 C6 G) }4 f2 i) X* i" ] /* F, G, H and I are basic MD5 functions.
*/
" A% Q% m! @. s& J3 ~) ~#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 }' L+ m! s5 \- v9 K h#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ Y. f! G/ _% W#define H(x, y, z) ((x) ^ (y) ^ (z))
. B9 @" J8 I- P#define I(x, y, z) ((y) ^ ((x) | (~z)))
; W/ t5 g* o* Q2 ^: R7 Y% f$ [) u
, y4 w( M- B0 L8 y- b6 W0 U /* ROTATE_LEFT rotates x left n bits.
*/
6 g- F9 m) y2 m% b7 S% ?' F* Q; @( r#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
9 s9 J! p/ w, }0 {
. {0 H- r( Y" W3 X7 S2 w5 X8 i& c/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* \) O8 t' I- P# iRotation is separate from addition to prevent recomputation.
*/
' n# l* b& U2 B0 X6 h7 q3 H: Q#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# U* Y/ v5 a8 l! U#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 L+ K: q5 l' `; \0 E% s4 Q; ]) u #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. O9 ]7 C$ k0 X4 H |" D #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ p0 ? h1 ^+ W0 g; P; z4 h+ M
$ y$ S. e$ w& s- J+ J- h /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ g, q, n9 D) b1 o0 B void MD5Init (context)
- ^8 f& j3 A' n4 t8 p MD5_CTX *context; /* context */
) j- Z9 n- J; ^4 n# Z" G{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! O0 G2 R! S7 Z" B. u*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) c* Z* j2 c3 e% K}
+ a/ l: V$ U9 X' q* ^% W1 B
3 P' l5 K/ R9 k6 E9 K# L2 k /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 L/ ^3 P D+ q- c7 ~6 L+ Zvoid MD5Update (context, input, inputLen)
$ S. z( p4 H" i: `8 rMD5_CTX *context; /* context */
+ n& u( A- X8 b: ~" \# W8 ~ unsigned char *input; /* input block */
% N" ~2 L5 y5 a( Q unsigned int inputLen; /* length of input block */
2 G X' T- H9 i3 I& V8 `( J% t{
unsigned int i, index, partLen;
b( `4 l9 {! |' q, s6 P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
. t7 t7 a& }- x
/* 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 O& _ }' ]& I m Y/ T
8 {% o5 H2 O" { c# u
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-27 11:11 , Processed in 0.063482 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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