In: Computer Science
C++ PROGRAMMING.
S-DES: The purpose of this assignment is to implement algorithm for encryption with the simplified DES-type algorithm.
· Item #1. Write a C++ program that performs one round of the simplified DES-type algorithm. Test your code with a plaintext = 011100100110 and K = 010011001.
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>
using namespace std;
string SDESEncryption(string, string, int);
string findKey(string, int);
string functionF(string, string);
string XOR(string, string);
string S1Box(string);
string S2Box(string);
string randomString(int);
string header();
int main()
{
        //Declare variables
        //initializing plaintext and key so they aren't the same.
        string plaintext = "011100100110";
        cout    << "Generating your plaintext and key pair." << endl
                        << "Please wait..." << endl;
        string key =  "010011001";
        string ciphertext;
        string decryption;
        int numrounds = 1;
        int rounds;
        //Print out unnecessary header
        cout    << header();
        //Print out original plaintext P
        cout    << "\tOriginal Plaintext:\t" << plaintext << endl
                        << "\tKey:\t\t\t" << key << endl;
        //Do [numrounds] rounds of SDES on plaintext P ( max 1)
        cout << "\nEncryption:\n";
        for(int i = 0; i < numrounds; i++)
        {
                plaintext = SDESEncryption(key, plaintext, i+1);
                cout << "\tC (after round " << i + 1 << "):\t" << plaintext << endl;
                rounds = i + 1;
        }
        
        return 0;
}
string SDESEncryption(string key, string plaintext, int round)
{
    string Li;
        string Ri;
        string Ln;
        string Rn;
        string K;
        string f;
        
        K = findKey(key, round);
        
        Li.append(plaintext, 0, 6);
        Ri.append(plaintext, 6, 6);
        
        Ln = Ri;
        
        f.append(functionF(Ri, K));
        
        Rn.append(f);
        Rn = XOR(Li, f);
        
        return (Ln + Rn);
}
string findKey(string key, int round)
{
        
        string K;
        if(round == 1)
                K.append(key, 0, 8);
        else if(round == 2)
                K.append(key, 1, 8);
        else if(round == 3)
        {
                K.append(key, 2, 7);
                K.append(key, 0, 1);
        }
        else if(round == 4)
        {
                K.append(key, 3, 6);
                K.append(key, 0, 2);
        }
        return K;
}
string functionF(string R, string K)
{
        char tmp;
        string s1;
        string s2;
        
        R.append(R, 4, 2);
        tmp = R[3];
        R[5] = R[2];
        R[4] = tmp;
        R[3] = R[2];
        R[2] = tmp;
        
        R = XOR(R, K);
        s1.append(R, 0, 4);
        s2.append(R, 4, 4);
        
        return S1Box(s1) + S2Box(s2);
}
string XOR(string x, string y)
{
        for(int i = 0; i < x.length(); i++)
        {
                if(x[i] == y[i])
                        x[i] = '0';
                else if(x[i] != y[i])
                        x[i] = '1';
        }
        return x;
}
string S1Box(string s1)
{
        
        string row1[8] = {"101", "010", "001", "110", "011", "100", "111", "000"};
        string row2[8] = {"001", "100", "110", "010", "000", "111", "101", "011"};
        int column = 0;
        
        if(s1[0] == '0')
        {
                
                if(s1[1] == '1')
                        column += 4;
                if(s1[2] == '1')
                        column += 2;
                if(s1[3] == '1')
                        column += 1;
                
                return row1[column];
        }
        
        else if(s1[0] == '1')
        {
                
                if(s1[1] == '1')
                        column += 4;
                if(s1[2] == '1')
                        column += 2;
                if(s1[3] == '1')
                        column += 1;
        
                return row2[column];
        }
        else
                
                return "ERROR";
}
string S2Box(string s2)
{
        
        string row1[8] = {"100", "000", "110", "101", "111", "001", "011", "010"};
        string row2[8] = {"101", "011", "000", "111", "110", "010", "001", "100"};
        int column = 0;
        
        if(s2[0] == '0')
        {
                
                if(s2[1] == '1')
                        column += 4;
                if(s2[2] == '1')
                        column += 2;
                if(s2[3] == '1')
                        column += 1;
                
                return row1[column];
        }
        
        else if(s2[0] == '1')
        {
                if(s2[1] == '1')
                        column += 4;
                if(s2[2] == '1')
                        column += 2;
                if(s2[3] == '1')
                        column += 1;
                return row2[column];
        }
        else
                return "ERROR";
}
string randomString(int length)
{
        srand(time(0));
        int randomNumber;
        string randomBit;
        string randomString;
        for(int k = 0; k < length; k++)
        {
                randomNumber = rand() % 2;
                if(randomNumber == 0)
                        randomBit = "0";
                else if(randomNumber == 1)
                        randomBit = "1";
                randomString.append(randomBit);
        }
        return randomString;
}
string header()
{
        string header = "*****************\t\t*****************\n\t\t    SDES DEMO\t\t\t\n*****************\t\t*****************\n";
        return header;
}
