中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, I x; N! y- e* z- E d6 m5 p+ l #include "md5.h"
6 X9 X- b% ~ ~, W, A$ I* ~( q) C
( T# ]4 r- H/ `' a1 B5 }$ r/* Constants for MD5Transform routine.
*/
# ^) g; j; f8 S# P5 o3 S; M6 f! f
* g2 D! l+ ~1 d* m0 h
/ }# r% P/ r7 o3 ^ #define S11 7
1 X1 _; ?9 _4 d2 _" m #define S12 12
; t3 z9 T# ?! v4 z1 g# d" n#define S13 17
* }! S7 p; ?" @2 W3 z#define S14 22
6 x+ R, i8 z0 z4 o: H! }) _#define S21 5
/ x( Y2 W5 Q" S! o) o' [/ { #define S22 9
5 }2 e; s& k, n5 S* s #define S23 14
% |1 c& k& j/ N; I1 l #define S24 20
8 T2 e4 T+ X( [- ]/ k#define S31 4
& h7 ~& B$ M8 I; h0 n* |2 [ #define S32 11
- e% \' G: Q7 Y9 c1 t% { #define S33 16
% A- \7 y% j# `8 e" u2 X3 G% m9 T t#define S34 23
4 P; ~- R3 L1 j5 h& X#define S41 6
; A/ m( f* Q9 p* z #define S42 10
) f9 l u) K4 B; i4 P7 q& ? #define S43 15
1 ], p: V1 ^; ?+ j* Y, ^#define S44 21
+ Y: u2 b o0 Z: e* w+ E
2 E+ r' ^" N) r( F' Jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
% C5 q6 v4 l7 k8 t6 ~ r. o static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ f& @$ W0 k/ S( f5 N4 Cstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ D' ?/ Z, V9 S static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+ Z1 Q& k8 d# |7 Tstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+ e$ P$ {! a& m6 G( B& g
, Y+ O7 X5 X+ f7 i- D ostatic 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
- u4 E+ A+ z, c8 g h};
) r7 I1 \" h, K- v* w( H6 V7 B
0 g# Y5 X" Z6 l /* F, G, H and I are basic MD5 functions.
*/
6 j9 _ R1 W! g2 E' A+ G9 \#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* Y$ W% r+ S4 u( t4 {% l3 A; n1 ~#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
w* B; R4 @; Q% n6 D#define H(x, y, z) ((x) ^ (y) ^ (z))
. f0 o' k, d) p, N- ? #define I(x, y, z) ((y) ^ ((x) | (~z)))
- Z% @# O! S# W3 @( G$ L7 B* _! F
4 c i9 c' r( J/* ROTATE_LEFT rotates x left n bits.
*/
/ Q1 W( f2 X$ c- m2 ?#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
; e2 E2 ]0 c# p6 F! |, k
) }0 {3 ?4 u2 ? o/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
9 w5 M! T, H- ]3 a% YRotation is separate from addition to prevent recomputation.
*/
( t+ a- f c* I) y) x i5 o1 U; 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); \
}
2 D U( |+ h* K+ E, v5 n, O3 R4 E #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 Q! @: s3 x4 A+ h: T* z% F #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ Y3 Z/ U# P0 c2 A! Q8 L #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 x& {- P) @! d- K* C& e5 N: j2 _
) K& t4 a m5 p3 S3 V8 D/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- x. I7 E8 U2 u( O- A; z& [4 O void MD5Init (context)
+ j& |, Q# R5 N& d MD5_CTX *context; /* context */
7 b% @, ]% L$ i" E1 q- \ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ _8 R& `. Y: t( b*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 C" E9 q4 f, c& U' u }
# G; h5 c7 n! Z
! S5 F9 b* @% b! D /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, E) p0 m8 N5 E+ U. Evoid MD5Update (context, input, inputLen)
1 ^5 ?% I3 i* F! d3 hMD5_CTX *context; /* context */
7 l* e. `7 Z& ^1 H# u unsigned char *input; /* input block */
8 ]$ z# Q {, d5 K" aunsigned int inputLen; /* length of input block */
8 ?; O( \% b' n9 q" P {
unsigned int i, index, partLen;
8 p) |$ m7 s' M& [0 g
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 z& ], z/ Z" H- v. P
/* 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++程序


9 ]; I% D9 y6 `" H
) H% t- I( v0 K$ O9 U% m2 c
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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