中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ f0 ]& N8 v& [3 o; I #include "md5.h"
& z* p4 y* d8 Z4 n) R& A
; A) D# s' A; V2 b! I8 S /* Constants for MD5Transform routine.
*/
* s/ h8 {, Y& j9 u9 C0 f% w
& X' P8 W, D4 _: Q1 [/ {9 w
) Y3 l6 M8 R# ^7 R: h- n5 O#define S11 7
6 K1 B, E" j" |; {. \, i1 w #define S12 12
- g' r) ]' Y7 y4 G#define S13 17
7 E3 a( b! _ s #define S14 22
% Y. _0 s; J' |$ ~#define S21 5
: P4 l: E/ o. R5 B7 l- A! d7 S+ D" c#define S22 9
& F+ d- o. ^" ^. t #define S23 14
- W' s4 B+ w9 [; ^: {8 E #define S24 20
0 s( w( e, L+ `7 ~ Z: ~ #define S31 4
9 S7 C1 U: f2 ?2 d) } p #define S32 11
8 |, V8 T* X3 l" a+ p3 _ #define S33 16
8 X6 u2 r" p/ R2 X. b& ~# q) q #define S34 23
+ J) i8 G, I9 N9 t* W9 M( |1 m/ E#define S41 6
c* e8 t- N- n #define S42 10
3 A" P9 q& s: i9 Z- Y#define S43 15
2 m' I6 t1 F: H- C9 ]% ~% F#define S44 21
8 e% h1 n$ i: {# o( R6 t
* E& w9 }# G! j- a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
% | l1 E5 \2 m K; d static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ U+ o' |' Y, x6 u1 s2 L1 z7 b& H static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
0 p9 x2 O/ o/ T6 ` H static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& D a1 P) y: [0 [; v. S9 Ustatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 }/ _# [/ A6 O+ c( Y6 p1 u8 J
: p8 y7 @+ V! q! |0 ^4 @ 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
4 [ [9 I% {" F O) A, E};
( X0 h( E+ y! x; u' K2 }; g5 h2 \% n
" r$ X: _' K* b# `/* F, G, H and I are basic MD5 functions.
*/
1 o' q$ ]+ m& Q# t& z" w #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ C* Q5 p* m6 x% w1 d #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
0 t3 Q7 F! e; `) Y2 h# | #define H(x, y, z) ((x) ^ (y) ^ (z))
% `. ^9 c* e8 v) O$ V/ W#define I(x, y, z) ((y) ^ ((x) | (~z)))
& P0 H' K9 G' P. K3 F
! W# f3 r5 p3 V/ P2 C i4 {/* ROTATE_LEFT rotates x left n bits.
*/
- D! k9 W: z- P. N8 L #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 O1 `3 e+ v+ j! y; i5 c
9 C' Z8 P& d/ h1 f3 L+ a/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' O( T7 F; k; B+ z Rotation is separate from addition to prevent recomputation.
*/
# B6 j5 N' Z2 H3 F #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 s3 W3 q+ X) H#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, f! u4 _" D$ I7 s* z #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; U+ g" L" [: e# ?8 V#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 A0 `- O1 E2 P S
. c0 T# D: I- ^" k$ T x- @ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 R" @+ c; y% z$ g% Tvoid MD5Init (context)
# M \% T3 ?, R6 O$ p' w MD5_CTX *context; /* context */
- H1 {7 H- n1 p{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
7 J6 F6 N9 q0 j, |# I6 i' I+ Q. A7 D */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# V) @" g: ]6 g* O! O4 o9 K}
# O8 P. ]% ?3 g$ p/ b4 V& W
8 A9 J# {( p6 ] /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 l! ^6 T# \. c# Tvoid MD5Update (context, input, inputLen)
# W5 M1 u# n9 TMD5_CTX *context; /* context */
& ~( s& i2 ?( _2 b. f unsigned char *input; /* input block */
/ B" X, u1 E+ s3 K unsigned int inputLen; /* length of input block */
- s7 m$ c; Z3 c {
unsigned int i, index, partLen;
1 ?# o( _* P% z, }# u5 w
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& J. H- y) f' \9 X
/* 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++程序


! Q6 `6 g) p& s5 [3 g Y) V
) c& w1 n1 t0 a5 ^( t+ z
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-9 08:28 , Processed in 0.056416 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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