中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) G# K' H" V' o' _ #include "md5.h"
' {- H9 t7 O2 A7 v" v# @* ]
% O% c. w2 g2 ^2 l/* Constants for MD5Transform routine.
*/
( C# o1 k5 B2 k
* U L. Z: e, L
* R$ C* k9 s; L/ ^; g$ o #define S11 7
; [5 k8 d; _4 q- a6 R8 Z#define S12 12
+ t- N8 Q+ w1 ^( {4 v) N- a! K" P #define S13 17
7 ~6 c1 O9 K) K* F+ D#define S14 22
) R! c4 _6 M C# k #define S21 5
2 |6 t$ O3 W7 V6 Y#define S22 9
7 k3 Z0 F8 W1 C #define S23 14
+ v. s1 C: r) }$ y5 L" G# I #define S24 20
. _& h/ p' V) Y; L' N( n% [" o# s#define S31 4
: F1 N8 J$ e; X1 J. r1 ]& S r#define S32 11
1 |7 A; `9 Z% Z% N. x #define S33 16
7 h. F) f* F& M#define S34 23
7 e7 q# Q1 q- F: z3 v* C#define S41 6
. |/ T, w4 K0 q Q# J' C#define S42 10
$ T+ N4 A$ \9 g9 o #define S43 15
+ ]: M& q4 v0 n. u) H V% X6 Z#define S44 21
4 R8 k5 j* ]& e* i/ m$ N
: [! }# p! w: z, w static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, A0 k6 ~! \7 t9 D' ^static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 s0 l, C( G' j! n static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 }; ~; y; t- n" }9 I static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
% y9 h5 s$ C$ P& U4 ^ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ D4 C% Q' b( h C
% h2 l9 L4 L# y! x/ D9 u% G9 i 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 s& T V8 N7 h r$ [* p/ [ };
P* y" ?+ z% |( I
, `6 {9 L& f5 s$ ^8 e. R: u /* F, G, H and I are basic MD5 functions.
*/
; x* k7 f; y' r5 F- ?) Q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 w" _3 Q& C" S2 I #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% ~" V7 Q0 }; {* T; y #define H(x, y, z) ((x) ^ (y) ^ (z))
, W+ ~7 |: U" ]- b #define I(x, y, z) ((y) ^ ((x) | (~z)))
, g) z4 V* F; _; S% `4 ]. ~
, r8 m! j4 T7 n /* ROTATE_LEFT rotates x left n bits.
*/
8 ~2 q( O/ z" M#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
9 }* p `' `, V/ C* t
7 m2 d7 m0 S: q9 L) | /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. \8 l: U0 w: RRotation is separate from addition to prevent recomputation.
*/
6 g9 n# n) O, Q4 L#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% S& a" j6 E0 Y8 t7 j* U$ H3 D, C #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( b5 Z$ t/ q" M! i/ S2 ? #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 W& ?2 J: w) P+ D+ X #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' i5 ]2 L- { u& \' m. h, K
" ]8 e3 J( S* N, V1 Z% ] /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' a x% U) d/ S3 o1 ? void MD5Init (context)
* H# E1 J+ H0 I! `: Q- u) H) s MD5_CTX *context; /* context */
( s. ^: w" K' V8 J" b- Q+ z{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' ?+ R7 k# q. s$ \9 |5 Q*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 `8 M7 {% p+ G }
+ t0 n4 N7 Z8 ~: E |
5 [" y* I1 m6 @. d. E- x5 |' `6 l/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ a; t. @# n3 A! M8 l* a6 \; Lvoid MD5Update (context, input, inputLen)
* c& O- `- N+ m0 TMD5_CTX *context; /* context */
( X* [1 J# h0 bunsigned char *input; /* input block */
0 D' q8 l! ~0 N; ~unsigned int inputLen; /* length of input block */
5 D' @. Y. C% M6 L4 r% g{
unsigned int i, index, partLen;
7 F& p' w& R1 D2 y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
7 x% u3 ]6 }! w- N O- A
/* 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++程序


1 G% L5 Q. Y' ^1 F2 }1 S6 D4 |& o
- y+ D$ b5 H! ] H9 M
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-13 14:38 , Processed in 0.144283 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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