中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 m& r: a0 G a9 q" e #include "md5.h"
8 Y, Q7 U! A9 k6 F3 ]( v7 S8 H
) K w0 b( d( U; l- E7 I/* Constants for MD5Transform routine.
*/
7 }2 x# L C* D) T* q1 ]6 s
* o$ V: l/ a' K- D$ }* S
) _6 a1 I1 K) Q2 ~ #define S11 7
/ o' J7 v. P) @; n; d#define S12 12
1 K2 A' J) w1 R7 V #define S13 17
# q* N a4 K0 @4 s& \ #define S14 22
$ C0 z) I+ x. R' H& H" m2 Y #define S21 5
# j1 A0 c# {6 A' A' _3 ~1 ?#define S22 9
U& C/ Y8 t- e1 p9 V#define S23 14
& L5 T: |* T& [! s# l#define S24 20
; F( o$ x' M7 z0 p& X#define S31 4
" J& n @: P: R, O- h, l #define S32 11
: h. U( @$ N1 V' u* l4 G #define S33 16
. X* e) u% E+ y s2 i- H#define S34 23
6 c, q' @7 {: J2 X, w7 } #define S41 6
) A+ J1 K0 t* n$ t( V#define S42 10
4 x6 R' ]3 U) p D( G" W#define S43 15
: l3 m" |/ V6 g' \) s #define S44 21
9 h ]! l, \' _# w' b# Y
5 \ g' K' J: n1 Ystatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 q3 I M; N( ?8 R0 X, m: y) a static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ m C. a3 a- H L& U5 O1 H# A static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& S' Y% d6 e8 s3 ?static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 v6 m5 v* T8 x g. Lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( z" c0 D# B( @9 K1 J
) k. k6 `$ f; Ystatic 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 Y9 `; h" c* n2 P y# l5 g};
1 L9 `- S, ^. T4 |+ c3 k
0 J6 ^7 g0 ?% @. E g /* F, G, H and I are basic MD5 functions.
*/
0 A. {+ x+ Y+ V5 x#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% u2 R- Y' @; l8 _# P6 V #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: I* I2 ^7 D0 m1 [: A#define H(x, y, z) ((x) ^ (y) ^ (z))
1 a# n) r9 t& g1 m #define I(x, y, z) ((y) ^ ((x) | (~z)))
" |; I% X( t/ e
; ~! T6 r- O( E /* ROTATE_LEFT rotates x left n bits.
*/
* Y3 A5 @. \/ A4 Z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 M5 B: W& N% ~0 u6 T
J$ ~2 V3 a: ?& e3 T. h/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% W4 e4 m" ?0 S0 RRotation is separate from addition to prevent recomputation.
*/
- B5 ^. L' g, _( o$ m#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 c1 Q) p' N1 x1 M: p. [#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- h+ d& j' M+ ]; k! 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); \
}
2 A6 L8 {6 u0 X- q" t) ` #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& J1 n* V$ x$ C% M V* y
2 \4 P( A' I1 N f% S/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 I( V* ?' ]* G. M2 d# Xvoid MD5Init (context)
^# J; e5 B) @1 { MD5_CTX *context; /* context */
1 r0 b$ p8 {, ]# h{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ |. D2 [$ V+ a' t) }, l y*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 {, U9 U& n. w }
' ?% [7 p) C8 t3 U" M7 ~
! X; m" l+ i( g2 X, }) T% T /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, h9 U4 ~2 w0 N/ [3 E8 Rvoid MD5Update (context, input, inputLen)
$ s; v; j- m& w# @2 ]& J% {: wMD5_CTX *context; /* context */
6 |; Q9 G7 q7 S9 \% Y+ L' a: ~+ Vunsigned char *input; /* input block */
! f9 W8 ~& m" S2 X unsigned int inputLen; /* length of input block */
9 k" U8 U* y( r1 `3 ?{
unsigned int i, index, partLen;
; \3 w! ` a: r
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- b. _- g7 }1 c0 T
/* 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++程序


) Y% m% E; p1 j
& J6 m7 `* y+ A4 m: N0 j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-22 19:09 , Processed in 0.055387 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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