用C语言实现MD5算法用C语言实现MD5算法,这个够复杂。。。
""用C语言实现MD5算法.
1)global.h 头文件
#ifndef PROTOTYPES
#define PROTOTYPES 0
#endif
/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;
/* UINT4 defines a four byte word */
typedef unsigned long int UINT4;
/* PROTO_LIST is defined depending on how PROTOTYPES is defined
above.If using PROTOTYPES, then PROTO_LIST returns the list, otherwise
it returns an empty list. */
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
2)md5.h 头文件
/* MD5 context. */
typedef struct {
UINT4 state[4]; /* state (ABCD) */
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
void MD5Init PROTO_LIST ((MD5_CTX *));
void MD5Update PROTO_LIST
((MD5_CTX *, unsigned char *, unsigned int));
void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
3)md5c.c 算法的核心部分
/* 参考 RSA Data Security, Inc., MD5 message-digest algorithm 的C源程
序
*/
#include "global.h"
#include "md5.h"
/* MD5的主循环的轮主要操作为a=b+((a+f(b,c,d)+M+t)<<值如下:
*/
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
/*函数调用说明*/
static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned
int));
static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
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
};
/* F, G, H and I 是MD5的基本函数.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT将 x 循环左移n bits. */
#define ROTATE_LEFT(x, n) (((x) <> (32-(n))))
/* FF, GG, HH, and II 代表1,2,3,4轮计算,先宏定义以便于后面引用.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (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); \
}
/* MD5 的初始化, MD5 操作的开始. */
void MD5Init (context)
MD5_CTX *context; /* context */
{
context->count[0] = context->count[1] = 0;
/* 给出4个32比特的初始向量.*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
/* MD5 块更新操作.不断地对消息块进行MD5 消息-摘要操作,并更新
context.
*/
void MD5Update (context, input, inputLen)
MD5_CTX *context; /* context */
unsigned char *input; /* input block */
unsigned int inputLen; /* length of input block */
{
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
< ((UINT4)inputLen context->count[1] += ((UINT4)inputLen >> 29);
partLen = 64 - index;
/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
MD5_memcpy
((POINTER)&context->buffer[index], (POINTER)input, partLen);
MD5Transform (context->state, context->buffer);
for (i = partLen; i + 63 state, &input);
index = 0;
}
else
i = 0;
/* Buffer remaining input */
MD5_memcpy
((POINTER)&context->buffer[index], (POINTER)&input,
inputLen-i);
}
/* MD5 的结束操作. 输出128比特的散列值并对context归零.
*/
void MD5Final (digest, context)
unsigned char digest[16]; /* message digest */
MD5_CTX *context; /*
context */
{
unsigned char bits[8];
unsigned int index, padLen;
/* Save number of bits */
Encode (bits, context->count, 8);
/* Pad out to 56 mod 64.
*/
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
padLen = (index state, 16);
/* Zeroize sensitive information.
*/
MD5_memset ((POINTER)context, 0, sizeof (*context));
}
/* 对一个消息块的16个子块进行MD5 基本计算.
*/
static void MD5Transform (state, block)
UINT4 state[4];
unsigned char block[64];
{
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode (x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information.
*/
MD5_memset ((POINTER)x, 0, sizeof (x));
}
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4. */
static void Encode (output, input, len)
unsigned char *output;
UINT4 *input;
unsigned int len;
{ unsigned int i, j;
for (i = 0, j = 0; j > 8) & 0xff);
output[j+2] = (unsigned char)((input >> 16) & 0xff);
output[j+3] = (unsigned char)((input >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (UINT4). Assumes len is a
multiple of 4. */
static void Decode (output, input, len)
UINT4 *output;
unsigned char *input;
unsigned int len;
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}
/* Note: Replace "for loop" with standard memcpy if possible. */
static void MD5_memcpy (output, input, len)
POINTER output;
POINTER input;
unsigned int len;
{ unsigned int i;
for (i = 0; i < len; i++)
output = input;
}
/* Note: Replace "for loop" with standard memset if possible. */
static void MD5_memset (output, value, len)
POINTER output;
int value;
unsigned int len;
{ unsigned int i;
for (i = 0; i < len; i++)
((char *)output) = (char)value;
}
4)md5test.c 主测试程序
/* MD5测试程序 */
#include
#include
#include "global.h"
#include "md5.h"
/* Length of test block, number of test blocks. */
static void MDString PROTO_LIST ((char *));
static void MDFilter PROTO_LIST ((void));
static void MDPrint PROTO_LIST ((unsigned char [16]));
/* Prints a message digest in hexadecimal.(十六进制) */
static void MDPrint (digest)
unsigned char digest[16];
{
unsigned int i;
printf("\n");
for (i = 0; i < 16; i++)
printf ("%02x", digest);
}
/* Digests the standard input and prints the result. */
static void MDFilter ()
{
MD5_CTX context;
int len;
unsigned char buffer[16], digest[16];
MD5Init (&context);
while (len = fread (buffer, 1, 16, stdin))
MD5Update (&context, buffer, len);
MD5Final (digest, &context);
MDPrint (digest);
printf ("\n");
}
/* Digests a string and prints the result. */
static void MDString (string)
char *string;
{
MD5_CTX context;
unsigned char digest[16];
unsigned int len = strlen (string);
MD5Init (&context);
MD5Update (&context, string, len);
MD5Final (digest, &context);
printf ("%s = ", string);
MDPrint (digest);
printf ("\n");
}
void main ()
{
printf ("MD5算法测试:\n");
printf ("原文=\n");
printf ("MD5消息摘要\n");
MDString ("\n赵涛");
MDString ("\n合肥电大站 学号:01230100015");
MDString
("\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0
123456789");
}
#include
#include
#include
void expand(const unsigned char in[32],unsigned char out[48])
{
unsigned char table4[48]={ 31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8, 9, 10, 11, 12,
11, 12, 13, 14, 15, 16,
15, 16, 17, 18, 19, 20,
19, 20, 21, 22, 23, 24,
23, 24, 25, 26, 27, 28,
27, 28, 29, 30, 31, 0 };
for (int i=0;i<48;i++) out[i]=in[table4[i]]; }
void keys(const unsigned char password[8], unsigned char K[16][48])
//根据密钥password计算16个子密钥K[16][48]
{
int i;
unsigned char key[64];
//1. 处理密钥:
//1.1 从用户处获得64位密钥.(每第8位为校验位,为使密钥有正确的奇偶校
// 验,每个密钥要有奇数个'1'位.),得key[64];
for (i=0;i<64;i++) {
int byte=i/8;
int bit=i%8;
int value=password[byte];
value>>=bit; value&=1;
key[i]= value?1:0;
}
//1.2 具体过程:
//1.2.1 对密钥key[64]实施换位变换得密钥keyT[56].
// (keyT[i]=key[j]; i=0..55, j值如下表,显然,忽略了校验位)
unsigned char keyT[56];
unsigned char table1[56]={56, 48, 40, 32, 24, 16, 8, 0,
57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14, 6,
61, 53, 45, 37, 29, 21, 13, 5,
60, 52, 44, 36, 28, 20, 12, 4,
27, 19, 11, 3 };
for (i=0; i<56; i++)
keyT[i]=key[table1[i]];
/*1.2.2 把变换后的密钥等分成两部分,前28位记为C, 后28位记为D.
*****************************************************
*****************************************************
***************************************************/
unsigned char *C=keyT;
unsigned char *D=keyT+28;
//1.2.3 计算子密钥(共16个), 从i=0开始。
for (i=0; i<16; i++){
//1.2.3.1 分别对C,D作循环左移
// 每次循环左移位数如下表所示:
// 循环次数i 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15
// 左移位数 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
char loop[16]= {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
unsigned char tmp[28];
int n=loop[i];
int j;
for (j=0; j<28; j++) tmp[j]=C[(j+28-n)%28];
for (j=0; j<28; j++) C[j]=tmp[j];
for (j=0; j<28; j++) tmp[j]=D[(j+28-n)%28];
for (j=0; j<28; j++) D[j]=tmp[j];
//1.2.3.2 串联C,D,得到一个56位数CD[56],然后对此数作如下换位变换
// 以产生48位子密钥K[i][48]。
unsigned char *CD=keyT;
unsigned char table2[48]=
{13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
for (j=0;j<48;j++)
K[i][j]=CD[table2[j]];
//1.2.3.3 按以上方法计算出16个子密钥K[16][48]。
}
}
void des(const unsigned char datain[8], unsigned char dataout[8],
const unsigned char K[16][48], const unsigned char mode)
//根据密钥K[16][48]和mode加密或解密数据块data[8],
//处理后的数据放在dataout[8]中
{
//2.对64位数据块的处理:
//2.1 把数据分成64位的数据块DATA[64],不够64位的以适当的方式填补。
unsigned char DATA[64];
int i,j;
for (i=0;i<64;i++) {
int byte=i/8;
int bit=i%8;
int value=datain[byte];
value>>=bit;
value&=1;
DATA[i]= value;
}
//2.2 对数据块DATA[64]作换位变换为data[64]。
unsigned char data[64];
unsigned char table3[64]= { 57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6 };
for (i=0;i<64;i++) data[i]=DATA[table3[i]];
//2.3 将变换后的数据块等分成前后两部分,前32位记为L[0],后32位记为R[0]。
unsigned char L[17][32];
unsigned char R[17][32];
for (i=0; i<32; i++) L[0][i]=data[i];
for (i=0; i<32; i++) R[0][i]=data[i+32];
unsigned char s[8][4][16]={
/* S[1] */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
/* S[2] */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
/* S[3] */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
/* S[4] */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
/* S[5] */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
/* S[6] */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
/* S[7] */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
/* S[8] */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
unsigned char R_[48];
//2.4 用16个子密钥对数据加密。从i=0开始。
for (i=0; i<16; i++) {
//2.4.1 根据下面表格扩充32位数据R[i]为48位数据R'。
expand(R[i],R_);
//2.4.2 用R'[48]与K[i][48]作异或运算。
for (j=0;j<48;j++)
if (mode) R_[j]^=K[i][j];
else R_[j]^=K[16-1-i][j];
//2.4.3 把所得的48位数分成8个6位数B[8]。
// 1-6位为B[0],7-12位为B[1],……,43-48位为B[7]。
unsigned char B[8];
for (j=0; j<8; j++){ B[j]=0;
for (int k=0; k<6; k++)
B[j]|=(R_[j*6+k]<<(6-1-k));}
//2.4.4 用S密箱里的值替换B[j]。
// 从j=0开始。S密箱里的值为4位数,共8个S密箱
for (j=0; j<8; j++){
//2.4.4.1 取出B[j]的第1和第6位串联起来成一个2位数,记为m.。
// m即是S密箱里用来替换B[j]的数所在的列数。
//2.4.4.2 取出B[j]的第2至第5位串联起来成一个4位数,记为n。
// n即是S密箱里用来替换B[j]的数所在的行数。
int m=0; int n=0;
m|=(B[j]&0x1); m|=((B[j]>>4)&0x2);
n=(B[j]>>1); n&=0x0f;
//2.4.4.3 用S密箱里的值S[j][m][n]替换B[j]。
B[j]=s[j][m][n];
//2.4.4.4 返回2.4.4.1直至8个数据块都被替换。
}
//2.4.5 把B[0]至B[7] 顺序串联起来得到一个32位数tmp[32]。
unsigned char tmp[32];
for (j=0;j<32; j++)
tmp[j]= (B[j/4]>> (4-1-(j%4)))&1;
unsigned char table5[32]={
15, 6, 19, 20, 28, 11, 27, 16,
0, 14, 22, 25, 4, 17, 30, 9,
1, 7, 23, 13, 31, 26, 2, 8,
18, 12, 29, 5, 21, 10, 3, 24};
unsigned char tmp1[32];
// 对tmp[32]做作换位变换为tmp1[32]。
for (j=0;j<32;j++)
tmp1[j]=tmp[table5[j]];
//2.4.6 把得到的结果tmp1[32]与L[i][32]作异或运算。
// 把计算结果赋给R[i+1][32]。
for (j=0;j<32;j++)
R[i+1][j]=L[i][j]^tmp1[j];
//2.4.7 把R[i][32]的值赋给L[i+1][32]。
for (j=0;j<32;j++)
L[i+1][j]=R[i][j];
//2.4.8 从2.4.1循环执行,直到K[15]也被用到。
}
//2.5 把R[16]和L[16] 顺序串联起来得到一个64位数tmp2[64]。
unsigned char tmp2[64];
for (i=0;i<32;i++) tmp2[i]=R[16][i];
for (i=32;i<64;i++)tmp2[i]=L[16][i-32];
// 对这个数实施2.2变换的逆变换得out[64]。
unsigned char out[64];
for (i=0;i<64;i++) out[table3[i]]=tmp2[i];
//2. 6 out[64]即为加密过的64位数据.
for (i=0; i<8;i++){
dataout[i]=0;
for (j=0; j<8; j++)
dataout[i]|=(out[i*8+j]<
//以下是测试部分
void main()
{
char infile[200], outfile[200];
FILE *iF, *oF;
int mode;
unsigned char password[9];
unsigned char source[8];
unsigned char target[8];
unsigned char K[16][48];
printf("请输入文件名:");
scanf("%s",infile);
printf("请输入方式:加密(1), 解密(0):");
scanf("%d",&mode);
printf("输出文件名:");
scanf("%s",outfile);
keys(password,K);
iF=fopen(infile,"rb");
oF=fopen(outfile,"rw");
while(1) {
int n=fread(source,1,8,iF);
if (n==0) break;
if (n<8) memset(source+n,0,8-n);
des(source, target,K,mode);
fwrite(target,1,8,oF);
}
fclose(iF);
fclose(oF);
}
好了,就这些了.
你可以下载标准的加密代码,比如 DES 加密程序。
回答的人很专业,很可信
我来谈谈思路,你把它写出来肯定要超过50行。首先需要用户输入一段口令,比如说8字节。然后按顺序读取文件,每一字节都要轮流跟口令的字节依序做位反运算。
那位md5加密的老兄,加了密,如何解密?