中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 {" U/ t9 Z3 ~, U7 q; I#include "md5.h"
0 `, [) x* O, `- B8 e
- z* _( S4 j$ M0 A+ Q9 q /* Constants for MD5Transform routine.
*/
8 x% n/ B# Y/ M' m ~2 O
+ c' Z4 ^ ~, k: x- T
9 S, w' ~* v7 ]' K #define S11 7
W9 U# v8 a* S6 L5 d3 L: M2 j #define S12 12
8 R% {& t# X/ L3 P8 v l#define S13 17
- N1 o7 k. |! O* m! j) f#define S14 22
) M& D2 s7 q6 {, _: `" Z #define S21 5
+ h. l' z2 J) g# I #define S22 9
! M" }9 x2 u- r. n; u B4 s#define S23 14
3 Z4 m! B" i n; g D #define S24 20
5 h( S* {2 [- \$ R! e, L" N#define S31 4
" {& ]( E1 h( o. p9 s+ ~" { #define S32 11
4 J, a0 ^3 Y4 r+ C+ R/ q7 m #define S33 16
. C: a2 l# {7 l* t9 d #define S34 23
) u1 q5 s- b3 z" q% |7 l3 D0 f#define S41 6
9 e8 U8 U0 \! T) w7 Y& G* x$ T2 A#define S42 10
1 ~$ C6 B5 c" _: V+ v7 c7 t#define S43 15
' _$ }1 c1 Z' a5 Z9 Z+ H#define S44 21
! B4 ~6 r0 h. O! h2 j) d H
) F% D& z8 P/ [1 Z2 P% J$ U( W. bstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 O' H) \2 `- A. }/ ` static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
. ^: ~+ U4 H1 [6 |- f: Estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
; J. u2 }! _7 T( r7 }static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! X; Z; n% A) K% b3 L static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 w8 v6 s2 Q9 e3 N, E: r: E
1 Z/ b6 [4 }/ ?) V- T) }1 qstatic 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
! {7 w7 T( f% N8 S0 V};
4 N, d1 a: b" O
' ?, J$ A; V: n$ y /* F, G, H and I are basic MD5 functions.
*/
! I" J* c" S- x8 b; c9 g: v#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 ~' }; f3 H% S9 R- I #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
- {+ g$ \% Z, T. {, Q/ y; ^ #define H(x, y, z) ((x) ^ (y) ^ (z))
# l1 a; _7 M; ]0 T; {#define I(x, y, z) ((y) ^ ((x) | (~z)))
" v# ~- k3 i' i! l
! P* S/ X' f/ c; K8 {5 i /* ROTATE_LEFT rotates x left n bits.
*/
8 p( @9 J# V, Q \4 S #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! U/ ?3 U4 y7 d8 `2 I# ]* v7 y
, w4 g4 h+ Z- B+ d, ]$ J- H /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' ?+ Y! {' e7 c3 H/ QRotation is separate from addition to prevent recomputation.
*/
% {8 m1 P& c/ L& v. 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); \
}
# O7 S, z; S/ \, v9 j#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- r1 \, a' U ?#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ m' [) S% _0 n6 }/ s9 m9 @: Z #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; m( N- E' e5 Q
* a. y1 U8 f4 l) D' A/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 C+ X: b8 M/ e3 D$ s* @ void MD5Init (context)
, x- Y; t) e3 e6 UMD5_CTX *context; /* context */
7 \* `5 i5 {# |6 C6 i{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ N1 A& R& g+ k: D# y) u( w( X */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 T& O8 S' ]& O$ l0 [ }
J5 c+ t6 \1 y: r, T+ t
9 P6 u7 t j) C; n /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 P' y. r/ H D* ~1 ~void MD5Update (context, input, inputLen)
2 K3 D9 e" N1 f; u, L% @' f% U8 t MD5_CTX *context; /* context */
1 g! z5 {( B3 Z- @! E unsigned char *input; /* input block */
1 |! U+ i' k$ K+ A& kunsigned int inputLen; /* length of input block */
% Z, S' c: I% o{
unsigned int i, index, partLen;
) ]# X0 L! Z* L) C) E& v5 m, Z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, J& y) I$ G* [8 @) G; P. l, c5 V
/* 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++程序


% q0 H/ n4 D" [& S3 d, D3 P
# u w8 ]/ o' T+ Z
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-22 08:16 , Processed in 0.079943 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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