中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( K$ t* Y6 C+ R4 E& L: F#include "md5.h"
: K: |4 j8 V3 F& F
$ e3 M% T ~ I9 @/* Constants for MD5Transform routine.
*/
4 {, C+ s5 p& N
- C$ g9 s( ?/ N( _% T( k
4 d# L. j8 f U2 |8 C3 B" h7 ?4 U/ U b, { #define S11 7
* P# f& i( |; g#define S12 12
' p! n+ j* \2 q9 n2 G Y #define S13 17
9 @( V7 |$ `; o) F. N#define S14 22
; |9 R. U$ E* h! P2 X#define S21 5
, z0 O. l1 M; Q7 \ e) G #define S22 9
2 W1 t3 b- y$ h' z6 q#define S23 14
% \ ?1 h5 n* b#define S24 20
) x, e% l7 ^) p" J; _0 d! {0 H2 h$ A#define S31 4
0 g8 F0 X4 j, m, m+ B4 d#define S32 11
( T' B. U, A, I. A- ]! n6 w#define S33 16
4 {8 s9 @, e* f1 N9 E #define S34 23
, N U4 c6 d- {$ N( D #define S41 6
* j+ e. P/ E2 a. h) A7 t% g4 O. W v" } #define S42 10
5 |; z- [/ A: E/ Z- Y% E" Y #define S43 15
. L$ s$ H" E( q w( H. C #define S44 21
4 \: E6 h! M9 R0 [; Y5 h7 M2 c' s
) ]7 `3 y( {, M! N5 E- m" istatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 v4 ]$ @* t6 a: Ustatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! J( N& b, q9 X! A& ]# ~static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
- ~7 G5 H, ?( z/ T0 d9 n9 Hstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 @* X, ?- @# q3 Dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. }% Y, z6 f' _7 ^
$ ~7 b; [2 m; ]5 P 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
$ R' k% ]6 a1 C6 ?2 X };
" U% T' H# J. R$ b" I4 x
2 ~+ A2 g4 C/ n6 v% W& {: h. r4 M /* F, G, H and I are basic MD5 functions.
*/
4 Q9 y8 W3 e" P$ Q#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& U* D0 u% h' d( I#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, w, d) J+ P" E" I' K+ O2 G #define H(x, y, z) ((x) ^ (y) ^ (z))
) Z- }7 }' s, S# T- P* W#define I(x, y, z) ((y) ^ ((x) | (~z)))
5 R" b; t( u& l# G- p, p
6 c& t- \% R J! Z% t+ O+ Y S /* ROTATE_LEFT rotates x left n bits.
*/
4 f% `! y, [; Y( b& @% ]+ n #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ q2 c4 B: K Y! p; V
/ }; @, C1 c( N4 Z /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ v3 J( B% X8 f8 IRotation is separate from addition to prevent recomputation.
*/
5 r. d. s# n; u& L#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 q4 a8 {) q5 m1 T4 }! i! m #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 s% O5 D& Z* t* ^. a; K #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 z) {/ u0 H( } W o #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 q+ h4 `2 a) H' f1 Y. N. k+ N3 f
! p1 l; ]1 _( ]& ]$ X+ n /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; d8 D: M3 u7 e, x4 bvoid MD5Init (context)
1 ]2 m! v3 n1 p: s6 q* \MD5_CTX *context; /* context */
* k9 l m4 c' \4 D8 K {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) Q. t9 N/ D; {2 T- v*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( }% M% ^9 a L# F8 I) F }
7 j7 N) R& T' A; p& F
* V8 L7 }8 v1 X5 g2 X6 L& V/ Z7 O/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ E5 N; i% ^8 K# [) U" M void MD5Update (context, input, inputLen)
$ L4 t, w5 M$ Q3 l5 r6 |- UMD5_CTX *context; /* context */
+ {( V% C, S8 h; {* B1 E funsigned char *input; /* input block */
- I& \( }# M- `* A& ^unsigned int inputLen; /* length of input block */
" W# Z& T" q% m+ \1 U% W{
unsigned int i, index, partLen;
0 Y- |1 Y. H2 _$ {, T( X
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+ Q0 S) {$ ?1 ~$ E: b& W8 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++程序


" m. o: B2 M) ~% c. O
; Y. H; C" ]+ \2 [
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-12 10:28 , Processed in 0.081238 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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