Question

In: Computer Science

SDES C++ The code is not working currently I know the functions are correct but the...

SDES C++

The code is not working currently I know the functions are correct but the original plaintext should match the "ciphertext after" when inputting 4

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <windows.h>


using namespace std;
//function prototypes
void splitString(string &x, string &y, string &original);
void expansionFunction(string &input);
string XORstring(string& str1, string& str2);
string getSBOXValue(string arr[][8], string val);
int binaryToDecimal(int n);
string generateKey();
string findKey(string Key, int round);
string Encryption(string &plaintext, string key);
string Decryption(string& cipher, string key);


int main()
{
   string plaintext = "011100100110", key = "010011001";
   cout << "SDES Encryption|\n---------------|\n";
   cout << "Original Plaintext = " << plaintext;
   cout << "\nOriginal Key" << setw(9) <<" = " << key << endl;

  
   //split string int left and right
   string left, right;
  
   splitString(left, right, plaintext);
  
   cout << endl << "L0 = " << left << endl << "R0 = " << right << endl;
   cout << "\nL1 = R0" << endl;
   string l1 = right; //assign l1 to be r0

   //find r1
   //first expand r0
   string eRight = right;
   expansionFunction(eRight);
   //reduce key value by 1
   key.resize(key.size() - 1);
  
   cout << "\nXOR\n";
   cout << "E(Right) = " << eRight << endl;
   cout << "K1"<< setw(9) <<" = " << key << endl;
   cout << "--------------------------" << endl;
  
   // E(R0) xor k1
   string XORresult = XORstring(eRight, key);
   cout << setw(19) << XORresult;

   //split string into s1 and s2
   string S1BOX[2][8] = {"101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011"};
   string S2BOX[2][8] = {"100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100"};
  

   string s1, s2;
   splitString(s1, s2, XORresult);
  
   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);
   cout << "\nS1 = " << s1 << " = " << s1result << endl << "S2 = " << s2 << " = " << s2result << endl;
   string functionResult = s1result + s2result;
   cout << endl << "Result from SBOX's is = " << functionResult << endl;
   cout << "L0 XOR f(R0,K1) = " << XORstring(left, functionResult) << "(R1)\n";

   string ciphertext = right + XORstring(left, functionResult);
   cout << "\nCiphertext(L1R1) = " << ciphertext << endl;

   cout << "----------------------------------------------------------------------------------------\n" << endl;
   int rounds;
   cout << "Round SDES\n";
   key = generateKey();
   cout << endl;
   string saveKey = key;
   plaintext = "011100100110";
   cout << "Random Key = " << key << endl;
   cout << "Plaintext = " << plaintext << endl;
  
   cout << "\nSelect number of SDES rounds\n-> ";
   cin >> rounds;
   cout << endl;
   cout << "Plaintext = " << plaintext << endl << endl;
  
   for (int i = 0; i < rounds;   i++)
   {
       key = findKey(key, i+1);
       plaintext = Encryption(plaintext, key);
       cout << "Round " << i+1 << " Ciphertext = " << plaintext << endl;
   }
   ciphertext = "";
   ciphertext.append(plaintext, 6, 6);
   ciphertext.append(plaintext, 0, 6);

   cout << endl << "Ciphertext after " << rounds << " rounds: " << ciphertext << endl << endl;
   cout << "Proof by decryption:\n\n" << "Ciphertext: " << ciphertext << endl << endl;

   string d;
   d.append(ciphertext, 6, 6);
   d.append(ciphertext, 0, 6);
   for (int j = rounds; j > 0; j--)
   {
       key = findKey(saveKey, j);
       d = Decryption(d, key);
       if (j != 1)
           cout << "Round " << j << " Ciphertext = " << d << endl;
       else if (j == 1)
           cout << "\nSucceeding Plaintext is: " << d << endl;
   }  

return 0;
}
//function definitions
void splitString(string &x, string &y, string &original)
{
       x = original.substr(0, original.length() / 2);
       y = original.substr(original.length() / 2);
}

void expansionFunction(string &input)
{
   char temp;
   //add two more letters to meet size requirement
   input.append(input, 4, 2);
   temp = input[3];
   input[5] = input[2];
   input[4] = temp;
   input[3] = input[2];
   input[2] = temp;
}

string XORstring(string& str1, string& str2)
{
   string temp = str2;
   for (int i = 0; i < str2.length(); i++)
   {
       temp[i] = (str1[i] ^ str2[i]) + '0';
   }
   return temp;
  
}

