📄 des.cpp
字号:
res[7] = r[32-1];
res[8] = r[39-1];
res[9] = r[7-1];
res[10] = r[47-1];
res[11] = r[15-1];
res[12] = r[55-1];
res[13] = r[23-1];
res[14] = r[63-1];
res[15] = r[31-1];
res[16] = r[38-1];
res[17] = r[6-1];
res[18] = r[46-1];
res[19] = r[14-1];
res[20] = r[54-1];
res[21] = r[22-1];
res[22] = r[62-1];
res[23] = r[30-1];
res[24] = r[37-1];
res[25] = r[5-1];
res[26] = r[45-1];
res[27] = r[13-1];
res[28] = r[53-1];
res[29] = r[21-1];
res[30] = r[61-1];
res[31] = r[29-1];
res[32] = r[36-1];
res[33] = r[4-1];
res[34] = r[44-1];
res[35] = r[12-1];
res[36] = r[52-1];
res[37] = r[20-1];
res[38] = r[60-1];
res[39] = r[28-1];
res[40] = r[35-1];
res[41] = r[3-1];
res[42] = r[43-1];
res[43] = r[11-1];
res[44] = r[51-1];
res[45] = r[19-1];
res[46] = r[59-1];
res[47] = r[27-1];
res[48] = r[34-1];
res[49] = r[2-1];
res[50] = r[42-1];
res[51] = r[10-1];
res[52] = r[50-1];
res[53] = r[18-1];
res[54] = r[58-1];
res[55] = r[26-1];
res[56] = r[33-1];
res[57] = r[1-1];
res[58] = r[41-1];
res[59] = r[9-1];
res[60] = r[49-1];
res[61] = r[17-1];
res[62] = r[57-1];
res[63] = r[25-1];
#endif
return res;
}
char* CDES::Decrypt()
{
// Calculate Keys
GenKeys();
// Begin Encryption
#ifdef SDES
char *ip = DataIP(); // Initial Permutation on Input bits
#else
char *ip = DataIP_DES();
#endif
cout << endl << "Initial Permutation";
print(ip,DATA_LENGTH);
char *loopData = ip;
char *FKr;
for (int i = NO_OF_KEYS ; i >= 1 ; i--)
{
FKr = FKFunction(loopData,i);
cout << endl << "FK" << i << " Result";
print(FKr,DATA_LENGTH);
if (i != 1)
{
loopData = Switch(FKr);
cout << endl << "Switch Result";
print(loopData,DATA_LENGTH);
}
else
{
loopData = FKr;
}
}
char *IPinv = IPInverse(loopData);
cout << endl << "IP Inverse";
print(IPinv,DATA_LENGTH);
return IPinv;
}
void CDES::FillAllS()
{
#ifdef SDES
FillS0_SDES();
FillS1_SDES();
#else DES
FillS0_DES();
FillS1_DES();
FillS2_DES();
FillS3_DES();
FillS4_DES();
FillS5_DES();
FillS6_DES();
FillS7_DES();
#endif
}
void CDES::FillS0_DES()
{
S[0][0][0] = 14; S[0][0][1] = 4; S[0][0][2] = 13; S[0][0][3] = 1;
S[0][0][4] = 2; S[0][0][5] = 15; S[0][0][6] = 11; S[0][0][7] = 8;
S[0][0][8] = 3; S[0][0][9] = 10; S[0][0][10] = 6; S[0][0][11] = 12;
S[0][0][12] = 5; S[0][0][13] = 9; S[0][0][14] = 0; S[0][0][15] = 7;
S[0][1][0] = 0; S[0][1][1] = 15; S[0][1][2] = 7; S[0][1][3] = 4;
S[0][1][4] = 14; S[0][1][5] = 2; S[0][1][6] = 13; S[0][1][7] = 1;
S[0][1][8] = 10; S[0][1][9] = 6; S[0][1][10] = 12; S[0][1][11] = 11;
S[0][1][12] = 9; S[0][1][13] = 5; S[0][1][14] = 3; S[0][1][15] = 8;
S[0][2][0] = 4; S[0][2][1] = 1; S[0][2][2] = 14; S[0][2][3] = 8;
S[0][2][4] = 13; S[0][2][5] = 6; S[0][2][6] = 2; S[0][2][7] = 11;
S[0][2][8] = 15; S[0][2][9] = 12; S[0][2][10] = 9; S[0][2][11] = 7;
S[0][2][12] = 3; S[0][2][13] = 10; S[0][2][14] = 5; S[0][2][15] = 0;
S[0][3][0] = 15; S[0][3][1] = 12; S[0][3][2] = 8; S[0][3][3] = 2;
S[0][3][4] = 4; S[0][3][5] = 9; S[0][3][6] = 1; S[0][3][7] = 7;
S[0][3][8] = 5; S[0][3][9] = 11; S[0][3][10] = 3; S[0][3][11] = 14;
S[0][3][12] = 10; S[0][3][13] = 0; S[0][3][14] = 6; S[0][3][15] = 13;
}
void CDES::FillS1_DES()
{
S[1][0][0] = 15; S[1][0][1] = 1; S[1][0][2] = 8; S[1][0][3] = 14;
S[1][0][4] = 6; S[1][0][5] = 11; S[1][0][6] = 3; S[1][0][7] = 4;
S[1][0][8] = 9; S[1][0][9] = 7; S[1][0][10] = 2; S[1][0][11] = 13;
S[1][0][12] = 12; S[1][0][13] = 0; S[1][0][14] = 5; S[1][0][15] = 10;
S[1][1][0] = 3; S[1][1][1] = 13; S[1][1][2] = 4; S[1][1][3] = 7;
S[1][1][4] = 15; S[1][1][5] = 2; S[1][1][6] = 8; S[1][1][7] = 14;
S[1][1][8] = 12; S[1][1][9] = 0; S[1][1][10] = 1; S[1][1][11] = 10;
S[1][1][12] = 6; S[1][1][13] = 9; S[1][1][14] = 11; S[1][1][15] = 5;
S[1][2][0] = 0; S[1][2][1] = 14; S[1][2][2] = 7; S[1][2][3] = 11;
S[1][2][4] = 10; S[1][2][5] = 4; S[1][2][6] = 13; S[1][2][7] = 1;
S[1][2][8] = 5; S[1][2][9] = 8; S[1][2][10] = 12; S[1][2][11] = 6;
S[1][2][12] = 9; S[1][2][13] = 3; S[1][2][14] = 2; S[1][2][15] = 15;
S[1][3][0] = 13; S[1][3][1] = 8; S[1][3][2] = 10; S[1][3][3] = 1;
S[1][3][4] = 3; S[1][3][5] = 15; S[1][3][6] = 4; S[1][3][7] = 2;
S[1][3][8] = 11; S[1][3][9] = 6; S[1][3][10] = 7; S[1][3][11] = 12;
S[1][3][12] = 0; S[1][3][13] = 5; S[1][3][14] = 14; S[1][3][15] = 9;
}
void CDES::FillS2_DES()
{
S[2][0][0] = 10; S[2][0][1] = 0; S[2][0][2] = 9; S[2][0][3] = 14;
S[2][0][4] = 6; S[2][0][5] = 3; S[2][0][6] = 15; S[2][0][7] = 5;
S[2][0][8] = 1; S[2][0][9] = 13; S[2][0][10] = 12; S[2][0][11] = 7;
S[2][0][12] = 11; S[2][0][13] = 4; S[2][0][14] = 2; S[2][0][15] = 8;
S[2][1][0] = 13; S[2][1][1] = 7; S[2][1][2] = 0; S[2][1][3] = 9;
S[2][1][4] = 3; S[2][1][5] = 4; S[2][1][6] = 6; S[2][1][7] = 10;
S[2][1][8] = 2; S[2][1][9] = 8; S[2][1][10] = 5; S[2][1][11] = 14;
S[2][1][12] = 12; S[2][1][13] = 10; S[2][1][14] = 15; S[2][1][15] = 1;
S[2][2][0] = 13; S[2][2][1] = 6; S[2][2][2] = 4; S[2][2][3] = 9;
S[2][2][4] = 8; S[2][2][5] = 15; S[2][2][6] = 3; S[2][2][7] = 0;
S[2][2][8] = 11; S[2][2][9] = 1; S[2][2][10] = 2; S[2][2][11] = 12;
S[2][2][12] = 5; S[2][2][13] = 10; S[2][2][14] = 14; S[2][2][15] = 7;
S[2][3][0] = 1; S[2][3][1] = 10; S[2][3][2] = 13; S[2][3][3] = 0;
S[2][3][4] = 6; S[2][3][5] = 9; S[2][3][6] = 8; S[2][3][7] = 7;
S[2][3][8] = 4; S[2][3][9] = 15; S[2][3][10] = 14; S[2][3][11] = 3;
S[2][3][12] = 11; S[2][3][13] = 5; S[2][3][14] = 2; S[2][3][15] = 12;
}
void CDES::FillS3_DES()
{
S[3][0][0] = 7; S[3][0][1] = 13; S[3][0][2] = 14; S[3][0][3] = 3;
S[3][0][4] = 0; S[3][0][5] = 6; S[3][0][6] = 9; S[3][0][7] = 10;
S[3][0][8] = 1; S[3][0][9] = 2; S[3][0][10] = 8; S[3][0][11] = 5;
S[3][0][12] = 11; S[3][0][13] = 12; S[3][0][14] = 4; S[3][0][15] = 15;
S[3][1][0] = 13; S[3][1][1] = 8; S[3][1][2] = 11; S[3][1][3] = 5;
S[3][1][4] = 6; S[3][1][5] = 15; S[3][1][6] = 0; S[3][1][7] = 3;
S[3][1][8] = 4; S[3][1][9] = 7; S[3][1][10] = 2; S[3][1][11] = 12;
S[3][1][12] = 1; S[3][1][13] = 10; S[3][1][14] = 14; S[3][1][15] = 9;
S[3][2][0] = 10; S[3][2][1] = 6; S[3][2][2] = 9; S[3][2][3] = 0;
S[3][2][4] = 12; S[3][2][5] = 11; S[3][2][6] = 7; S[3][2][7] = 13;
S[3][2][8] = 15; S[3][2][9] = 1; S[3][2][10] = 3; S[3][2][11] = 14;
S[3][2][12] = 5; S[3][2][13] = 2; S[3][2][14] = 8; S[3][2][15] = 4;
S[3][3][0] = 3; S[3][3][1] = 15; S[3][3][2] = 0; S[3][3][3] = 6;
S[3][3][4] = 10; S[3][3][5] = 1; S[3][3][6] = 13; S[3][3][7] = 8;
S[3][3][8] = 9; S[3][3][9] = 4; S[3][3][10] = 5; S[3][3][11] = 11;
S[3][3][12] = 12; S[3][3][13] = 7; S[3][3][14] = 2; S[3][3][15] = 14;
}
void CDES::FillS4_DES()
{
S[4][0][0] = 2; S[4][0][1] = 12; S[4][0][2] = 4; S[4][0][3] = 1;
S[4][0][4] = 7; S[4][0][5] = 10; S[4][0][6] = 11; S[4][0][7] = 6;
S[4][0][8] = 8; S[4][0][9] = 5; S[4][0][10] = 3; S[4][0][11] = 15;
S[4][0][12] = 13; S[4][0][13] = 0; S[4][0][14] = 14; S[4][0][15] = 9;
S[4][1][0] = 14; S[4][1][1] = 11; S[4][1][2] = 2; S[4][1][3] = 12;
S[4][1][4] = 4; S[4][1][5] = 7; S[4][1][6] = 13; S[4][1][7] = 1;
S[4][1][8] = 5; S[4][1][9] = 0; S[4][1][10] = 15; S[4][1][11] = 10;
S[4][1][12] = 3; S[4][1][13] = 9; S[4][1][14] = 8; S[4][1][15] = 6;
S[4][2][0] = 4; S[4][2][1] = 2; S[4][2][2] = 1; S[4][2][3] = 11;
S[4][2][4] = 10; S[4][2][5] = 13; S[4][2][6] = 7; S[4][2][7] = 8;
S[4][2][8] = 15; S[4][2][9] = 9; S[4][2][10] = 12; S[4][2][11] = 5;
S[4][2][12] = 6; S[4][2][13] = 3; S[4][2][14] = 0; S[4][2][15] = 14;
S[4][3][0] = 11; S[4][3][1] = 8; S[4][3][2] = 12; S[4][3][3] = 7;
S[4][3][4] = 1; S[4][3][5] = 14; S[4][3][6] = 2; S[4][3][7] = 13;
S[4][3][8] = 6; S[4][3][9] = 15; S[4][3][10] = 0; S[4][3][11] = 9;
S[4][3][12] = 10; S[4][3][13] = 4; S[4][3][14] = 5; S[4][3][15] = 3;
}
void CDES::FillS5_DES()
{
S[5][0][0] = 12; S[5][0][1] = 1; S[5][0][2] = 10; S[5][0][3] = 15;
S[5][0][4] = 9; S[5][0][5] = 2; S[5][0][6] = 6; S[5][0][7] = 8;
S[5][0][8] = 0; S[5][0][9] = 13; S[5][0][10] = 3; S[5][0][11] = 4;
S[5][0][12] = 14; S[5][0][13] = 7; S[5][0][14] = 5; S[5][0][15] = 11;
S[5][1][0] = 10; S[5][1][1] = 15; S[5][1][2] = 4; S[5][1][3] = 2;
S[5][1][4] = 7; S[5][1][5] = 12; S[5][1][6] = 9; S[5][1][7] = 5;
S[5][1][8] = 6; S[5][1][9] = 1; S[5][1][10] = 13; S[5][1][11] = 14;
S[5][1][12] = 0; S[5][1][13] = 11; S[5][1][14] = 3; S[5][1][15] = 8;
S[5][2][0] = 9; S[5][2][1] = 14; S[5][2][2] = 15; S[5][2][3] = 5;
S[5][2][4] = 2; S[5][2][5] = 8; S[5][2][6] = 12; S[5][2][7] = 3;
S[5][2][8] = 7; S[5][2][9] = 0; S[5][2][10] = 4; S[5][2][11] = 10;
S[5][2][12] = 1; S[5][2][13] = 13; S[5][2][14] = 11; S[5][2][15] = 6;
S[5][3][0] = 4; S[5][3][1] = 3; S[5][3][2] = 2; S[5][3][3] = 12;
S[5][3][4] = 9; S[5][3][5] = 5; S[5][3][6] = 15; S[5][3][7] = 10;
S[5][3][8] = 11; S[5][3][9] = 14; S[5][3][10] = 1; S[5][3][11] = 7;
S[5][3][12] = 6; S[5][3][13] = 0; S[5][3][14] = 8; S[5][3][15] = 13;
}
void CDES::FillS6_DES()
{
S[6][0][0] = 4; S[6][0][1] = 11; S[6][0][2] = 2; S[6][0][3] = 14;
S[6][0][4] = 15; S[6][0][5] = 0; S[6][0][6] = 8; S[6][0][7] = 13;
S[6][0][8] = 3; S[6][0][9] = 12; S[6][0][10] = 9; S[6][0][11] = 7;
S[6][0][12] = 5; S[6][0][13] = 10; S[6][0][14] = 6; S[6][0][15] = 1;
S[6][1][0] = 13; S[6][1][1] = 0; S[6][1][2] = 11; S[6][1][3] = 7;
S[6][1][4] = 4; S[6][1][5] = 9; S[6][1][6] = 1; S[6][1][7] = 10;
S[6][1][8] = 14; S[6][1][9] = 3; S[6][1][10] = 5; S[6][1][11] = 12;
S[6][1][12] = 2; S[6][1][13] = 15; S[6][1][14] = 8; S[6][1][15] = 6;
S[6][2][0] = 1; S[6][2][1] = 4; S[6][2][2] = 11; S[6][2][3] = 13;
S[6][2][4] = 12; S[6][2][5] = 3; S[6][2][6] = 7; S[6][2][7] = 14;
S[6][2][8] = 10; S[6][2][9] = 15; S[6][2][10] = 6; S[6][2][11] = 8;
S[6][2][12] = 0; S[6][2][13] = 5; S[6][2][14] = 9; S[6][2][15] = 2;
S[6][3][0] = 6; S[6][3][1] = 11; S[6][3][2] = 13; S[6][3][3] = 8;
S[6][3][4] = 1; S[6][3][5] = 4; S[6][3][6] = 10; S[6][3][7] = 7;
S[6][3][8] = 9; S[6][3][9] = 5; S[6][3][10] = 0; S[6][3][11] = 15;
S[6][3][12] = 14; S[6][3][13] = 2; S[6][3][14] = 3; S[6][3][15] = 12;
}
void CDES::FillS7_DES()
{
S[7][0][0] = 13; S[7][0][1] = 2; S[7][0][2] = 8; S[7][0][3] = 4;
S[7][0][4] = 6; S[7][0][5] = 15; S[7][0][6] = 11; S[7][0][7] = 1;
S[7][0][8] = 10; S[7][0][9] = 9; S[7][0][10] = 3; S[7][0][11] = 14;
S[7][0][12] = 5; S[7][0][13] = 0; S[7][0][14] = 12; S[7][0][15] = 7;
S[7][1][0] = 1; S[7][1][1] = 15; S[7][1][2] = 13; S[7][1][3] = 8;
S[7][1][4] = 10; S[7][1][5] = 3; S[7][1][6] = 7; S[7][1][7] = 4;
S[7][1][8] = 12; S[7][1][9] = 5; S[7][1][10] = 6; S[7][1][11] = 11;
S[7][1][12] = 0; S[7][1][13] = 14; S[7][1][14] = 9; S[7][1][15] = 2;
S[7][2][0] = 7; S[7][2][1] = 11; S[7][2][2] = 4; S[7][2][3] = 1;
S[7][2][4] = 9; S[7][2][5] = 12; S[7][2][6] = 14; S[7][2][7] = 2;
S[7][2][8] = 0; S[7][2][9] = 6; S[7][2][10] = 10; S[7][2][11] = 13;
S[7][2][12] = 15; S[7][2][13] = 3; S[7][2][14] = 5; S[7][2][15] = 8;
S[7][3][0] = 2; S[7][3][1] = 1; S[7][3][2] = 14; S[7][3][3] = 7;
S[7][3][4] = 4; S[7][3][5] = 10; S[7][3][6] = 8; S[7][3][7] = 13;
S[7][3][8] = 15; S[7][3][9] = 12; S[7][3][10] = 9; S[7][3][11] = 0;
S[7][3][12] = 3; S[7][3][13] = 5; S[7][3][14] = 6; S[7][3][15] = 11;
}
char* CDES::JoinSres_DES(char *a, char *b, char *c, char *d, char *e, char *f, char *g, char *h)
{
char* JoinTres = new char[32];
int i,j;
for (i = 0 ; i < 4 ; i++)
{
JoinTres[i] = a[i];
}
for (i = 4,j = 0 ; i < 8 ; i++,j++)
{
JoinTres[i] = b[j];
}
for (i = 8,j = 0 ; i < 12 ; i++,j++)
{
JoinTres[i] = c[j];
}
for (i = 12,j = 0 ; i < 16 ; i++,j++)
{
JoinTres[i] = d[j];
}
for (i = 16,j = 0 ; i < 20 ; i++,j++)
{
JoinTres[i] = e[j];
}
for (i = 20,j = 0 ; i < 24 ; i++,j++)
{
JoinTres[i] = f[j];
}
for (i = 24,j = 0 ; i < 28 ; i++,j++)
{
JoinTres[i] = g[j];
}
for (i = 28,j = 0 ; i < 32 ; i++,j++)
{
JoinTres[i] = h[j];
}
return JoinTres;
}
char* CDES::PermuteSres_DES(char *r)
{
char *res = new char[32];
res[0] = r[16-1];
res[1] = r[7-1];
res[2] = r[20-1];
res[3] = r[21-1];
res[4] = r[29-1];
res[5] = r[12-1];
res[6] = r[28-1];
res[7] = r[17-1];
res[8] = r[1-1];
res[9] = r[15-1];
res[10] = r[23-1];
res[11] = r[26-1];
res[12] = r[5-1];
res[13] = r[18-1];
res[14] = r[31-1];
res[15] = r[10-1];
res[16] = r[2-1];
res[17] = r[8-1];
res[18] = r[24-1];
res[19] = r[14-1];
res[20] = r[32-1];
res[21] = r[27-1];
res[22] = r[3-1];
res[23] = r[9-1];
res[24] = r[19-1];
res[25] = r[13-1];
res[26] = r[30-1];
res[27] = r[6-1];
res[28] = r[22-1];
res[29] = r[11-1];
res[30] = r[4-1];
res[31] = r[25-1];
return res;
}
void CDES::GenKeys()
{
char *keyPF = keyIPermutation();
char *keyLshifted = LeftShift(keyPF,'L',1); // Left shift 1st Five bits to 1 bit
char *keyRshifted = LeftShift(keyPF,'R',1); // Left shift 2nd Five bits to 1 bit
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),1); // create Key1
#ifdef SDES
keyLshifted = LeftShift(keyPF,'L',3);
keyRshifted = LeftShift(keyPF,'R',3);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),2); // create Key2
#else
keyLshifted = LeftShift(keyPF,'L',2);
keyRshifted = LeftShift(keyPF,'R',2);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),2); // create Key2
keyLshifted = LeftShift(keyPF,'L',4);
keyRshifted = LeftShift(keyPF,'R',4);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),3); // create key3
keyLshifted = LeftShift(keyPF,'L',6);
keyRshifted = LeftShift(keyPF,'R',6);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),4); // create key4
keyLshifted = LeftShift(keyPF,'L',8);
keyRshifted = LeftShift(keyPF,'R',8);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),5); // create key5
keyLshifted = LeftShift(keyPF,'L',10);
keyRshifted = LeftShift(keyPF,'R',10);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),6); // create key6
keyLshifted = LeftShift(keyPF,'L',12);
keyRshifted = LeftShift(keyPF,'R',12);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),7); // create key7
keyLshifted = LeftShift(keyPF,'L',14);
keyRshifted = LeftShift(keyPF,'R',14);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),8); // create key8
keyLshifted = LeftShift(keyPF,'L',15);
keyRshifted = LeftShift(keyPF,'R',15);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),9); // create key9
keyLshifted = LeftShift(keyPF,'L',17);
keyRshifted = LeftShift(keyPF,'R',17);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),10); // create key10
keyLshifted = LeftShift(keyPF,'L',19);
keyRshifted = LeftShift(keyPF,'R',19);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),11); // create key11
keyLshifted = LeftShift(keyPF,'L',21);
keyRshifted = LeftShift(keyPF,'R',21);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),12); // create key12
keyLshifted = LeftShift(keyPF,'L',23);
keyRshifted = LeftShift(keyPF,'R',23);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),13); // create key13
keyLshifted = LeftShift(keyPF,'L',25);
keyRshifted = LeftShift(keyPF,'R',25);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),14); // create key14
keyLshifted = LeftShift(keyPF,'L',27);
keyRshifted = LeftShift(keyPF,'R',27);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),15); // create key15
keyLshifted = LeftShift(keyPF,'L',28);
keyRshifted = LeftShift(keyPF,'R',28);
keyPermutationA(JoinShifted(keyLshifted,keyRshifted),16); // create key16
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -