中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* T0 W2 L- a$ C0 i #include "md5.h"
. n, y$ u; }+ a) _
0 S' @6 i: @- h) \/* Constants for MD5Transform routine.
*/
' ^) M; J* ?, K# x+ V, c
/ d% i+ T ~5 D" H
/ I$ c/ `+ O: \# d$ O* B#define S11 7
2 n9 o4 J% b; V% l#define S12 12
' a) R1 ^. i" @' q; Q" v1 v #define S13 17
; m. a9 N8 V) b7 m1 N( G #define S14 22
. F; P/ V% N0 {0 ~. T, {#define S21 5
" ]; [8 i5 E2 M4 K3 ]#define S22 9
1 ]; k8 m/ u2 I1 |7 g1 P+ c #define S23 14
: G7 ?# `, W( V& l+ a; j7 g #define S24 20
8 P; b8 J+ H2 s+ X" ~#define S31 4
& ?3 g T" F$ X* `#define S32 11
) {3 d) y! t4 I, |: y #define S33 16
! |( Z4 L( O5 g #define S34 23
2 M6 r6 G: \9 ?' j- s" [2 h0 h#define S41 6
' F. R6 x" ^3 d #define S42 10
2 p8 {( k; `8 q" `3 ^#define S43 15
# R) b3 F$ ]: r# z5 u! x #define S44 21
9 z- D8 ~2 F( g9 P% S* a
; R2 c2 X" u& x& L, V6 S& I static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 o' {) b6 b* C6 d static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
9 W* b: m# r: a7 E! F$ t: o static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
g6 l5 m3 j2 `* }2 ^5 Lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& Y- o- E: g8 h6 ^+ a- p% Q static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& M2 I8 u& d; V5 t
e5 I5 e! c+ }3 r" @ 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
& d% j4 E8 z( T" r3 `6 V };
- k1 {' g. |6 o! g
[6 ], W+ E( s0 t; y! y1 G3 e/* F, G, H and I are basic MD5 functions.
*/
, u7 P+ h' c" c) Q' v) G #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ p' Z& t8 A6 _ G6 j #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; [5 S7 C5 l( w% ~3 w9 s #define H(x, y, z) ((x) ^ (y) ^ (z))
6 d4 Q) b8 U! |$ S% j4 k4 k9 e#define I(x, y, z) ((y) ^ ((x) | (~z)))
( ~0 _% Z F& J f5 y2 k. X
7 U. n7 J4 R2 ^& M/ F! _ X /* ROTATE_LEFT rotates x left n bits.
*/
) k9 x3 b- I& ?, ?& D d #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: b3 O% n) x" i Z6 x* D7 S( z
; J* o' k$ }$ z8 `* _' O# P6 p /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 i2 g3 A3 V( Y' C U. nRotation is separate from addition to prevent recomputation.
*/
. l2 s x3 {1 K4 [#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( C4 P% m( |3 C0 O#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 J [' G/ {9 o9 k5 s8 v$ p, ] #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) D" x# A* a' T( T" Z: W2 A" M #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' ^ Y* J" u( f* k3 F: ^
/ t2 w. w1 {) Q D4 d0 {4 L4 ]/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 K% p3 J# l6 X0 D9 Kvoid MD5Init (context)
. u- U+ ?+ m9 ]8 q$ EMD5_CTX *context; /* context */
3 N2 d- b2 I h0 @0 v$ Y& Q {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ p$ Z* U4 n0 T/ b4 u- P# r- x*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; X5 m3 r2 p# t}
; Y( X* u; R. O* I+ P' a E
' z- p3 o: S( J! n& `5 y /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, Z f' c- ]; H& {3 hvoid MD5Update (context, input, inputLen)
; I2 P C; t& X9 h7 d8 VMD5_CTX *context; /* context */
0 G! R1 q$ x4 ]/ G5 C' Cunsigned char *input; /* input block */
3 v) E C4 }5 E2 ]& \9 Vunsigned int inputLen; /* length of input block */
$ o- U3 }- e1 A* J3 v6 p* I {
unsigned int i, index, partLen;
4 m% n* s& V& P% o. y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
w( C" k% v9 D }) b+ N; 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++程序


% ]( |5 y4 f/ i: z( }
0 J7 {/ T2 H# s/ k; `) j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-22 05:45 , Processed in 0.133965 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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