string getSBOXValue(string arr[][8], string val)
{
   int column;
   if (val[0] == '0')
   {
       column = stoi(val.substr(1, val.length()));
       column = binaryToDecimal(column);
       for (int i = 0; i < 8; i++)
           if (i == column)
               return arr[0][i];
   }
   else
   {
       column = stoi(val.substr(1, val.length()));
       column = binaryToDecimal(column);
       for (int i = 0; i < 8; i++)
           if (i == column)
               return arr[1][i];
   }
              
}
int binaryToDecimal(int n)
{
   int decimal = 0;

   // Initializing base value to 1, i.e 2^0
   int base = 1;

   int temp = n;
   while (temp) {
       int last = temp % 10;
       temp = temp / 10;
       decimal += last * base;
       base = base * 2;
   }

   return decimal;
}
string generateKey()
{
   string randomKey;
   string binary[2] = { "0","1" };
   randomKey.reserve(8);
   srand(time(0));
   cout << "Generating KEY...\n";
   for (int i = 0; i < 9; i++)
   {
       randomKey += binary[rand()%2];
   }
   return randomKey;


}
string findKey(string Key, int round)
{
   string temp;
   //Get the key for the round
   if (round == 1)
       temp.append(Key, 0, 8);
   else if (round == 2)
       temp.append(Key, 1, 8);
   else if (round == 3)
   {
       temp.append(Key, 2, 7);
       temp.append(Key, 0, 1);
   }
   else if (round == 4)
   {
       temp.append(Key, 3, 6);
       temp.append(Key, 0, 2);
   }
   return temp;
}

string Encryption(string &plaintext, string key)
   {
   string left, right, eRight, result, s1, s2, Ln, Rn;
   string S1BOX[2][8] = { "101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011" };
   string S2BOX[2][8] = { "100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100" };
  
   splitString(left, right, plaintext);

   Ln = right; // Ln = Rn - 1
   eRight = right;
   //use expansion on right side
   expansionFunction(eRight);
   result = XORstring(eRight, key);
   splitString(s1, s2, result);

   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);

   string functionResult = s1result + s2result;
   Rn = XORstring(left, functionResult);
   return Ln + Rn; //L1R2
}

string Decryption(string& cipher, string key)
{
   string Left, Right, eRight, Ln, Rn, result, s1, s2;
   string S1BOX[2][8] = { "101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011" };
   string S2BOX[2][8] = { "100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100" };
   //find key
   splitString(Left, Right, cipher);
   Rn = Left;
   eRight = Rn;
   expansionFunction(eRight);
   result = XORstring(eRight, key);
   splitString(s1, s2, result);
   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);

   string functionResult = s1result + s2result;
   Ln = XORstring(Right, functionResult);
   return Ln + Rn;
}

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <windows.h>


using namespace std;
//function prototypes
void splitString(string &x, string &y, string &original);
void expansionFunction(string &input);
string XORstring(string& str1, string& str2);
string getSBOXValue(string arr[][8], string val);
int binaryToDecimal(int n);
string generateKey();
string findKey(string Key, int round);
string Encryption(string &plaintext, string key);
string Decryption(string& cipher, string key);


int main()
{
   string plaintext = "011100100110", key = "010011001";
  
   cout << "SDES Encryption|\n---------------|\n";
   cout << "Original Plaintext = " << plaintext;
   cout << "\nOriginal Key" << setw(9) << " = " << key << endl;


   //split string int left and right
   string left, right;

   splitString(left, right, plaintext);

   cout << endl << "L0 = " << left << endl << "R0 = " << right << endl;
   cout << "\nL1 = R0" << endl;
   string l1 = right; //assign l1 to be r0

   //find r1
   //first expand r0
   string eRight = right;
   expansionFunction(eRight);
   //reduce key value by 1
   key.resize(key.size() - 1);

   cout << "\nXOR\n";
   cout << "E(Right) = " << eRight << endl;
   cout << "K1" << setw(9) << " = " << key << endl;
   cout << "--------------------------" << endl;

   // E(R0) xor k1
   string XORresult = XORstring(eRight, key);
   cout << setw(19) << XORresult;

   //split string into s1 and s2
   string S1BOX[2][8] = { "101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011" };
   string S2BOX[2][8] = { "100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100" };


   string s1, s2;
   splitString(s1, s2, XORresult);

   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);
   cout << "\nS1 = " << s1 << " = " << s1result << endl << "S2 = " << s2 << " = " << s2result << endl;
   string functionResult = s1result + s2result;
   cout << endl << "Result from SBOX's is = " << functionResult << endl;
   cout << "L0 XOR f(R0,K1) = " << XORstring(left, functionResult) << "(R1)\n";

   string ciphertext = right + XORstring(left, functionResult);
   cout << "\nCiphertext(L1R1) = " << ciphertext << endl;

   cout << "----------------------------------------------------------------------------------------\n" << endl;
   int rounds;
   cout << "Round SDES\n";
   key = generateKey();
   cout << endl;
   string saveKey = key;
   plaintext = "011100100110";
   cout << "Random Key = " << key << endl;
   cout << "Plaintext = " << plaintext << endl;

   cout << "\nSelect number of SDES rounds\n-> ";
   cin >> rounds;
   string *keyarray = new string[rounds];
   for (int i = 0; i < rounds; i++)
   {
       keyarray[i] = generateKey();
   }
   cout << "Plaintext = " << plaintext << endl << endl;

   for (int i = 0; i < rounds; i++)
   {
       key = keyarray[i];
       plaintext = Encryption(plaintext, key);
       cout << "Round " << i + 1 << " Ciphertext = " << plaintext << endl;
   }
   ciphertext = "";
   ciphertext.append(plaintext, 6, 6);
   ciphertext.append(plaintext, 0, 6);

   cout << endl << "Ciphertext after " << rounds << " rounds: " << ciphertext << endl << endl;
   cout << "Proof by decryption:\n\n" << "Ciphertext: " << ciphertext << endl << endl;

   string d;
   d.append(ciphertext, 6, 6);
   d.append(ciphertext, 0, 6);
   for (int j = rounds; j > 0; j--)
   {
       key = keyarray[rounds - j];
       d = Decryption(d, key);
       if (j != 1)
           cout << "Round " << j << " Ciphertext = " << d << endl;
       else if (j == 1)
           cout << "\nSucceeding Plaintext is: " << d << endl;
   }

   return 0;
}
//function definitions
void splitString(string &x, string &y, string &original)
{
   x = original.substr(0, original.length() / 2);
   y = original.substr(original.length() / 2);
}

void expansionFunction(string &input)
{
   char temp;
   //add two more letters to meet size requirement
   input.append(input, 4, 2);
   temp = input[3];
   input[5] = input[2];
   input[4] = temp;
   input[3] = input[2];
   input[2] = temp;
}

string XORstring(string& str1, string& str2)
{
   string temp = str2;
   for (int i = 0; i < str2.length(); i++)
   {
       temp[i] = (str1[i] ^ str2[i]) + '0';
   }
   return temp;

}

string getSBOXValue(string arr[][8], string val)
{
   int column;
   if (val[0] == '0')
   {
       column = stoi(val.substr(1, val.length()));
       column = binaryToDecimal(column);
       for (int i = 0; i < 8; i++)
           if (i == column)
               return arr[0][i];
   }
   else
   {
       column = stoi(val.substr(1, val.length()));
       column = binaryToDecimal(column);
       for (int i = 0; i < 8; i++)
           if (i == column)
               return arr[1][i];
   }

}
int binaryToDecimal(int n)
{
   int decimal = 0;

   // Initializing base value to 1, i.e 2^0
   int base = 1;

   int temp = n;
   while (temp) {
       int last = temp % 10;
       temp = temp / 10;
       decimal += last * base;
       base = base * 2;
   }

   return decimal;
}
string generateKey()
{
   string randomKey;
   string binary[2] = { "0","1" };
   randomKey.reserve(8);
   srand(time(0));
   cout << "Generating KEY...\n";
   for (int i = 0; i < 9; i++)
   {
       randomKey += binary[rand() % 2];
   }
   return randomKey;


}

string Encryption(string &plaintext, string key)
{
   string left, right, eRight, result, s1, s2, Ln, Rn;
   string S1BOX[2][8] = { "101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011" };
   string S2BOX[2][8] = { "100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100" };

   splitString(left, right, plaintext);

   Ln = right; // Ln = Rn - 1
   eRight = right;
   //use expansion on right side
   expansionFunction(eRight);
   result = XORstring(eRight, key);
   splitString(s1, s2, result);

   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);

   string functionResult = s1result + s2result;
   Rn = XORstring(left, functionResult);
   return Ln + Rn; //L1R2
}

string Decryption(string& cipher, string key)
{
   string Left, Right, eRight, Ln, Rn, result, s1, s2;
   string S1BOX[2][8] = { "101","010","001","110","011","100","111","000","001","100","110","010","000","111","101","011" };
   string S2BOX[2][8] = { "100","000","110","101","111","001","011","010","101","011","000","111","110","010","001","100" };
   //find key
   splitString(Left, Right, cipher);
   Rn = Left;
   eRight = Rn;
   expansionFunction(eRight);
   result = XORstring(eRight, key);
   splitString(s1, s2, result);
   string s1result = getSBOXValue(S1BOX, s1);
   string s2result = getSBOXValue(S2BOX, s2);

   string functionResult = s1result + s2result;
   Ln = XORstring(Right, functionResult);
   return Ln + Rn;
}

OUTPUT:

SDES Encryption|
---------------|
Original Plaintext = 011100100110
Original Key = 010011001

L0 = 011100
R0 = 100110

L1 = R0

XOR
E(Right) = 10101010
K1 = 01001100
--------------------------
11100110
S1 = 1110 = 101
S2 = 0110 = 011

Result from SBOX's is = 101011
L0 XOR f(R0,K1) = 110111(R1)

Ciphertext(L1R1) = 100110110111
----------------------------------------------------------------------------------------

Round SDES
Generating KEY...

Random Key = 110000011
Plaintext = 011100100110

Select number of SDES rounds
-> 4
Generating KEY...
Generating KEY...
Generating KEY...
Generating KEY...
Plaintext = 011100100110

Round 1 Ciphertext = 100110001011
Round 2 Ciphertext = 001011001101
Round 3 Ciphertext = 001101000110
Round 4 Ciphertext = 000110111010

Ciphertext after 4 rounds: 111010000110

Proof by decryption:

Ciphertext: 111010000110

Round 4 Ciphertext = 001101000110
Round 3 Ciphertext = 001011001101
Round 2 Ciphertext = 100110001011

Succeeding Plaintext is: 011100100110

NOTE: findKey() gave away wrong key for decryption hence wrong value was generated.


Related Solutions

I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT