DES加密解密算法
// 仿射变换加解密.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <conio.h>
static char key[10], key_a[5], code[8], key_b[5], temp[10], key_aa[8], key_bb[8], l[4], r[4], l_a[4], r_a[4], V[8] = { 0, 1, 0, 1, 0, 1, 0, 1 }, b[8];
char Code, CCode[100];
int p_a[10] = { 3, 5, 2, 7, 4, 10, 1, 9, 8, 6 };
int p_b[8] = { 6, 3, 7, 4, 8, 5, 10, 9 };
int S_a[4][4] = { 1, 0, 2, 3, 3, 2, 1, 0, 0, 2, 1, 3, 3, 1, 3, 2 };
int S_b[4][4] = { 0, 1, 2, 3, 2, 0, 1, 3, 3, 0, 1, 0, 2, 1, 0, 3 };
int IP_a[8] = { 2, 6, 3, 1, 4, 8, 5, 7 }, IP_b[8] = { 4, 1, 3, 5, 7, 2, 8, 6 };
int F_E[8] = { 4, 1, 2, 3, 2, 3, 4, 1 };
void C_code(char);
void coder(char a[10]);
void decoder(char a[10]);
void Sub_Key_Product(char a[10]);
void box(char a[4], int b[4][4]);
char XOR(char, char);
char ReCode(char a[8]);
void C_code_b(char);
void get_key();
void Coder(char a[100], char v[8]);
void Decoder(char a[100], char v[8]);
/**************************************************************************/
int main()
{
//int i;
while (getchar() != ‘e‘)
{
//clrscr();
printf("Please Enter The Text You Want to Code:\n");
scanf("%s", CCode);
Coder(CCode, V);
printf("%s", CCode);
printf("\n");
Decoder(CCode, V);
printf("%s", CCode);
}
}
void Coder(char a[100], char v[8]) /*Make the text into coder*/
{
int i, j;
get_key();
C_code(a[0]);
for (i = 0; i<8; i++)
code[i] = XOR(code[i], v[i]);
Code = ReCode(code);
coder(key);
a[0] = Code;
for (j = 1; j<100; j++)
{
C_code_b(a[j - 1]);
C_code(a[j]);
for (i = 0; i<8; i++)
code[i] = XOR(code[i], b[i]);
Code = ReCode(code);
coder(key);
a[j] = Code;
}
}
void get_key() /*Give the key to the computer*/
{
int i;
getch();
printf("Please enter the key:");
scanf("%s", key);
for (i = 0; i<10; i++)
key[i] -= 48;
}
void C_code_b(char m) /*Make the number into binary */
{
char i = 1, n = 64;
if (m<0)
{
b[0] = 1;
m = m + 128;
}
else
b[0] = 0;
while (i<8)
{
b[i] = m / n;
m = m - n*b[i];
n = n / 2;
i++;
}
}
void C_code(char m)
{
char i = 1, n = 64;
if (m<0)
{
code[0] = 1;
m = m + 128;
}
else
code[0] = 0;
while (i<8)
{
code[i] = m / n;
m = m - n*code[i];
n = n / 2;
i++;
}
}
char XOR(char a, char b)
{
if (a == b)
return 0;
else return 1;
}
char ReCode(char a[8])
{
int i;
for (i = 0; i<7; i++)
a[i + 1] = 2 * a[i] + a[i + 1];
return a[7];
}
void box(char a[4], int b[4][4])
{
int t;
t = b[a[0] * 2 + a[3]][a[1] * 2 + a[2]];
temp[6] = t / 2;
temp[7] = t % 2;
}
void Sub_Key_Product(char a[10]) /*Product the key word*/
{
int i;
for (i = 0; i<10; i++)
temp[i] = a[p_a[i] - 1];/* m1 */
for (i = 0; i<5; i++)
{
key_a[i] = temp[(i + 1) % 5];
key_b[i] = temp[(i + 1) % 5 + 5];
}
i = 0;
while (i<5)
{
temp[i] = key_a[i];
i++;
}
while (i<10)
{
temp[i] = key_b[i - 5];
i++;
}/* m2 */
for (i = 0; i<8; i++)
key_aa[i] = temp[p_b[i] - 1];/* k1 */
for (i = 0; i<5; i++)
{
key_a[i] = temp[(i + 2) % 5];
key_b[i] = temp[(i + 2) % 5 + 5];
} /* m2 移位 */
i = 0;
while (i<5)
{
temp[i] = key_a[i];
i++;
}
while (i<10)
{
temp[i] = key_b[i - 5];
i++;
} /* m3 */
for (i = 0; i<8; i++)
key_bb[i] = temp[p_b[i] - 1];/* k2 */
}
void IP()
{
int i;
for (i = 0; i<8; i++)
temp[i] = code[IP_a[i] - 1];
for (i = 0; i<8; i++)
code[i] = temp[i];
}
void de_IP()
{
int i;
for (i = 0; i<8; i++)
temp[i] = code[IP_b[i] - 1];
for (i = 0; i<8; i++)
code[i] = temp[i];
}
void fk(char c_key[8])
{
int i;
for (i = 0; i<4; i++)
{
l[i] = code[i];
r[i] = code[i + 4];
}
for (i = 0; i<8; i++)
temp[i] = XOR(r[F_E[i] - 1], c_key[i]);/* 与key异或 */
for (i = 0; i<4; i++)
{
l_a[i] = temp[i];
r_a[i] = temp[i + 4];
}
box(l_a, S_a);/* S0 */
temp[4] = temp[6];
temp[5] = temp[7];
box(r_a, S_b);/* S1 */
temp[0] = temp[5];
temp[1] = temp[7];
temp[2] = temp[6];
temp[3] = temp[4];/* P4 */
for (i = 0; i<4; i++)
l[i] = XOR(temp[i], l[i]);
i = 0;
while (i<4)
{
code[i] = l[i];
i++;
}
while (i<8)
{
code[i] = r[i - 4];
i++;
}/* 合并 */
}
void SW()
{
int i = 0;
while (i<4)
{
code[i] = r[i];
i++;
}
while (i<8)
{
code[i] = l[i - 4];
i++;
}/* SW */
}
void coder(char a[10])
{
C_code(Code);
IP();
Sub_Key_Product(a);
fk(key_aa);
SW();
fk(key_bb);
de_IP();
Code = ReCode(code);
}
void decoder(char a[10])
{
C_code(Code);
IP();
Sub_Key_Product(a);
fk(key_bb);
SW();
fk(key_aa);
de_IP();
Code = ReCode(code);
}
void Decoder(char a[100], char v[8]) /*Make the coder into text*/
{
int i, j;
get_key();
Code = a[0];
decoder(key);
C_code(Code);
for (i = 0; i<8; i++)
code[i] = XOR(code[i], v[i]);
C_code_b(a[0]);
a[0] = ReCode(code);
for (j = 1; j<100; j++)
{
Code = a[j];
decoder(key);
C_code(Code);
for (i = 0; i<8; i++)
code[i] = XOR(code[i], b[i]);
C_code_b(a[j]);
a[j] = ReCode(code);
}
}
时间: 2024-10-04 00:04:42