Question

In: Computer Science

C++ PROGRAMMING. S-DES: The purpose of this assignment is to implement algorithm for encryption with the...

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.

Solutions

Expert Solution

#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;
}


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
Data Encryption Standard (DES) is insecure because of the size of the encryption key. Advanced Encryption...
Data Encryption Standard (DES) is insecure because of the size of the encryption key. Advanced Encryption Standard (AES) is the current NIST standard for encryption and is used in most applications. Explain the differences between the following concepts as they apply to both algorithms. Concept DES AES S-BOX Permutation Key Size
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
1. The purpose of this assignment is to teach you about encryption and for you to...
1. The purpose of this assignment is to teach you about encryption and for you to be able to encrypt and protect your important documents before you store them in the cloud. 2. Please be careful with the encryption. If you loose or forget the password of your encrypted file, you will not be able to recover the documents. 3. Take Backup of your System. It is important to take backup anytime you are installing or removing a program. It...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
Project Description: In groups of four students, implement (New Encryption Algorithm) character oriented and apply it...
Project Description: In groups of four students, implement (New Encryption Algorithm) character oriented and apply it using any programming Language that you are familiar with to encrypt and decrypt your name. Your work should cover: 1. Create the algorithm and explain (encryption-decryption) using block diagram. 2. Mention whether will you use the keys or not in your algorithm. 3- If you will use keys, explain how to generate the keys. 2. Display the generated keys 3. Encrypt your name 4....
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT