中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, n: ]" M$ e& \4 {/ z* w! o7 B, M#include "md5.h"
* \" _3 R% X+ [! T+ g! {; o
4 O+ R, Q. s" ^ J4 q, R# F/* Constants for MD5Transform routine.
*/
' l: g7 x/ |5 x6 Z- v2 s
- I' M+ a, N/ W p2 [3 G5 E
& y( W/ _9 D: j0 ] #define S11 7
- H7 g6 ~/ g& l1 s#define S12 12
* l7 Y; ~2 G# P+ U# y#define S13 17
4 \/ V& H" a: J, a #define S14 22
* z! `) Z, E3 O& `( ~& O# U3 p #define S21 5
$ r; k3 {" n3 d2 ]+ w #define S22 9
4 u0 w: V' e N2 e#define S23 14
9 s8 | J$ E( L- l; a' z #define S24 20
2 J$ ^6 j3 z% p; a5 @) r+ R #define S31 4
# S. z$ D) d$ P2 P! D D/ z#define S32 11
$ v, R- o: ]) G. A7 R8 e, O; s#define S33 16
8 Q$ ?# m% {! e$ i# t/ [; p #define S34 23
3 T$ ^/ j8 Z- k3 o# p/ M #define S41 6
* q& l( e" ^1 S#define S42 10
9 H4 |* {% ? d #define S43 15
/ V- o: Y( o; V. _" F, F2 _ #define S44 21
' u2 b, s w# Z' f* D
2 o$ z5 A( L, W! V# O% { static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ d8 V6 u% S( jstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
V5 L- T; D" ]2 A8 f1 g static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, Z3 H4 C) T! a N H static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. A! C' P( U7 k% ^% {0 X% d. U) }1 g- Jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( x: e! Y! @! f) e
7 q6 e7 `7 |; A0 I1 r: _ 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
@+ g" F8 F- q# {9 ^};
9 h4 T" z- {5 w6 x9 y5 K. i
; a- x8 I4 F! s X. Q: Y6 E/* F, G, H and I are basic MD5 functions.
*/
3 y# O+ Y5 A5 t3 X U #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" e/ H! m5 H! O+ {/ H#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 g1 B0 Z; `6 j+ p9 h8 o. h( [ #define H(x, y, z) ((x) ^ (y) ^ (z))
% ~ ^# O. X. |* a) Z #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 I" E$ }2 v3 c( A
& b$ i [+ }3 Y! g* r% B& D/* ROTATE_LEFT rotates x left n bits.
*/
6 d( a D* P z$ D# M5 n #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: J- `% r/ b$ A$ l7 ?
: c9 R/ q. z* {& P3 {2 P/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# V0 w6 u9 w# z$ {Rotation is separate from addition to prevent recomputation.
*/
: p" s: a+ n b: L: ^9 B #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) C# l1 v- Y; p* Z! D#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& ?0 m: E( e; |+ 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); \
}
% j4 G0 c3 J& @0 w1 Z0 b. i% n! ~ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; j L4 I) K2 ]* @* c
' m0 p0 F6 A: n; e* C3 e$ h/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- n- z% h% d; f% e) avoid MD5Init (context)
. Q8 H) a% i. m: p+ k+ k. M6 T+ nMD5_CTX *context; /* context */
$ N) H% i7 @3 |{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
: C# X5 S% ]6 \* v: U2 U9 h1 G- n# p9 M*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ X- A! v( r A3 y }
- K5 |. z% z# I) L4 @, Z3 @
) V+ {# k+ Y1 T9 W- V4 ` /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( m8 n5 n, u: q1 evoid MD5Update (context, input, inputLen)
: _4 ~! l1 d) h8 u* W% o- RMD5_CTX *context; /* context */
4 l( w2 M% f3 n$ s4 f! i unsigned char *input; /* input block */
; z5 ~ f, X8 b0 r unsigned int inputLen; /* length of input block */
) N. }* V) b& u' l {
unsigned int i, index, partLen;
* H+ j( S/ i G& C
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 g1 X7 f5 P3 |% b: t: p7 r
/* 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++程序


8 i( x) h" _. A, l8 u: t
$ H3 ^5 q, V6 |- Q5 }2 g+ U) a3 j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-31 20:30 , Processed in 0.056800 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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