中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 `8 X0 J, R, l2 d#include "md5.h"
2 |+ Q4 h+ w( `4 B) _$ k
/ `: y: N6 T: @8 A! q$ W2 j/* Constants for MD5Transform routine.
*/
, t7 a: Q9 l G' m( U, X
7 ?6 [! \, Q2 b- D2 L1 c* N
0 |5 H5 D' X9 g r- ?1 Y1 @( c#define S11 7
/ Z0 Q; @& B/ k( ] #define S12 12
; w# `( |3 ?+ h4 f0 e, b' t$ b. e#define S13 17
# D9 w. Z. x: t/ A* ] #define S14 22
1 {/ c. g* T7 ]2 y6 w #define S21 5
* i C4 U6 s' k1 Q #define S22 9
* A; {' p% M V' _* _0 @5 k#define S23 14
! _* M( Z! y% \; r( S0 D1 S#define S24 20
5 \- s& L0 j. f4 O. t) X, c: y #define S31 4
6 D+ f' l7 t: _ #define S32 11
( {% e' I. P1 r ~$ c. {#define S33 16
& `+ c# }( S7 F6 q3 N" u# w- O #define S34 23
/ Y" y1 R* m) u( B* l#define S41 6
7 q: E$ A" v/ E5 {#define S42 10
K4 P0 C$ r8 H% \8 n2 ~ #define S43 15
; V/ Y$ J! g9 `3 f #define S44 21
% N* `5 x: d% A3 |/ q% v
: u( o9 E3 O7 c* u. q+ [$ e4 a static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- Q! ]0 K x- o static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 {) A9 h j5 `4 J6 e static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) l5 T" D* F1 x$ I9 K static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 a+ B: L, x4 Y/ p/ Z& Istatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
: `- t% R) f( b! `' n
6 Q k) H; f0 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
& f' j' b; z8 r/ c3 J6 g$ R };
. J$ X% ?; E3 r8 e/ P/ T
! `' A: L1 t2 F% G- y' X/ `9 `/* F, G, H and I are basic MD5 functions.
*/
. z5 e! e q6 U+ Q4 T8 i/ _ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
]. o) s; O1 E- s c7 R+ |8 ?5 n" b#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 k% h! A: T/ t) N#define H(x, y, z) ((x) ^ (y) ^ (z))
5 D- u. q! z* r( h #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 _) M: ~4 Z& p0 E9 i6 t
: `' C% E' l! M z# ]* G /* ROTATE_LEFT rotates x left n bits.
*/
4 K2 h3 ]6 E& [! `7 _' c7 H2 Y #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 M- `. H) A1 H
G; o: O3 z9 v5 ~, ] /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
O) w: j" o. c' @" J8 c: A# ERotation is separate from addition to prevent recomputation.
*/
1 J* E' `0 `$ }#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 r0 `7 O1 t( K1 G; R#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ C+ U! }" d2 @ K. y) 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); \
}
3 o7 t+ g9 f7 h' ~0 Y/ p#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" k- r7 c t( c$ r$ ]# q! ^$ m8 M
; t: [9 S% @- G! S& t$ h3 V( J/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
~( H) c' F, T% X" z5 ^4 ` void MD5Init (context)
2 k: Y$ R$ x4 L, D0 n- TMD5_CTX *context; /* context */
1 d1 v6 n" p* e4 r) U% n{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# e& u/ O a) x* M */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ N9 z; z# y5 I9 y# f, W }
J' X; f5 S8 `1 T8 v
m3 q, d! |# T% N1 Q3 K( T /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
& d# i3 U. A5 \7 ]& ]void MD5Update (context, input, inputLen)
4 W; ^2 w' ?, @2 T4 D2 J6 dMD5_CTX *context; /* context */
4 y1 G' R. R& n0 c unsigned char *input; /* input block */
" \9 u8 I+ m+ g' F" Xunsigned int inputLen; /* length of input block */
1 x# ~& p& r. M; d5 ~! V8 b; J {
unsigned int i, index, partLen;
Q4 t" Y8 w/ p2 ?( n
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- m# @" s' j* P/ A
/* 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 C E" V7 M% n
L9 L$ F) U& z1 \2 ~
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-29 08:08 , Processed in 0.055897 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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