中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( z) J9 D: C' N, _0 b$ ]#include "md5.h"
! p( t' s& B" b
7 I) @5 I: t& ]. n4 i: W& F /* Constants for MD5Transform routine.
*/
6 S0 U) g7 s/ L4 J
, z: ~& X8 y# V. [2 E3 A
/ d2 H! k# @6 h; V#define S11 7
, s! t6 w" v9 A #define S12 12
# J& J. ^0 y7 G- C' z #define S13 17
0 y8 Z* v, X) {#define S14 22
! r! W5 p- u, S- t* q6 e#define S21 5
/ f2 C! o6 h" A- _( B { #define S22 9
$ G& h9 \7 a0 b" y& ` #define S23 14
; \/ E! o% y1 q+ m% p' Q #define S24 20
0 j. p0 M8 J( G8 D. P( P% O#define S31 4
) p8 q+ d, t" y% I6 S6 ~' ^+ o #define S32 11
5 k& D, m) L* T& l#define S33 16
5 L1 q5 w1 I- E. S' d8 P #define S34 23
B" F) ~$ H+ T" C! y#define S41 6
& Y- U; q7 w3 ~ _/ u$ y# d#define S42 10
& J' o% h, u0 \! D #define S43 15
- |& J% I1 S4 i. b6 k7 m6 ]5 m1 C#define S44 21
/ ~6 H, H' }. t. \7 g# M
/ I6 }; l8 w+ |' i& N6 H static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, {; C; q; T: o' D; p7 M/ n/ H9 dstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) E- J3 m+ F+ h3 V2 B6 F1 n/ H; Estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* P3 s8 |0 g6 U9 ^) J) {# [: Y7 Pstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
|+ x. f3 ]& M" I2 M static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. @- p/ ?6 L8 D) Z- d
& u; i! v8 C4 Y) b2 Y; } 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
( N0 l: K4 U: f* ~) G' Z };
+ p/ x( R. c. c) d$ q
- i( H' I/ Y; C, @7 D/ E/* F, G, H and I are basic MD5 functions.
*/
& \% b) `. d, K+ r/ a #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 \' E( g, Z O' S6 ?( d* V$ H: }. \+ a #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
m6 T7 Y' S* G; v8 [' l #define H(x, y, z) ((x) ^ (y) ^ (z))
" W7 y; i U% W6 r #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 l4 o5 J4 T' o( g$ b3 j3 r' e) J: E
# ]7 T; _" S$ e) Q( e& I6 ~/* ROTATE_LEFT rotates x left n bits.
*/
/ ]. h" a% y L9 a' F( [+ F1 q* l#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. `9 {& @ I) Z
" U8 z# |3 g( O( v- V% \% n# |: C0 l /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: D$ _! d6 V9 c6 g0 i, F" Y: Q Rotation is separate from addition to prevent recomputation.
*/
8 l0 c- ^$ r! P9 v#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ |2 v# y/ ]$ r+ p& k) B#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 D u% U4 ^6 p! Z {* ?$ A #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* y! m9 C( O0 l6 ]" ] #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& L- R5 G) _& f) q% D) I
% ?: m) j9 C3 q2 Z* G/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
z0 e+ q. r; _7 E. Vvoid MD5Init (context)
* P5 G* h8 X5 R. S3 Z, n" \$ `% [MD5_CTX *context; /* context */
7 z1 Z, L% S$ z& l; ~5 m9 k2 z{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
* @) G( l0 S9 A */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
: M9 ~+ e7 @$ `' S T. y}
' X- b9 L$ Y7 ^4 N- b0 q
" L! R1 Y' h1 b3 c8 l5 X /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 K4 V( H5 h$ d k5 J3 ?. Y" _, O void MD5Update (context, input, inputLen)
, Q; y' U3 N. D& ?MD5_CTX *context; /* context */
% ~/ g3 |0 I( f# h/ U: u unsigned char *input; /* input block */
" ?- z! I6 o" \" o( J9 p' f- N unsigned int inputLen; /* length of input block */
& A6 s' e8 E3 K+ q) t {
unsigned int i, index, partLen;
/ O: ?: T- Z) q7 r/ [
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
8 U4 Q" ^9 ~$ 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++程序


/ N7 {( a1 w n( t/ q7 F
2 R, R: w0 ~4 S% @8 U
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-26 15:08 , Processed in 0.248777 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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