中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 ?, V+ g, I5 T#include "md5.h"
; z6 d- V" B1 v6 ^; |
/ |1 D( Q9 ?; G3 s" g5 p( a8 ` /* Constants for MD5Transform routine.
*/
3 h+ M; @4 A9 m" a- f
- L) R! M: S+ D( V( ?! w) Q
5 p0 P1 M4 j/ e% X7 k#define S11 7
6 ?+ j3 D( ^; J" W6 d5 b# G#define S12 12
`* s# X. t7 U( w T+ |#define S13 17
# p [- ?( a. v+ T #define S14 22
$ `+ P) e3 J" z8 Q1 G( _1 j #define S21 5
7 @" \' I# V8 s9 T8 L, I #define S22 9
( o$ p' E0 @- E- U3 x f #define S23 14
" V: m( `6 w g Z3 H #define S24 20
% O5 E' k' D. b5 @1 {% n#define S31 4
. s) @/ _, n! g1 i4 R #define S32 11
" s# c3 V% L" `' n+ K6 @#define S33 16
9 U7 ~1 j- w% g3 t" B0 E) [& m( n, [#define S34 23
3 k" A6 o+ X6 v% C4 B0 S& `#define S41 6
+ j6 a: P3 X, Z8 w#define S42 10
( ~8 t7 s$ S7 V/ A% c+ U* U- r5 E#define S43 15
. z7 |& G0 k" W$ V& r6 Y: i#define S44 21
5 R7 A' V0 Z! E* R# C; j# x3 d0 d- q
3 L5 @2 d1 i) I' y& W; M) Cstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, f4 ]8 V$ _1 A' O% i static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; v& R" u) R/ u+ b2 M1 [9 K7 Q% h2 r5 J static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& D4 ?5 n7 [2 k$ a3 Q static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" v5 E5 q/ o+ ~9 o' |* M static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
; A# q" l: B: ]1 _4 a: {
3 g9 v# m- d6 e 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
. V @2 w$ X6 {* c- \- f};
7 y. W4 T) [3 z& F) @% O
2 k5 r6 S+ _) |; h /* F, G, H and I are basic MD5 functions.
*/
4 n: O% j1 x4 R. p7 G& B#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, l$ k# C5 K6 d& j8 M8 Q+ g: d. K#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 D5 j$ T# r( H2 p/ Q9 K, G#define H(x, y, z) ((x) ^ (y) ^ (z))
# `% k; d3 [6 A. H* T #define I(x, y, z) ((y) ^ ((x) | (~z)))
7 Y% \# Z+ L. h0 S2 u
: Q# }4 a0 i: v6 ]/* ROTATE_LEFT rotates x left n bits.
*/
2 }% F" I$ v4 B0 t j, N% z#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 S& n7 R* w9 N4 E
' `4 Q. N1 g) D9 |, l& u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
6 y( P$ p( r+ D& G& u Rotation is separate from addition to prevent recomputation.
*/
3 }8 C- C# j/ W#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% z6 a8 [$ ?* U& U#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( A: n8 ~3 n( ~/ ]8 M#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& G* } ~6 v- Q3 C/ g! r/ n# 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); \
}
* }- _$ `5 J. k4 ?# c, d3 `8 [
8 p& V7 A: i; p3 j/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" n% Z! o5 e; Z" w! Dvoid MD5Init (context)
1 @. y Y9 H% k ?8 f3 o( NMD5_CTX *context; /* context */
* D. W! ^' P) m7 x( \ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 z# H; Y$ l: w" E*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ o- q* J! ?+ W0 y }
8 F& G8 g; R: u* j9 r3 r; }
9 {) `7 N8 T3 ]# N9 s /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, L" M- d( l, e* F5 B* s* i* g$ H void MD5Update (context, input, inputLen)
8 _0 _. u& W2 }4 ?* q1 n+ l5 CMD5_CTX *context; /* context */
- v. z6 N" y8 f- d8 O" T7 @unsigned char *input; /* input block */
% a) u2 S3 S0 x: n1 Cunsigned int inputLen; /* length of input block */
j- U" X! E& r' ~2 P{
unsigned int i, index, partLen;
3 ?: {0 K+ ~ P5 t; v
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 x$ [; L- f9 p. k& ~5 `" ?$ O
/* 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++程序


2 e: d( `% D l' f
9 c/ @( n# m. H# z+ k$ H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-25 09:19 , Processed in 0.055565 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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