中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
9 A- U& X* r6 G8 E' o; [) b8 |8 z #include "md5.h"
8 r3 {- y7 C9 t8 c( g
5 D8 }4 m, |* n0 Y, N/* Constants for MD5Transform routine.
*/
2 m2 R+ z9 W3 Q3 v- R+ M
% v2 p& y) f& F' k
4 X, ~* }/ c6 t, `+ ~6 D+ p& S: c+ Z#define S11 7
" }2 m0 J- Z* [( f7 U#define S12 12
" V2 K+ p& Y5 d( \: d#define S13 17
9 M; c5 p5 x/ e! d5 X/ g# s1 } #define S14 22
^" L/ M0 b5 J" N" h& E" H#define S21 5
/ L! j* F1 u- X" r #define S22 9
4 Q0 I+ R+ X& E' z9 E7 [' M$ P#define S23 14
7 P+ O; H M# d2 G2 V V #define S24 20
$ S: R6 f, G2 u$ N0 k+ k; C d #define S31 4
' S# ]# I& g1 P6 `! f) }: i, A( ]#define S32 11
! {4 G7 s/ q" A5 E3 n+ q) r #define S33 16
/ _3 R- g; z+ o/ T#define S34 23
+ E3 n& P+ }( X: K8 }5 b- r* R #define S41 6
7 H; N& p% v& h0 g: a; n4 b2 C#define S42 10
+ l: c8 `1 D( M; w#define S43 15
; |1 }+ U5 C* Z1 t0 E6 S" g1 r5 ?) k #define S44 21
- O" w2 B5 I3 @* N# n# u9 w" O
5 V5 j/ c! f- P static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, K, ?+ r1 e) [" _( T static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' p3 @7 K# A; J. r/ E7 s+ Q- ^1 y- G static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: r- S+ z+ c3 |# p9 A+ ]% D! Sstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 ^) Q- \! b4 S+ w* estatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& r" [$ d B# m5 S( F
U7 d% N+ {$ o$ I8 { static 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
, J& x4 n0 K2 Q( [; w};
# o1 ^. k2 h& q; a- y1 w
- u0 L( n% b+ g, e, i/* F, G, H and I are basic MD5 functions.
*/
' [$ @& ?' Z& [! v#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; p6 X& p' r! {( d" Y* l# i #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ X, Y6 `8 C8 }6 T: `6 Z+ @$ [) n #define H(x, y, z) ((x) ^ (y) ^ (z))
! O; r5 \* X% _4 i, M; z#define I(x, y, z) ((y) ^ ((x) | (~z)))
) y) S7 d/ M" r9 N6 N: n! e
% e$ S) V1 { f$ P3 j, } /* ROTATE_LEFT rotates x left n bits.
*/
; w1 f3 V$ b( T* Q$ \# h- O3 o#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
0 ^. w" ]7 s+ q0 m% o% I' h+ \
5 v8 u. U% d$ @7 P/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
E9 O& @6 o& G: }! LRotation is separate from addition to prevent recomputation.
*/
0 y& `8 L1 R7 @# b9 L$ U #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) G D) _7 t& K#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ q: \% ?3 n o/ @- ?; F! r #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 y' O2 \9 N! P) i) X #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% b6 Q5 ^6 L l( C6 O& z
A) y6 A, E' c" t% | /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 W0 g) I4 @/ c6 Evoid MD5Init (context)
5 C. o0 S F+ `( v8 q+ Y# {+ o MD5_CTX *context; /* context */
' L5 e7 o% [, s% x {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 _, `9 L8 W l0 C, U% J*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 @! H7 q1 y" o4 T}
; u3 a2 ?2 c' p! ]2 K& W2 Z
" ^! ]/ E6 |; P9 ]/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" ]# s' ~4 {: W) Vvoid MD5Update (context, input, inputLen)
% p4 T. O+ N& y! C1 aMD5_CTX *context; /* context */
2 K; w, f! @# |7 V- O' R unsigned char *input; /* input block */
. t8 Y2 z4 P4 k, V0 J X* J unsigned int inputLen; /* length of input block */
* [! }: r$ A/ Z* x, j) c N{
unsigned int i, index, partLen;
+ @4 k$ X# M: w- T# ~
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+ P9 m8 }- e1 c' u
/* 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++程序


- J( Z* `, F# Z, d8 g
( p, \$ `" A( C1 S
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-4 02:41 , Processed in 0.065397 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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