中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& T2 p/ W$ c5 U/ u* H9 N% G( I#include "md5.h"
3 D5 a9 [0 B* [/ H
: K% |: T! o1 `8 m8 N$ w /* Constants for MD5Transform routine.
*/
9 v& F2 I2 e% c7 R
* s* e/ i7 p% O `4 i" t
, j$ @: `* g* j) Y: \#define S11 7
* D2 n: X8 x& D$ L, N: X #define S12 12
/ ]" H2 X9 i D% L- ]: P #define S13 17
. O7 _% H/ E. s; D% c1 ^' @#define S14 22
3 |( x4 R$ t- G. y. f% A#define S21 5
. C" Q( A8 T4 m( Q: |#define S22 9
' }, \1 ~7 |2 u3 a#define S23 14
$ y# n& p& J& h, t& Y #define S24 20
9 ~9 g5 c1 K9 p- g& }& t#define S31 4
2 G S% H/ E: I6 ?$ V0 i2 [ #define S32 11
: W- d' T( C# v! n0 f) u6 {; @0 `#define S33 16
P$ c+ r" \8 w( J1 ]) N #define S34 23
) \) {) h8 }$ u9 Z, W+ Z& d9 {#define S41 6
7 |# H8 d% y7 R5 M- h9 i9 J9 u#define S42 10
; {* F% l* n- p/ G7 A8 w#define S43 15
2 i& M! L; w" }" ?2 m #define S44 21
9 I. c& ?* Z3 N" Q
# B: R; l& a4 c" F! tstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 B+ H8 ~6 B5 e" y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 j+ y6 }; Y2 s9 N/ s$ H static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 A( b7 Q# q" @0 a' H' }' Ostatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* a; g5 [+ C, G( ?: A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' n) {# h( g( k6 J
% P2 J0 z$ g l! L3 Istatic 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
1 [& Y5 |! M9 u5 V, x- t) g7 B9 Y6 x };
, k: X J- w5 h! Z. d! s u1 a y
# a; {9 p' l* J: |5 S+ d/* F, G, H and I are basic MD5 functions.
*/
. r. B& ?8 p1 H( i# F#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# U3 R; T( f v- p ]' ^ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" {) P" F- O3 ]8 [; V#define H(x, y, z) ((x) ^ (y) ^ (z))
! Z; S$ m G3 ~- j#define I(x, y, z) ((y) ^ ((x) | (~z)))
% Z+ n+ G% s$ v* U' t; c" u
+ C1 S) q# W# }' N9 I/* ROTATE_LEFT rotates x left n bits.
*/
2 S3 m- P6 _/ O! G #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 P! z9 @- W% p' j. Y7 @) r
3 A9 p4 T1 z L4 k6 v/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. r% ~ V+ S" z- W- bRotation is separate from addition to prevent recomputation.
*/
0 _) N, x) t5 k6 W: l4 q4 Y9 h #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ K1 L3 b- i. }. @3 F( S0 @#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 _' i& f2 ?$ \3 @6 _5 [( E #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, O. [1 \4 S. P3 j) 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); \
}
9 t) F2 L8 G7 R8 y; |
' u& _3 V$ |9 Q+ z5 ~0 J/ _8 E/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: e# o, v7 A3 T+ ~- B( z' \* y" pvoid MD5Init (context)
+ m7 y8 o& F; W) q' LMD5_CTX *context; /* context */
3 L/ ?* L+ y6 m! I; M) I5 G7 l& ?- o{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 q- x6 n' S, k */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 s" \) w' K$ U2 K( I* u; ^ }
2 A' Z) y, ]# L, ~& A
0 p9 M6 x( Y0 B( D7 N /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& K2 |9 ^& D2 f- U; J& t! j8 gvoid MD5Update (context, input, inputLen)
, i; p7 f2 Y: P U5 [1 qMD5_CTX *context; /* context */
' p( V `9 G7 q; i# F unsigned char *input; /* input block */
1 i1 p* ]5 M" M6 M' t+ n( Y; _unsigned int inputLen; /* length of input block */
' W* o% x' Z/ Z* A, U4 \ {
unsigned int i, index, partLen;
; Z' `+ V7 M5 s) U' J2 }0 A* k* Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
7 Z8 h+ `, R, Q* R
/* 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++程序


& O" E6 T5 H) E5 G0 I6 ]" ?
, G5 X, r- Z" j: T2 v
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-10 21:04 , Processed in 0.061605 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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