中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' K$ Q. P; X m2 g) g. w) P1 R4 O #include "md5.h"
" T5 ^0 k& R) m
* x" a1 k! U6 B8 l( c6 z% W5 L/* Constants for MD5Transform routine.
*/
2 Y* `2 v3 C6 y( ?7 u
( g: D$ \9 j; f( B
7 \, `9 q% O7 s) r/ o#define S11 7
, y% ^% z4 ~5 {# `#define S12 12
" u l; p1 ]# b. z#define S13 17
; F* y6 e: R0 e* G2 l#define S14 22
% z" U4 S( R* ]+ o6 N* R5 b& y. \2 L #define S21 5
4 g, l' {* _9 s; u" n3 g! ]#define S22 9
3 G+ T( o! f x7 `8 G #define S23 14
% w& l2 z$ T/ k. y. M" P#define S24 20
9 F: i2 }5 z6 e5 @5 u$ q( ^5 j/ O #define S31 4
! W6 E5 z8 ]. F #define S32 11
5 w4 i; l" K, ], I, J8 J. T! y#define S33 16
! f# M8 A2 P2 C3 l0 L: {$ _/ m$ K #define S34 23
3 i4 l I% \" g. |: [/ e #define S41 6
" O$ z; I# [ C$ t#define S42 10
' h- {% e* M% ^* { W2 L: `3 ]#define S43 15
, ]" r. |8 N: o7 G! w$ b #define S44 21
q' |% @7 a8 E& @" l: t) q
5 `8 s7 M; R$ X static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
. _# Z0 [# E8 u+ k static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
6 I* a% ^. A& j" d0 J( G static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' w( ]( C9 s0 d/ q7 wstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 `/ I6 `+ k& a1 h* B1 Y: x. N& b static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 E. T4 C! k3 D3 z
5 Q7 C4 ]( C; kstatic 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
) ?/ |- _, M$ v; R7 r& b% g0 Z};
8 m# x9 Z. ` _2 D8 c
2 g' O4 w" V8 N' Y, V) u; F, e7 o /* F, G, H and I are basic MD5 functions.
*/
3 S! }# I' T7 M# F #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; H9 E, m- B& [, g7 S #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 d2 w. ^* r: Y" U' P #define H(x, y, z) ((x) ^ (y) ^ (z))
0 k7 U1 ~ ]9 W9 ]6 b. v- K#define I(x, y, z) ((y) ^ ((x) | (~z)))
. O- G( m2 Y+ m, r# q: \
n+ U: U4 k8 s/* ROTATE_LEFT rotates x left n bits.
*/
' T, H; N% ^! S: K4 l #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
9 a5 S: D; j3 k) \" N* w. p
( C3 A' f! h" r; p9 z /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ ?$ i$ A% |1 B$ G3 B& j5 nRotation is separate from addition to prevent recomputation.
*/
8 O; R1 s2 p/ X9 k% 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); \
}
+ d4 }6 i. m# O/ P& s#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' T% u6 @ H8 R& p #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 p5 E1 _/ u* D #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' e& a/ K+ M7 S" q! I8 k. @( u
4 A+ O# {: a6 C$ v3 S/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
/ a( K8 F. ~6 a, xvoid MD5Init (context)
# T1 c* R; ~2 D8 D0 D* [MD5_CTX *context; /* context */
& @3 h% s. ^3 C {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 a6 ~ W7 _9 G; d1 e6 p*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 l& e, F, h: |( x }
3 c8 p# A1 K) Y) {+ ]1 D
, `" U+ n5 Y) n5 H/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 p, W* T' t/ A; s; F3 Fvoid MD5Update (context, input, inputLen)
( k2 M) i' ]4 b" Z, o9 n1 ^* a3 }MD5_CTX *context; /* context */
, X9 G5 L) Y+ G& g. junsigned char *input; /* input block */
, u3 g: h4 n' `: hunsigned int inputLen; /* length of input block */
$ Q5 d, ^: ^1 v0 s{
unsigned int i, index, partLen;
' v X& s: B w) D& }) Z" @ L
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 I3 \& U7 f; [
/* 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++程序


, A1 {. V6 [% l8 r& R
7 ~& ~7 M9 H/ m' w v7 w% m
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-24 16:13 , Processed in 0.134975 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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