中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
2 W+ r. I0 B# k9 c #include "md5.h"
/ Q2 Y' z6 i* b* ?- U8 Z# [' r
9 _7 R/ {0 t2 ^3 R% L* l) h* ?% f/* Constants for MD5Transform routine.
*/
/ L4 |6 c9 Z0 U+ ?
& O' j+ a) U. p0 c" r5 ?% Q3 `) K
% x$ }0 x. D8 T8 i" \* F8 b; @#define S11 7
+ t I8 D& M+ w0 H#define S12 12
" X4 J8 i8 G( N5 v9 Y T6 C0 C4 Y* ]$ m #define S13 17
3 f: A2 S q! v. A+ @3 t4 D#define S14 22
# H8 c* Y2 }- ?3 H! V #define S21 5
0 j) \! v6 n( {) ~* @#define S22 9
# R* x7 [: G# d9 _ #define S23 14
; e$ y. n$ [8 n# b0 P P' z3 O#define S24 20
, t3 g* I# o' g8 e* F# @* u! e #define S31 4
: V7 d* s3 e( r, l/ f# ~ #define S32 11
3 k* C" U0 s; P' V* w( S$ ]#define S33 16
* a6 J. J4 U1 U* Q3 s/ e#define S34 23
2 @+ [# P! |3 _7 ]6 s5 O; ?8 w#define S41 6
6 S2 y, P' q! b5 w1 C#define S42 10
7 U: R9 q/ y1 J% j, v/ ~ #define S43 15
, z; D9 }3 z9 {( R0 i! ]0 \+ h#define S44 21
. @5 j7 O; x* h) e
( y+ Q! j: F# e- s2 @; v static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 N @- N8 k* Y/ g" d) W static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 s: Z: ]/ T/ [' V; istatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) H- t) o- ]; q& S3 B: b static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- m3 e! Z& M! q, b5 n; A2 x static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' M+ C2 y6 P+ m' J z
0 P* A B3 z3 K+ f) B 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
/ Q, v( g- O6 p) j };
) E! S r. `- C2 F Y: L
0 J/ `% M0 L# ]% y7 U /* F, G, H and I are basic MD5 functions.
*/
- Q$ `7 n! p# W0 d9 Y* Q#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 }/ h u. d. w3 A5 }8 J#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) y8 x0 J4 H9 C ~/ H; I ?8 q#define H(x, y, z) ((x) ^ (y) ^ (z))
% }8 L+ e8 |& |3 H7 u#define I(x, y, z) ((y) ^ ((x) | (~z)))
5 H) ^0 K+ P2 @ ~
1 Y8 c6 Q; m$ x0 V9 A /* ROTATE_LEFT rotates x left n bits.
*/
; W% S) @3 O/ g#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 K" `; V( p# h. `0 m
' a8 B" F+ n/ s6 v! X7 q /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 N$ h. E) M" K c2 o+ R Rotation is separate from addition to prevent recomputation.
*/
q# w) j3 s' x: a#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
{4 B8 { P f! n#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- v+ W0 H& Y; j, u3 ?& o #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% U4 n& `3 G& n! V8 V) k% O# 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); \
}
3 |8 d* m# n1 T
3 g/ x- E! X$ Q8 `# r/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 T) Z2 Y0 q9 X6 V6 evoid MD5Init (context)
% ]& A; G; n# t @: | MD5_CTX *context; /* context */
" J5 v: u2 S* t% ?0 |{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! K2 ]* q' o r3 [! J- w */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ h: {7 l+ q# w5 c3 x- V, T}
( K8 o! m5 s% ?; x7 B6 E
! C; v+ c& S) L2 @/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. M8 j2 Y! G @8 yvoid MD5Update (context, input, inputLen)
5 U9 W" s% f5 \4 x MD5_CTX *context; /* context */
; k/ Q, ~% o- B& B$ f+ xunsigned char *input; /* input block */
& i" p1 k. L) r c; b3 z6 Q unsigned int inputLen; /* length of input block */
% b" ^+ ?4 J* B# ^# {/ N/ O {
unsigned int i, index, partLen;
+ e9 t6 q* D! D" Z5 e ~
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" X/ W# ~* Q7 }4 v0 M
/* 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++程序


( X( s% e$ J! J5 d( F1 ?% K
1 e8 \. X. f9 e
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-19 06:36 , Processed in 0.054964 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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