中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 Y" ]& Y. |* ~5 `0 i [- L) o$ A #include "md5.h"
; Z$ U6 o3 R& ^) G g' {
- W, C3 h4 V/ H2 A5 s/* Constants for MD5Transform routine.
*/
. v9 C9 v2 q5 g0 S ]: m
% g( i, w4 Z1 i) U
& C: [+ K- x: S. q2 V+ t/ p3 y9 Z, U #define S11 7
, L* k1 z! I9 Y& z1 f: ]/ X% I#define S12 12
/ o+ E, W2 Y, K% K7 A #define S13 17
6 a( v: c& W) w' z0 g; Y* z #define S14 22
x- b2 L4 L* R/ L#define S21 5
) L- M# q9 `. ^ #define S22 9
2 H0 L' ?$ h4 S3 j#define S23 14
! \1 O9 t' {% D0 M #define S24 20
' s X# G0 v" }! W/ H* r4 M #define S31 4
( b1 {5 G9 ~- P8 y#define S32 11
& F0 D- `5 G+ ]5 J2 H#define S33 16
" w% T" a: ^; J/ [+ h #define S34 23
$ N* z6 b" p5 l [2 f+ Q3 ^ #define S41 6
# u5 z4 U' Y( M9 O #define S42 10
. L/ H( ]# w, y0 x #define S43 15
# c F0 q- ?/ p) e, h( n* A #define S44 21
+ b) v( Z9 R# E. P2 s/ B) W, B* y
% n1 h+ d2 w" D8 Kstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 z- D' g# i1 b1 lstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! l: }; X/ n7 t n static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- J8 p5 h/ e, e; C static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# j1 ?6 _4 t7 D6 g3 \" [ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# H: \) [: d* I; j9 D3 P
- i" L9 w" l) _. N. T6 A3 W! Qstatic 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
4 e" B9 c7 t4 F+ q9 t) F };
) k6 n0 ? z- F9 N% R. z
( f2 L! s# u4 j/* F, G, H and I are basic MD5 functions.
*/
' r' [3 f/ @# D$ ?. u; \! y, Y #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. W- @7 o" F3 @3 E5 }#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 ?% |. @9 P7 I! q. T #define H(x, y, z) ((x) ^ (y) ^ (z))
7 x9 X% U1 {/ A' t. f9 e2 w2 ` #define I(x, y, z) ((y) ^ ((x) | (~z)))
$ V" c; |1 n& y9 t* P7 x
( i+ L# ~# h6 k; }" e O" _+ \2 p6 K/* ROTATE_LEFT rotates x left n bits.
*/
. @: m$ b2 D" u( }+ e3 x `: O8 z/ C3 H #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. J% W \. T+ j6 {
, Q. b& P2 ~, Q" I g0 r/ S/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
g* }8 c! |" R$ q1 p$ c Rotation is separate from addition to prevent recomputation.
*/
# @0 ]0 \, _# G #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 F: Z4 j9 ]1 O8 ` #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% r8 ], r2 V! c" K7 G7 k8 d1 ]& 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); \
}
: u" @( M$ q- I; [$ L# X) j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 F5 c. u C( e- _
$ ]! x9 M& L4 R. ~/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 P0 Y) A# D3 @: p void MD5Init (context)
7 u- ]& o' Q5 M5 c! U j MD5_CTX *context; /* context */
( O/ X: _: T6 x( g8 m {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# ], ~& v3 A2 p# ~ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 J: Q7 Q( l- d' F0 X$ [ }
$ K+ e6 B& q7 X a
- g; p! D, o- ] z /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- y1 f4 Q3 T" `, |) K7 X8 zvoid MD5Update (context, input, inputLen)
- h: }$ {4 E: M, ~ MD5_CTX *context; /* context */
6 G/ O! ?1 W3 v8 S1 G8 N" D unsigned char *input; /* input block */
' O* y* V8 ]8 @. P* n3 X- iunsigned int inputLen; /* length of input block */
/ F7 j8 s: v' W) r' S6 ]& I {
unsigned int i, index, partLen;
3 @1 I/ t% g' f' n
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+ X! H# c" c: \$ D0 E# |
/* 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++程序


q# _7 Y9 D) |! E- [
4 |. f. G+ Z Y8 \2 s$ M
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-4 16:02 , Processed in 0.055420 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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