中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
6 \% k& K3 | H+ t1 }. E& s#include "md5.h"
* b) A, P- f8 q/ a" h
+ g5 v- U9 K/ K" W) X9 j /* Constants for MD5Transform routine.
*/
4 m! `) w& b6 `6 U+ B0 ^0 h: |; ?$ [
& B+ j' o, I k
# Z2 o* d3 C8 m; [7 o/ e. n: E#define S11 7
( A* A* w5 e/ m# F% |0 e; o+ r% y#define S12 12
) C- g. q8 I5 z; T, q1 R; ` Y0 q #define S13 17
7 Q+ ~0 O! e, `0 b#define S14 22
* M) g. v' [4 y( U2 N" Z4 s#define S21 5
. X5 K. y' k" p) z$ V4 h8 m1 I% c# x#define S22 9
, h n- i( z, `! A #define S23 14
* Z* u# \: R: v$ H' L' _! _ #define S24 20
- Z8 i: h- [; F$ ?: M! q H #define S31 4
. r' t" a6 Z- M/ a! X, c8 R) v#define S32 11
1 J1 k( w8 I, \! z% n$ H #define S33 16
8 `" V+ S2 P1 F! v/ K #define S34 23
# c7 }0 C, I8 e8 j9 t/ u' A #define S41 6
+ U' U7 [) C" F4 |- V% b- ?4 f #define S42 10
3 A) y) ^( D( P2 f% x: `* E% j4 s" t #define S43 15
: T6 |3 \: K2 L! [+ a. ^ #define S44 21
" B6 K8 k' d: C5 P
) C) s, D% D, {( e- a* H+ F! y5 X2 a' gstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 I6 {7 r. y( E& s" ` static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ P& e* Q8 D: w static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: @ c. q% \4 d: w8 ustatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
( v* w& e9 U; t# Kstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 x! X# N3 {: m7 A% h! I" u
1 h' E/ ~/ u8 q, h' S/ Hstatic 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
7 h& k) W- N/ J* a};
* a; H$ c; A/ W# l1 F
* n/ F% v) o/ S3 @3 g/* F, G, H and I are basic MD5 functions.
*/
! _+ l! l9 P! |1 ^$ ~#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 A+ D6 ?# L. W- P#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! o1 e4 T; z* J2 y#define H(x, y, z) ((x) ^ (y) ^ (z))
/ O& |4 V* V# l1 j* i0 _- b5 b z #define I(x, y, z) ((y) ^ ((x) | (~z)))
: e) l6 v! a9 {$ l: M
- `$ V$ G4 m5 c8 A /* ROTATE_LEFT rotates x left n bits.
*/
# k- b$ _2 t4 j #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
6 |) O/ p$ {2 s
4 I7 Z- X' ~2 _' B, G8 D/ k) C /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 N% C2 | f+ fRotation is separate from addition to prevent recomputation.
*/
- H3 Q; d( Q& }) M1 b+ L- s- X #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; o' W1 I/ j$ f, k9 }#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( a0 ^0 T# M* V% ?; x2 _% O+ 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); \
}
- t) q* K1 i7 n. t9 Q \ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' v8 ^& s9 g- J3 ]1 F. P+ a
$ }) E3 _& ?9 R8 G8 n1 x- M" C /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: e: I7 G5 b8 Vvoid MD5Init (context)
% i# F* A: I+ l ]9 D. h# \ MD5_CTX *context; /* context */
$ I1 O1 g) j+ ~) F, h. I {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" _, I4 P7 s8 |6 \. T) j2 X3 y1 n7 d U*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ W$ U% v( g% [ l}
* Z. V0 d. G. X8 ?" _# V
6 I/ o; K& D0 |2 x( { q$ e$ n( { /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. G+ J( c. I* e, g6 | u( Bvoid MD5Update (context, input, inputLen)
& m' l2 z: @' J, H, XMD5_CTX *context; /* context */
, L! v( ^8 K x- sunsigned char *input; /* input block */
" `2 d2 f) B, Q8 X9 `1 @: lunsigned int inputLen; /* length of input block */
2 u8 T, L8 g/ i \ {
unsigned int i, index, partLen;
4 ?* [$ D: ?8 b$ |! V' l- A4 P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 U' v7 {8 N8 B' z$ Q
/* 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++程序


& ] J: \- H8 g. o, k
2 E4 x* R& m2 {
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-1 01:22 , Processed in 0.063436 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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