中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* o8 ~3 c- e U6 @0 Q$ _, n! m7 S6 K #include "md5.h"
. j# U9 } J9 B' k
* u3 Q c" X7 g9 o3 W/* Constants for MD5Transform routine.
*/
9 W! r& b; | A4 j; W& [ a* Q
X- ]( s1 I2 D# e+ g. @5 N% t) f) Z
9 y1 [' d" h! J#define S11 7
0 j1 g1 r' |8 q) ~, @) A #define S12 12
8 b- }( f& p% j #define S13 17
9 O3 s9 N: k( g2 }6 @& B #define S14 22
, S$ ^$ c7 {# v5 \ #define S21 5
5 R+ b7 L- M& {- |2 L3 i7 s- [ #define S22 9
# n) r& m/ P6 N3 O#define S23 14
( S T9 p6 [- J7 A! T! C- L #define S24 20
' N# E4 S' K( w; ~( |! W% ~ #define S31 4
+ T) y) q. z3 Y+ _5 G: D+ ]7 R #define S32 11
4 e- ? c' f& K6 ?" K#define S33 16
/ a1 p' X5 |$ M( e6 d( r #define S34 23
W3 B- @+ S- @. i7 j#define S41 6
. s( o# z R2 F#define S42 10
- O2 P2 v+ ]5 G4 ^; S! R- k' J' _$ \ #define S43 15
. `1 `" m( z+ c _#define S44 21
% Y7 I& \9 X+ I+ n1 q( Y
. E7 J0 {% Q3 f8 _' k7 q; Istatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ t2 P7 Y; m4 \7 t: d8 V5 N+ Kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 G" J0 B7 X1 r: pstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. e `+ }) g3 q5 R. kstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& ^5 ?; U& B: T z( mstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 V/ S0 K* c+ P. h X" K
0 m' ` Q5 q9 s9 Gstatic 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
* X) k# y* n4 S9 q( K};
, O2 y* a4 y$ P3 H; d- d
3 W4 @) U0 ]2 i2 R$ F /* F, G, H and I are basic MD5 functions.
*/
; H2 }( W- h& }! r% G#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- w( U w! R% D8 [ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( m+ d+ s) e' v#define H(x, y, z) ((x) ^ (y) ^ (z))
, z3 q; M) z" e: k3 {* l1 @#define I(x, y, z) ((y) ^ ((x) | (~z)))
% R1 a4 ]( a5 x7 C- `
% T+ j5 ]6 e! W0 b/* ROTATE_LEFT rotates x left n bits.
*/
9 p- X. x- d {* i0 B9 \ u' _ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
* K! P f; C/ i* O3 c3 @0 m
" u: T0 L! ^0 K3 F /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 O; m/ }5 y3 `6 P" vRotation is separate from addition to prevent recomputation.
*/
5 `. g* w L0 A' D) w9 d I P; u+ h 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); \
}
3 Z* a' s5 R0 L2 ^ m/ n #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, l4 W* e7 T3 o1 x$ Z #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 w7 _7 x- E% C6 ?5 }; F) 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); \
}
# S% _: ?. k( X2 u. u' V1 h
4 B8 c4 d" U7 q8 d# [* J9 K /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 c/ I" c' j( Q: y, Uvoid MD5Init (context)
8 c5 V( {1 @1 ^! |& g" F" Y MD5_CTX *context; /* context */
. x: i8 z+ {: \7 T0 P {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 f3 f& Z- G0 e" b$ d2 q*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
P9 z, q, ~# g7 R4 |- |}
' a$ z1 B- y0 [; ~0 W
5 j6 T0 k1 [& N/ t, F: { /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. j1 I) u& v- Y& i: a8 r void MD5Update (context, input, inputLen)
5 Y, d3 o% [, [: ]' K' _% Q MD5_CTX *context; /* context */
* s' {9 B/ }5 [" [2 p6 J+ | unsigned char *input; /* input block */
$ ?0 A$ m1 S" Y1 w1 H unsigned int inputLen; /* length of input block */
: l6 b2 ?& G9 \3 ]6 k: |/ Y {
unsigned int i, index, partLen;
& ~0 J7 [! L: ?+ a0 n* I
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ N( }+ b6 B+ Y7 {, C! Q
/* 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++程序


* U3 L0 K- |) E V5 i* ]) N1 \
9 S0 q" U5 @0 o! \/ Q% N# p
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-24 13:29 , Processed in 0.055771 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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