中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( o. n) C/ k) m' ~ #include "md5.h"
2 J1 g: Z7 [* e+ z4 y! j: l" `1 [
7 x b+ h& N' m/ z3 g% T /* Constants for MD5Transform routine.
*/
( X" E9 e! s2 P0 n* Q
- c: x' y+ Z# J; Y3 M
3 Z' s* j3 V2 r$ N #define S11 7
: A9 U' f) r& T" P! P #define S12 12
5 v0 ]1 [' w8 `- Y. E! _3 n# h#define S13 17
, Y% D2 W ~% K6 z. m& w #define S14 22
' }8 h) e: u1 J6 n5 {#define S21 5
% S( N8 O3 `! E% n& ^' u; g9 H#define S22 9
& Y) W9 {! m. a* f1 O#define S23 14
4 t1 L8 _, T% p7 z$ Y7 V #define S24 20
% v1 J: G5 @) P #define S31 4
9 n! {2 g/ o q; e o% ?* p#define S32 11
/ R/ n# R9 A# }8 V2 n #define S33 16
. D1 s7 m) \- r3 I. a3 v [#define S34 23
) L" C8 x& B% @1 j2 T; k #define S41 6
8 E7 H9 B+ u3 h: m# t/ g# M #define S42 10
2 @! t$ w0 ]* j% n- ], n: o+ c# E #define S43 15
& L+ y( h9 G+ ~4 q #define S44 21
( g( J) q* }5 u. p, w5 M2 z
/ _( Y/ A( X2 L+ X" I8 fstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 I+ u' S( v3 C3 `- x static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
3 R+ P3 h& b( w/ x1 X! t static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* k3 w; m4 k0 u: e @5 Cstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 |& O3 b6 t; y) W static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 A4 K3 z) q3 Y& D% B
" L) l! @# `' E9 ~9 i x S static 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
/ }8 D# k( j k% M( Z3 V# \/ n };
0 s8 P+ f, V0 {0 }) x
h8 a7 T( `: | /* F, G, H and I are basic MD5 functions.
*/
) {. u6 M9 N( T6 G8 ]- b; I#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
5 D: B9 L& ~( Q$ |; ?& i% }$ z#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 R1 F' A: ^/ D3 r2 K#define H(x, y, z) ((x) ^ (y) ^ (z))
' K4 s/ l/ R& h#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 t7 ^% A1 L* ?& A1 h1 Q
; A. D D2 F+ C! l8 ]( @/* ROTATE_LEFT rotates x left n bits.
*/
1 |% @/ `, V, [( ?. \6 x" c#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 U6 V9 s* a- b$ q
! ]5 y3 t7 M$ J% L& Y$ H4 v/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 `% Q& I* k9 u. X8 \2 T0 RRotation is separate from addition to prevent recomputation.
*/
+ j' x0 X0 Y* W) w; ?5 B1 H R, c9 o5 _#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) ]2 \3 @: U; y#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 h q7 C1 d: m. I1 K#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: F3 l4 x' u$ b5 F( 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); \
}
9 Y5 w/ c) O+ M8 R, [$ t# M
/ h8 _, H. |4 E1 A/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
$ Y+ j6 y+ F& @0 h( H6 N% Q; N1 bvoid MD5Init (context)
* _7 n& J. N8 s1 Z: fMD5_CTX *context; /* context */
' u! |* u V2 l! { z4 @0 z' o( ^0 p7 c {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
- L( b; e9 S) [5 V; B*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
0 [- m$ L: ?( G5 A+ p, z# X' R) z% T}
) u6 U& h3 N: c! H
( T3 ?5 h5 e P7 n: x /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& i( s' A! _' c9 y" I4 }8 evoid MD5Update (context, input, inputLen)
& t* L. l8 s- q8 d6 _ MD5_CTX *context; /* context */
6 Y$ m; d, p6 X* p' E! a4 t# g unsigned char *input; /* input block */
( n# i$ B) z! i5 \/ R0 P unsigned int inputLen; /* length of input block */
+ s4 N! ^- B# `{
unsigned int i, index, partLen;
: m3 k; t& D* j# X/ B7 g7 m: ]& h
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ U+ a) g4 d4 \# }. H% ~
/* 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++程序


: n5 T# R( V" n
! H R1 Z, D9 e: v% b
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-26 19:56 , Processed in 0.058564 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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