中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. z7 ?: M. B7 Y5 b( @4 i#include "md5.h"
1 L! z7 I: p% ~
: Y( P6 g! j$ v8 S /* Constants for MD5Transform routine.
*/
9 a8 T: ^! w/ Q' a$ \8 N0 {
! X" E6 |4 u W M. p3 y
, b7 Y7 K8 @) h. |+ n+ v#define S11 7
1 m2 \" @( B, _; e+ M+ l! o7 Z#define S12 12
. Q1 k( t T- N; N$ [# C; L' J. f #define S13 17
+ y4 x; m& H8 p) l$ }# s* d1 c#define S14 22
/ M# w9 P8 \# K6 j1 e' k4 u" W" S #define S21 5
/ c% x5 i8 @; b. `; ]3 U #define S22 9
% {" L* L1 S) k#define S23 14
, Y) B* h4 E& B' b #define S24 20
! X. {7 {: c+ `! u! ]#define S31 4
8 e* M3 D9 i9 p+ J2 y4 r #define S32 11
3 |0 ^! O" V9 @' B#define S33 16
/ T7 l2 [0 g9 }- f+ S9 T4 o#define S34 23
" ~% B4 Z+ w) S3 w i #define S41 6
) T# x( `' ?+ [( l" k; R#define S42 10
0 J9 ?/ }+ r! Y #define S43 15
+ {3 i; T& R1 @#define S44 21
% X$ a- Z {' \5 c7 n
: Y `4 S( w% P' Q5 w7 L) [4 T2 |7 h W static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
( U- V* P" Y; \( j9 Ystatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, x9 T; o8 {) Qstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- i0 I3 G+ F# k, e static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; u7 ]$ }; ~$ u6 wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
; C7 `7 e, n2 z
! f* j( S- @0 N- [6 w 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
! J- q+ H8 l: o3 O& P; w5 d0 ]};
0 w( \4 c9 @8 [. x% v- Y) q, ~
; [; ^& W" |: x3 w1 C1 y N: ^ /* F, G, H and I are basic MD5 functions.
*/
# [2 N$ u, {9 A# x ~) p/ H+ K#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 D5 x$ l/ u' O, u #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
8 E( T* E4 a& T$ @" b! E#define H(x, y, z) ((x) ^ (y) ^ (z))
! G7 I! U1 h/ Z0 s# h& R7 B #define I(x, y, z) ((y) ^ ((x) | (~z)))
( p7 Z1 V, W3 ]- J5 x, h. z, u
5 C' E$ K( u- k/ o/* ROTATE_LEFT rotates x left n bits.
*/
4 J1 {) e! |0 f1 K+ L S- ] #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
0 E2 @ P: H6 F) ~; a/ B/ A4 @
) ~5 u2 m! L* c1 j; i# s /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% r0 {% e0 W* Y# |; r; U$ G# _ Rotation is separate from addition to prevent recomputation.
*/
( d+ J% g5 E' c: i" h#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: P7 T' [. T7 f$ Y% f0 o# d# |#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) c/ {+ M6 i' a" u# W* b#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 a; M" U4 Y b$ L) c #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& D- l. j* x5 {' y8 g
* X% Z8 Q" N+ q0 } /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 ?* R7 q3 x8 N/ k# Q void MD5Init (context)
: u- z2 ]4 J8 t( MMD5_CTX *context; /* context */
( c4 U' d8 t. i9 n* a3 i0 ]( E {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, ?5 g# w3 M* O! c( Z5 Q*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 G2 W. c8 S5 R7 w}
5 G9 R+ K& u. x' K
3 j* m) _* q% X: q6 K7 N9 U& w/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 [, Y2 n5 d d6 D+ A2 M/ Q; jvoid MD5Update (context, input, inputLen)
9 U8 k5 K' u c! Y' sMD5_CTX *context; /* context */
2 W0 R m) V* I( f- Qunsigned char *input; /* input block */
, O# o1 R$ b% G- Lunsigned int inputLen; /* length of input block */
3 [* @- }4 O' h: o {
unsigned int i, index, partLen;
w1 H: l9 Y9 T ?% D+ [
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! n, Z' r; \8 d; R6 C. Z
/* 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++程序


9 B& N1 q. j" @7 w' |
$ S8 M) o* s9 q; s
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-22 15:13 , Processed in 0.059740 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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