中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! s" P4 P" P1 a8 Y j! B( l: z8 ^ #include "md5.h"
, Y& b+ p8 i2 V
% M9 O" Z+ S8 C$ V: Q: X4 g /* Constants for MD5Transform routine.
*/
' N/ i! N" c0 F$ T
2 o T; t3 @3 @( h. z4 P# Y
0 V1 ^% X" k1 c2 c#define S11 7
- {- P' j8 C& j #define S12 12
3 ^% A8 `* k3 l9 W p# `; P#define S13 17
" R/ D1 R0 f6 M) C [% E( l9 ~#define S14 22
" [% @ y( \5 v3 r) W5 b/ ?) ` #define S21 5
' O8 G# u, I4 d' o" }" ^% b. v2 P#define S22 9
* }2 l9 _9 `) j #define S23 14
2 i" ^ |9 I5 _0 M5 a #define S24 20
* U# d3 U' s# }8 m+ ?" s5 E #define S31 4
7 ~6 U3 M# \& j$ H; Z+ w2 r6 N3 P! G" T8 a #define S32 11
: k+ ?5 x, M7 l9 N T0 I3 I# f; Q #define S33 16
. }3 o% Y: N& p& x" u+ ` #define S34 23
- A: m9 x1 p/ a+ Z/ H2 S% G' J #define S41 6
( o% {4 w- G% K1 c6 }9 b7 {% P+ Z#define S42 10
; ?' u* y- Z) { #define S43 15
& R* f) v5 }# I8 E$ q& o #define S44 21
2 n0 [8 E s1 b5 _
) N% ]% ~ R& \! T. y static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) d) `3 m/ P+ J( o; i( Y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
Y! d3 Y6 J$ Q) W- A) y9 v static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
a, |' j, {2 N' L3 sstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ ?% t6 j% B. m+ Z) B. s$ ustatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
" P- \0 m" t; D( M% n9 Q' D
; W0 J8 W$ f* T( Y5 J% mstatic 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
% ^- O- K2 |2 V1 q: \8 p};
. v: x5 l$ ] k
b$ H. ?/ H$ Z/* F, G, H and I are basic MD5 functions.
*/
7 ?$ L2 _3 B1 }6 I #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 u8 b( J$ s; C% j k: _3 ^ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ j/ g R' \$ v# o; n. n* j' n #define H(x, y, z) ((x) ^ (y) ^ (z))
" d# F) f$ v" Z z" [, k8 S+ D#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 V3 u8 G+ q, K* m, s1 x3 A4 H
$ `5 a9 h+ Y+ w3 ~' q5 x /* ROTATE_LEFT rotates x left n bits.
*/
1 k# d0 R: i9 G1 C* w3 b2 y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' r* y7 N* N) m1 [8 a
* ]2 _ R2 n; P9 I' [/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ a8 U8 S, a; N( q, P9 }1 k" u! c+ L5 \Rotation is separate from addition to prevent recomputation.
*/
4 k. o( x5 `- i, I% D# \: m) A#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
o. T) `1 o" z, Q2 W# R. R! W #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# N. t" c3 g1 | #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 ^/ L2 t) N$ L! `$ T8 K+ h r/ i) B7 k4 B #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, U; t ?3 P7 t; {( k
. ]1 c3 c1 S$ p2 g- S( y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) p% h7 a7 e7 P. ?, s svoid MD5Init (context)
" g/ c* @+ a) w' x* JMD5_CTX *context; /* context */
. h/ }. |2 ~% q5 @{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' X `% \5 L% y9 z" e*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! A5 D2 `. @& u: E! b" }5 i# G4 P}
5 g" f; A+ i2 D) W. _* ]
9 g5 f. O. T! s! V& h+ Q4 A' o' N" F /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, D1 s3 b5 m) {8 _9 A: s6 V# K/ Hvoid MD5Update (context, input, inputLen)
- v8 |) e r9 s3 y- m4 `" pMD5_CTX *context; /* context */
z2 D5 C! w+ u( f G unsigned char *input; /* input block */
f$ t; B& T* Y2 L7 k unsigned int inputLen; /* length of input block */
1 M# z% e( q) H1 w0 M8 O{
unsigned int i, index, partLen;
7 g& H9 n- j5 H, s+ g
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ c. a, U2 _# E2 K8 H
/* 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++程序


8 [/ T7 N; ]% n5 T
' b0 O* l) \) ~& f& c* K
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-25 04:32 , Processed in 0.055195 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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