Skip to content

Instantly share code, notes, and snippets.

@thasan3003
Created December 2, 2018 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thasan3003/c43959beec366449d6b514df29dba956 to your computer and use it in GitHub Desktop.
Save thasan3003/c43959beec366449d6b514df29dba956 to your computer and use it in GitHub Desktop.
This is only the key generation part of S-DES.
/*
Md Tahmid Hasan
CSE, BSMRSTU
tahmidhasan3003
*/
#include<bits/stdc++.h>
using namespace std;
int p10[10]= {3,5,2,7,4,10,1,9,8,6}, p8[8]= {6,3,7,4,8,5,10,9};
void showPTable(int x)
{
cout<<"Your P"<<x<<" permutation table is\t: ";
if(x == 8)
for(int i=0; i<x; i++)
cout<<p8[i]<<" ";
else if(x == 10)
for(int i=0; i<x; i++)
cout<<p10[i]<<" ";
cout<<endl<<endl;
}
string doP8(string inputText)
{
string outputText;
for(int i=0; i<8; i++)
outputText += inputText[p8[i]-1];
cout<<"After P8 applying\t: "<<outputText<<endl;
return outputText;
}
string doP10(string inputText)
{
string outputText;
for(int i=0; i<10; i++)
outputText += inputText[p10[i]-1];
cout<<"After P10 applying\t: "<<outputText<<endl<<endl;
return outputText;
}
string doLS1(string inputText)
{
string outputText;
for(int i=0; i<inputText.length(); i++)
{
switch (i)
{
case 4:
outputText += inputText[0];
break;
case 9:
outputText += inputText[5];
break;
default:
outputText += inputText[i+1];
}
}
cout<<"After LS-1 applying\t: "<<outputText<<endl;
return outputText;
}
string doLS2(string inputText)
{
string outputText;
for(int i=0; i<inputText.length(); i++)
{
switch (i)
{
case 3:
outputText += inputText[0];
break;
case 4:
outputText += inputText[1];
break;
case 8:
outputText += inputText[5];
break;
case 9:
outputText += inputText[6];
break;
default:
outputText += inputText[i+2];
}
}
cout<<"After LS-2 applying\t: "<<outputText<<endl;
return outputText;
}
int main()
{
string inputKey, key1, key2, temp, ls1, ls2;
cout<<"Enter 10 bits input key:"<<endl;
cin>>inputKey;
while(inputKey.length() != 10)
{
cout<<"Please enter 10 bits input key:"<<endl;
cin>>inputKey;
}
cout<<endl;
showPTable(8);
showPTable(10);
temp = doP10(inputKey);
ls1 = doLS1(temp);
key1 = doP8(ls1);
cout<<"Key 1\t\t\t: "<<key1<<endl<<endl;
ls2 = doLS2(ls1);
key2 = doP8(ls2);
cout<<"Key 2\t\t\t: "<<key2<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment