中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
1 Q* ?" `) W5 K4 o9 Y( A, ~#include "md5.h"
7 i7 b0 ^7 q: U9 n" M8 x
5 J, \! \, F- Y7 f' [ z/* Constants for MD5Transform routine.
*/
$ f; P0 \" }4 b$ ^# t
' ], l& l( x: o: q6 L3 ?. B
: K" n% p* N: P1 q, V+ g" x' N h#define S11 7
; f/ N4 G1 Z# e h#define S12 12
+ F+ R9 Q) O, u7 Z4 x0 B6 s#define S13 17
3 P# f; o+ P+ E2 y7 M5 k1 n( S# n6 ? #define S14 22
& M2 n$ N+ A O#define S21 5
5 U; H4 M3 ~6 a4 _3 K) I- C #define S22 9
; U# t4 L6 J9 D) Z8 o" Y #define S23 14
9 s: E; Q) C( S5 i#define S24 20
& [9 O7 v5 ]/ G! D5 m1 x! ~& p#define S31 4
3 |- e- P, i" Q: y$ b Q, D #define S32 11
2 \9 Z) C1 B- q5 g#define S33 16
8 T4 Q+ w: S h) c #define S34 23
( l/ I" S6 D, U0 o8 N; c#define S41 6
4 J+ d+ H: S `! g: S3 N( j #define S42 10
! c7 q/ D: K* \! K" J5 } #define S43 15
0 N' e. G, j0 k#define S44 21
" O9 p& w' G8 c' M* f' U1 s8 R3 ?2 u+ Y
! a5 X9 `. x7 t7 R static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 \* a+ B/ O+ y' Lstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
* X* H/ ?, U; v static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" R+ N- ?* p' l# H& C3 J# | static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) s B9 I$ {6 c. ^( |! H static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# g4 J: y- W l/ C& f7 {
& G0 e& p7 e! e& W" R/ S/ vstatic 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
2 O) A6 K! l5 F2 J5 S5 r6 `; P};
2 t0 D3 z( \( W& _8 d
2 G5 c& m6 I; D' l" {2 | /* F, G, H and I are basic MD5 functions.
*/
$ ?) M4 w5 W. T9 ^* m2 ` #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 k2 \8 q4 w. Z# s#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 [1 [- i* {, r) {- Y5 E #define H(x, y, z) ((x) ^ (y) ^ (z))
; s9 G: k# Q q; E0 N2 E! G X$ w#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 u; J) ~; f, w; s6 \
& ]" t- R5 m) y! i /* ROTATE_LEFT rotates x left n bits.
*/
1 r# u; m) X# h1 p z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% N0 u# P3 i6 t: b
1 W: f: o" m- q; I* {$ N /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ H" C8 e+ Q+ X" @ Rotation is separate from addition to prevent recomputation.
*/
! {* \$ t e' I% y, i& ]) I, \* o6 z#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 d; ?3 Z" H B' A #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 K7 @+ v2 k3 Z- `+ h#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ o. \+ M$ G+ `: [9 p #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
@7 z9 ]- `0 r% g
/ p5 [3 m ^# X, [; t$ |/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 I7 c* `! t1 F) S. }9 [ void MD5Init (context)
) f; w# U6 m7 j! ?0 k9 d3 |' [0 R MD5_CTX *context; /* context */
5 f9 | i' ^, X4 W. E9 r$ v{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ |, \# ^3 i% j8 H1 E*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 [- l( Z$ l8 J& C$ f7 M: O5 W' X7 B }
. E* A2 o% v% D* y4 d3 M$ U, `
1 P, |+ U( _2 ^/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 H \, k* l1 w8 Z void MD5Update (context, input, inputLen)
* E b" x% u( Q, X3 {5 ?MD5_CTX *context; /* context */
7 `/ S6 N4 {. Y- yunsigned char *input; /* input block */
2 N; v1 w' G" H/ T( u4 nunsigned int inputLen; /* length of input block */
) c5 v G& `' N! ` {
unsigned int i, index, partLen;
3 X1 [5 @( ?) X4 V/ y0 Y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% f! v& b& k0 P& w7 c
/* 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++程序


5 a# R' n- C$ j9 ^ O6 R, J* J
4 w H; u9 }4 l- p7 \' f& s7 @
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-20 04:06 , Processed in 0.208763 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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