des大致可以分为三个部分,第一步,密匙的生成,第二步,加密阶段,第三步,解密阶段,下面给出各个部分的测试代码,以及最终的汇总。
第一步:密匙生成函数测试
#include <iostream>
#include <string>
using namespace std;
//总共需要16个子密匙
//密匙的生成分为多步:
//1.将初始的密匙64位压缩成56位
//2.将对应的56位拆分成两个28位,然后分别根据移位表移位,然后合并
//3.将得到的56位压缩成48位
//4.重复执行2和3,得到16个密匙(每一个密匙都依赖于上一个)
int Compress_Page1[8][7]={ 57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
3,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4 };
//根据compress表,选取保留的对应位置字符
string Key_Compression1(string s)
{
string result="";
for(int i=0;i<8;i++)
{
for(int j=0;j<7;j++)
{
result+=s[Compress_Page1[i][j]-1];
}
}
return result;
}
int Shift[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//移位
string Shift_Key(int k,string s)
{
string left=s.substr(0,28);
string right=s.substr(28,56);
string result=left.substr(k,28-k)+left.substr(0,k)+right.substr(k,28-k)+right.substr(0,k);
return result;
}
int Compress_Page2[8][6]={ 14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32 };
//第二次压缩
string Key_Compression2(string s)
{
string result="";
for(int i=0;i<8;i++)
{
for(int j=0;j<6;j++)
{
result+=s[Compress_Page2[i][j]-1];
}
}
return result;
}
string key[20];
//产生密匙的总函数
void Key_Generate(string initial_key)
{
string compress_key1=Key_Compression1(initial_key);
string new_compress_key1=compress_key1;
for(int i=0;i<16;i++)
{
new_compress_key1=Shift_Key(Shift[i],new_compress_key1);
key[i]=Key_Compression2(new_compress_key1);
}
}
//另外,为了密匙的表示方便,使用十六进制来表示,但是上述函数都是默认使用二进制,所以需要以下的转换函数
string H2B(string s)
{
string s1;
string result="";
for(int i=0;i<s.length();i++)
{
int x;
if(s[i]>='0'&&s[i]<='9')
{
x=s[i]-'0';
}else
{
x=s[i]-'A'+10;
}
s1="";
int y=8;
for(int j=1;j<=4;j++)
{
if(x<y)
{
y/=2;
s1+="0";
}else
{
s1+="1";
x=x%y;
y=y/2;
}
}
result+=s1;
}
return result;
}
int main()
{
string test="133457799BBCDFF1";
string temp=H2B(test);
cout<<temp<<endl;
Key_Generate(temp);
for(int i=0;i<16;i++)
{
cout<<"第"<<i+1<<"轮的密匙是:"<<key[i]<<endl;
}
}
第二步:加密函数测试
//加密的过程主要分为三步
//第一步,初始明文的置换
//第二步,分为两组,16轮的迭代异或加密,然后合并
//第三步,结尾置换
//加密的验证和解密的验证是相辅相成的
#include <iostream>
#include <string>
using namespace std;
string key[20] = { "000110110000001011101111111111000111000001110010",
"011110011010111011011001110110111100100111100101",
"010101011111110010001010010000101100111110011001",
"011100101010110111010110110110110011010100011101",
"011111001110110000000111111010110101001110101000",
"011000111010010100111110010100000111101100101111",
"111011001000010010110111111101100001100010111100",
"111101111000101000111010110000010011101111111011",
"111000001101101111101011111011011110011110000001",
"101100011111001101000111101110100100011001001111",
"001000010101111111010011110111101101001110000110",
"011101010111000111110101100101000110011111101001",
"100101111100010111010001111110101011101001000001",
"010111110100001110110111111100101110011100111010",
"101111111001000110001101001111010011111100001010",
"110010110011110110001011000011100001011111110101" };
int Permutation_Page1[8][8] = { 58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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 };
string Initial_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page1[i][j] - 1];
}
}
return result;
}
int Permutation_Page2[8][8] = { 40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25 };
string Final_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page2[i][j] - 1];
}
}
return result;
}
//异或运算
string Xor(string s1, string s2)
{
string result = "";
for (int i = 0; i < s1.length() && i < s2.length(); i++)
{
result += (s1[i] - '0') ^ (s2[i] - '0') + '0';
}
return result;
}
int Extend_Page[8][6] = { 32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1 };
//拓展函数:将拆分过后的32位数据拓展成48位,方便与密匙进行f函数运算
string Extend(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 6; j++)
{
result += s[Extend_Page[i][j] - 1];
}
}
return result;
}
int Compress_Page3[8][4][16] = { {{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}} };
string Data_Compression(string s)
{
string result = "";
int row = 0;
int conlumn = 0;
int count = 0;
for (int i = 0; i <= 42; i = i + 6)
{
row = (s[i] - '0') * 2 + (s[i + 5] - '0') * 1;
conlumn = (s[i + 1] - '0') * 8 + (s[i + 2] - '0') * 4 + (s[i + 3] - '0') * 2 + (s[i + 4] - '0') * 1;
int number = Compress_Page3[count][row][conlumn];
string temp = "";
//以下的步骤是将十进制转换为二进制(已知需要转换为四位
int x = 8;
for (int j = 0; j < 4; j++)
{
if (number < x)
{
temp += "0";
x /= 2;
}
else
{
temp += "1";
number = number % x;
x /= 2;
}
}
result += temp;
count++;//count代表使用不同的压缩表
}
return result;
}
int Permutation_Page3[8][4] = { 16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25 };
string Data_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 4; j++)
{
result += s[Permutation_Page3[i][j] - 1];
}
}
return result;
}
//f函数:第一步32位数据拓展成48位,第二步需要将数据和密匙进行异或运算
//第三步需要将48位的数据拆分为8组,每组六位,根据压缩表(一共8个)压缩成四位,第四步把得到的32位再进行置换
//解释一下压缩:123456,1&6拼接后转十进制代表行号,2345拼接之后转十进制代表列号
//输入为32位的数据和48位的密匙,输出为32位
string f(string data, string key)
{
string extend_data = Extend(data);
cout<<"拓展结果:"<<extend_data<<endl;
string xor_data = Xor(extend_data, key);
cout<<"异或结果:"<<xor_data<<endl;
string compress_data = Data_Compression(xor_data);
cout<<"数据压缩结果:"<<compress_data<<endl;
string permutation_data = Data_Permutation(compress_data);
cout<<"数据置换结果:"<<permutation_data<<endl;
return permutation_data;
}
string Encript(string s)
{
//初始置换
s = Initial_Permutation(s);
cout<<"初始置换的结果"<<s<<endl;
cout<<endl;
//分组+迭代
string left = s.substr(0, 32);
string right = s.substr(32, 64);
cout<<"L[0]="<<left<<endl;
cout<<"R[0]="<<right<<endl;
cout<<endl;
for (int i = 0; i < 16; i++)
{
string temp = right;
right = Xor(left, f(right, key[i]));
left = temp;
cout<<"L["<<i+1<<"]="<<left<<endl;
cout<<"R["<<i+1<<"]="<<right<<endl;
cout<<endl;
}
string result = right+left;//这里的拼接注意是反过来的,解密也是一样
//结尾置换
result = Final_Permutation(result);
return result;
}
string H2B(string s)
{
string s1;
string result = "";
for (int i = 0; i < s.length(); i++)
{
int x;
if (s[i] >= '0'&&s[i] <= '9')
{
x = s[i] - '0';
}
else
{
x = s[i] - 'A' + 10;
}
s1 = "";
int y = 8;
for (int j = 1; j <= 4; j++)
{
if (x < y)
{
y /= 2;
s1 += "0";
}
else
{
s1 += "1";
x = x % y;
y = y / 2;
}
}
result += s1;
}
return result;
}
int main()
{
string test = "0123456789ABCDEF";
string temp = H2B(test);
string result = Encript(temp);
cout << result << endl;
}
第三步:解密函数测试
//加密的过程主要分为三步
//第一步,初始明文的置换
//第二步,分为两组,16轮的迭代异或解密(注意是逆序),然后合并
//第三步,结尾置换
#include <iostream>
#include <string>
using namespace std;
string key[20] = { "000110110000001011101111111111000111000001110010",
"011110011010111011011001110110111100100111100101",
"010101011111110010001010010000101100111110011001",
"011100101010110111010110110110110011010100011101",
"011111001110110000000111111010110101001110101000",
"011000111010010100111110010100000111101100101111",
"111011001000010010110111111101100001100010111100",
"111101111000101000111010110000010011101111111011",
"111000001101101111101011111011011110011110000001",
"101100011111001101000111101110100100011001001111",
"001000010101111111010011110111101101001110000110",
"011101010111000111110101100101000110011111101001",
"100101111100010111010001111110101011101001000001",
"010111110100001110110111111100101110011100111010",
"101111111001000110001101001111010011111100001010",
"110010110011110110001011000011100001011111110101" };
int Permutation_Page1[8][8] = { 58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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 };
string Initial_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page1[i][j] - 1];
}
}
return result;
}
int Permutation_Page2[8][8] = { 40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25 };
string Final_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page2[i][j] - 1];
}
}
return result;
}
//异或运算
string Xor(string s1, string s2)
{
string result = "";
for (int i = 0; i < s1.length() && i < s2.length(); i++)
{
result += (s1[i] - '0') ^ (s2[i] - '0') + '0';
}
return result;
}
int Extend_Page[8][6] = { 32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1 };
//拓展函数:将拆分过后的32位数据拓展成48位,方便与密匙进行f函数运算
string Extend(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 6; j++)
{
result += s[Extend_Page[i][j] - 1];
}
}
return result;
}
int Compress_Page3[8][4][16] = { {{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}} };
string Data_Compression(string s)
{
string result = "";
int row = 0;
int conlumn = 0;
int count = 0;
for (int i = 0; i <= 42; i = i + 6)
{
row = (s[i] - '0') * 2 + (s[i + 5] - '0') * 1;
conlumn = (s[i + 1] - '0') * 8 + (s[i + 2] - '0') * 4 + (s[i + 3] - '0') * 2 + (s[i + 4] - '0') * 1;
int number = Compress_Page3[count][row][conlumn];
string temp = "";
//以下的步骤是将十进制转换为二进制(已知需要转换为四位
int x = 8;
for (int j = 0; j < 4; j++)
{
if (number < x)
{
temp += "0";
x /= 2;
}
else
{
temp += "1";
number = number % x;
x /= 2;
}
}
result += temp;
count++;
}
return result;
}
int Permutation_Page3[8][4] = { 16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25 };
string Data_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 4; j++)
{
result += s[Permutation_Page3[i][j] - 1];
}
}
return result;
}
//f函数:第一步32位数据拓展成48位,第二步需要将数据和密匙进行异或运算
//第三步需要将48位的数据拆分为8组,每组六位,根据压缩表(一共8个)压缩成四位,第四步把得到的32位再进行置换
//解释一下压缩:123456,1&6拼接后转十进制代表行号,2345拼接之后转十进制代表列号
//输入为32位的数据和48位的密匙,输出为32位
string f(string data, string key)
{
string extend_data = Extend(data);
string xor_data = Xor(extend_data, key);
string compress_data = Data_Compression(xor_data);
string permutation_data = Data_Permutation(compress_data);
return permutation_data;
}
string Decript(string s)
{
//初始置换
s = Initial_Permutation(s);
//分组+迭代(逆序)
string left = s.substr(0, 32);
string right = s.substr(32, 64);
for (int i = 15; i>=0; i--)
{
string temp = right;
right = Xor(left, f(right, key[i]));
left = temp;
}
string result = right + left;
//结尾置换
result = Final_Permutation(result);
return result;
}
string B2H(string s)
{
string result="";
char temp;
for(int i=0;i<=s.length()-4;i=i+4)
{
int x=(s[i]-'0')*8+(s[i+1]-'0')*4+(s[i+2]-'0')*2+s[i+3]-'0';
if(x>=10)
{
temp=(char)(x-10+'A');
}else
{
temp=(char)(x+'0');
}
result+=temp;
}
return result;
}
int main()
{
string test = "1000010111101000000100110101010000001111000010101011010000000101";
string result = Decript(test);
result=B2H(result);
cout << result << endl;
}
第四步:综合函数
#include <iostream>
#include <string>
using namespace std;
/*密匙生成*/
//总共需要16个子密匙
//密匙的生成分为多步:
//1.将初始的密匙64位压缩成56位
//2.将对应的56位拆分成两个28位,然后分别根据移位表移位,然后合并
//3.将得到的56位压缩成48位
//4.重复执行2和3,得到16个密匙(每一个密匙都依赖于上一个)
int Compress_Page1[8][7]={ 57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
3,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4 };
//根据compress表,选取保留的对应位置字符
string Key_Compression1(string s)
{
string result="";
for(int i=0;i<8;i++)
{
for(int j=0;j<7;j++)
{
result+=s[Compress_Page1[i][j]-1];
}
}
return result;
}
int Shift[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//移位
string Shift_Key(int k,string s)
{
string left=s.substr(0,28);
string right=s.substr(28,56);
string result=left.substr(k,28-k)+left.substr(0,k)+right.substr(k,28-k)+right.substr(0,k);
return result;
}
int Compress_Page2[8][6]={ 14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32 };
//第二次压缩
string Key_Compression2(string s)
{
string result="";
for(int i=0;i<8;i++)
{
for(int j=0;j<6;j++)
{
result+=s[Compress_Page2[i][j]-1];
}
}
return result;
}
string key[20];
//产生密匙的总函数
void Key_Generate(string initial_key)
{
string compress_key1=Key_Compression1(initial_key);
string new_compress_key1=compress_key1;
for(int i=0;i<16;i++)
{
new_compress_key1=Shift_Key(Shift[i],new_compress_key1);
key[i]=Key_Compression2(new_compress_key1);
}
}
//另外,为了密匙的表示方便,使用十六进制来表示,但是上述函数都是默认使用二进制,所以需要以下的转换函数
string H2B(string s)
{
string s1;
string result="";
for(int i=0;i<s.length();i++)
{
int x;
if(s[i]>='0'&&s[i]<='9')
{
x=s[i]-'0';
}else
{
x=s[i]-'A'+10;
}
s1="";
int y=8;
for(int j=1;j<=4;j++)
{
if(x<y)
{
y/=2;
s1+="0";
}else
{
s1+="1";
x=x%y;
y=y/2;
}
}
result+=s1;
}
return result;
}
/*加密过程*/
//加密的过程主要分为三步
//第一步,初始明文的置换
//第二步,分为两组,16轮的迭代异或加密,然后合并
//第三步,结尾置换
//注:加密的验证和解密的验证是相辅相成的
int Permutation_Page1[8][8] = { 58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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 };
string Initial_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page1[i][j] - 1];
}
}
return result;
}
int Permutation_Page2[8][8] = { 40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25 };
string Final_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
result += s[Permutation_Page2[i][j] - 1];
}
}
return result;
}
//异或运算
string Xor(string s1, string s2)
{
string result = "";
for (int i = 0; i < s1.length() && i < s2.length(); i++)
{
result += (s1[i] - '0') ^ (s2[i] - '0') + '0';
}
return result;
}
int Extend_Page[8][6] = { 32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1 };
//拓展函数:将拆分过后的32位数据拓展成48位,方便与密匙进行f函数运算
string Extend(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 6; j++)
{
result += s[Extend_Page[i][j] - 1];
}
}
return result;
}
int Compress_Page3[8][4][16] = { {{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}} };
string Data_Compression(string s)
{
string result = "";
int row = 0;
int conlumn = 0;
int count = 0;
for (int i = 0; i <= 42; i = i + 6)
{
row = (s[i] - '0') * 2 + (s[i + 5] - '0') * 1;
conlumn = (s[i + 1] - '0') * 8 + (s[i + 2] - '0') * 4 + (s[i + 3] - '0') * 2 + (s[i + 4] - '0') * 1;
int number = Compress_Page3[count][row][conlumn];
string temp = "";
//以下的步骤是将十进制转换为二进制(已知需要转换为四位
int x = 8;
for (int j = 0; j < 4; j++)
{
if (number < x)
{
temp += "0";
x /= 2;
}
else
{
temp += "1";
number = number % x;
x /= 2;
}
}
result += temp;
count++;//count代表使用不同的压缩表
}
return result;
}
int Permutation_Page3[8][4] = { 16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25 };
string Data_Permutation(string s)
{
string result = "";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 4; j++)
{
result += s[Permutation_Page3[i][j] - 1];
}
}
return result;
}
//f函数:第一步32位数据拓展成48位,第二步需要将数据和密匙进行异或运算
//第三步需要将48位的数据拆分为8组,每组六位,根据压缩表(一共8个)压缩成四位,第四步把得到的32位再进行置换
//解释一下压缩:123456,1&6拼接后转十进制代表行号,2345拼接之后转十进制代表列号
//输入为32位的数据和48位的密匙,输出为32位
string f(string data, string key)
{
string extend_data = Extend(data);
string xor_data = Xor(extend_data, key);
string compress_data = Data_Compression(xor_data);
string permutation_data = Data_Permutation(compress_data);
return permutation_data;
}
string Encript(string s)
{
//初始置换
s = Initial_Permutation(s);
//分组+迭代
string left = s.substr(0, 32);
string right = s.substr(32, 64);
for (int i = 0; i < 16; i++)
{
string temp = right;
right = Xor(left, f(right, key[i]));
left = temp;
}
string result = right+left;//这里的拼接注意是反过来的,解密也是一样
//结尾置换
result = Final_Permutation(result);
return result;
}
/*解密过程*/
string Decript(string s)
{
//初始置换
s = Initial_Permutation(s);
//分组+迭代(逆序)
string left = s.substr(0, 32);
string right = s.substr(32, 64);
for (int i = 15; i>=0; i--)
{
string temp = right;
right = Xor(left, f(right, key[i]));
left = temp;
}
string result = right + left;
//结尾置换
result = Final_Permutation(result);
return result;
}
string B2H(string s)
{
string result="";
char temp;
for(int i=0;i<=s.length()-4;i=i+4)
{
int x=(s[i]-'0')*8+(s[i+1]-'0')*4+(s[i+2]-'0')*2+s[i+3]-'0';
if(x>=10)
{
temp=(char)(x-10+'A');
}else
{
temp=(char)(x+'0');
}
result+=temp;
}
return result;
}
int main()
{
string test1="133457799BBCDFF1";//初始密匙
string test2="0123456789ABCDEF";//初始明文
cout<<"明文:"<<test2<<endl;
string temp1=H2B(test1);
string temp2=H2B(test2);
Key_Generate(temp1);
string encript_data = Encript(temp2);
string data_trans = B2H(encript_data);
cout<<"密文:"<<data_trans<<endl;
string decript_data = Decript(encript_data);
string result = B2H(decript_data);
cout<<"解密:"<<result<<endl;
}