/*DES算法由加密、解密和子密钥的生成三部分组成。*/
#include<iostream.h>
#include<string.h>
//s盒变换,首末两位决定行,中间四位决定列
void dd(int aa[6],int &hang,int &lie)
{
hang=aa[0]*2+aa[5];
lie=aa[1]*8+aa[2]*4+aa[3]*2+aa[4];
}
int main()
{
/*******************明文及密钥预处理******************/
int cnt=0;
char mingwen[9]={"american"};//明文8位 mingwen[9]='/0';
char key[9]={"cryptogr"};//密钥8位
int minint[64];//明文
int keyint[64];//密钥
int miwen[64];//密文
//明文密文转换为2进制
int i=0,j=0,m=0,n=0;
for(i=0;i<64;i++)//用于存储明文化为二进制
minint[i]=0;
for(i=0;i<64;i++)//用于存储密钥二进制
keyint[i]=0;
for(i=0;i<8;i++)//明文化位2进制
{
int temp=(int)mingwen[i];
int z=0;
for(z=7;z>=0;z--)
{
minint[8*i+z]=temp%2;
temp=temp/2;
cnt++;
}
}
for(i=0;i<8;i++)//密钥化位2进制
{
int temp=(int)key[i];
int z;
for(z=7;z>=0;z--)
{
keyint[8*i+z]=temp%2;
temp=temp/2;
}
}
/***********************置换表***********************/
int pc1[56]={ //pc1 置换表
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};
int pc2[48]={ //pc2置换表
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 };
int beginip[64]={ //初始置换IP
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 };
int zhihuanE[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,