CS 1410 Assignment 2 XOR – Permutation Cipher Due Before: Saturday, April 21st,
ID: 3709390 • Letter: C
Question
CS 1410
Assignment 2
XOR – Permutation Cipher
Due Before: Saturday, April 21st, 2018, 11:30 p.m.
The purpose of this program is to help you to write programs that can process information at the bit level. This program must be a C++ Win32 Console Application Project (that uses the empty project option). All programs will be graded using Visual Studio 2015. You will want to compile and run your code using this IDE.
25 points per day will be deducted if this is turned in late.
Instructions: Write a C++ program that will:
Input a 7 bit key from a file. This file must be entitled exactly “key.txt”.
Input a plaintext message of any size from a file entitled “plaintext.txt”.
For every character in the plaintext message, perform the following tasks to encrypt it:
Compute its base 10 ASCII value
Convert the base 10 number into an equivalent 7 bit unsigned binary number
Permute the bits of the 7 bit binary number according to the following permutation:
permuated_bits[0] = original_bits[2]
permuated_bits[1] = original_bits[5]
permuated_bits[2] = original_bits[6]
permuated_bits[3] = original_bits[4]
permuated_bits[4] = original_bits[1]
permuated_bits[5] = original_bits[3]
permuated_bits[6] = original_bits[0]
XOR the permuted_bits with the key to compute the ciphertext bits.
Output to the console screen the 7 ciphertext bits for that plaintext character.
Output a Carriage Return (endl) so that there are only 7 ciphertext bits per line.
Note: To conserve memory and to partially simulate how this would be implemented at the machine level, you must use the C++ bitset in order to store the following:
Plaintext bits
Key bits
Cipertext bits
C++ Bitset references:
https://www.geeksforgeeks.org/c-bitset-and-its-application/
http://www.cplusplus.com/reference/bitset/bitset/
Visual Studio 2015 Download link: https://www.uhd.edu/computing/labs-technology-centers/academic-computing-labs/Pages/acl-dreamspark.aspx
C++ Win32 Console Application Reference: https://msdn.microsoft.com/en-us/library/ms235629(v=vs.140).aspx
Sample Run 1:
plaintext.txt = XOR Permuation Cipher.
key.txt = 1010101
______________________________________________________
Sample Run 2:
plaintext.txt = abc123$% QWERT
key.txt = 1111110
This program needs to be able to work with plaintext files of any size and any 7 bit key.
Turn in your .cpp file into the Blackboard dropbox for assignment 2. Only turn in 1 .cpp file entitled “Your Name.cpp” (using your personal name). This file will look like the following:
Do not wait until the last minute to do this assignment. Get started as soon as you read this. Writing an algorithm will help you to code this. If you are experiencing logic errors, do not forget to use your debugger. Tutoring is available in S735.
Explanation / Answer
===========================================================================================
Bitset stores values in reverse order (binary reverse order). I interpreted the permutation code in question as, the operation given on bitset bits. If the operations given on theoratical bits, we should change our permutation code.
Please replace this code in the program and Let me know it gave the output as you expect. If you have some trouble in doing it. just send me the input and output you expected. I will change it for you and I will post the entire code again.
-----------------------------------------------------------------
// Permutation - Given in question
permuated_bits[0] = original_bits[4];
permuated_bits[1] = original_bits[1];
permuated_bits[2] = original_bits[0];
permuated_bits[3] = original_bits[2];
permuated_bits[4] = original_bits[5];
permuated_bits[5] = original_bits[3];
permuated_bits[6] = original_bits[6];
-------------------------------------
Input:
plaintext.txt abc123$% QWERT
key.txt 1111110
The output for above code is:
0101010
0101100
0101000
1101011
1101101
1101001
1100110
1100010
1101110
0111011
0110001
0110010
0111101
0110111
0110111
============================================================================================
YourName.cpp
------------------------------
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" //For Visual Studio. Can be commented if not required.
//#include <bits/stdc++.h>
#include <iostream>
#include <bitset>
#include <fstream>
using namespace std;
#define SIZE 7 // for 7-bit string
int main()
{
char ch; // char to read single char
const char *keyfile = "key.txt";
const char *textfile = "plaintext.txt";
//char keyarray[SIZE+1]; // To store the 7 bit-key and '' at the end
string keystr; //To read the key from the file.
ifstream fin; //To read input files.
// Read key file and save key in bitset
fin.open(keyfile, ios::in); //ios::in file open for reading
if (!fin) {
cout << "Unable to open input file : " << keyfile << endl;
return 1;
}
// read the file if opened.
getline(fin, keystr); // read a line(key) from the file.
//fin.read(keyarray, SIZE); // Read 7-bit key to key array.
//keyarray[7] = ''; //Making Null for last char.
keystr.substr(0, 7);
//cout << "Key Read: " << keystr.substr(0, 7) << endl;
// key bit string.
bitset<SIZE> key(keystr.substr(0, 7)); //Sending only 7 chars to avoid any other letters after the 7bit key
//cout << "Bit Key: " << key << endl;
fin.close(); // closing key.txt file.
// Reading the plain text file.
fin.open(textfile, ios::in);
if (!fin) {
cout << "Unable to open input file : " << textfile << endl;
return 1;
}
//cout << "File Contents: " << endl;
bitset<SIZE> original_bits; // To store the character read from file.
bitset<SIZE> permuated_bits; // To store the permuted bit char
// Read the file character by character and encrypt the char.
while (!fin.eof()) { // read till we reach the end of file.
fin >> noskipws >> ch; //noskipws = no skip white space. i.e. it will read the spaces or tabs etc.
//cout << ch << endl;
bitset<SIZE> original_bits((int)ch); //Create bitset for the read char
//cout << "Original Bits" << original_bits << endl;
// Permutation - Given in question
permuated_bits[0] = original_bits[2];
permuated_bits[1] = original_bits[5];
permuated_bits[2] = original_bits[6];
permuated_bits[3] = original_bits[4];
permuated_bits[4] = original_bits[1];
permuated_bits[5] = original_bits[3];
permuated_bits[6] = original_bits[0];
// XOR and output to console the cipher
cout << (permuated_bits ^ key) << endl;
}
fin.close(); // close the plaintext.txt
system("pause");
return 0;
}
====================================================================================
Sample Run 1:
Plaintext.txt
XOR Permuation Cipher.
Key.txt:
1010101
Output:
1111001
0100000
1001001
1010111
1011001
0010010
1001011
0110010
0011010
0010011
1011010
0110011
0100010
1100010
1010111
0000001
0110011
1011011
1110011
0010010
1001011
1100110
1100110
Sample Run 2:
plaintext.txt = abc123$% QWERT
key.txt = 1111110
Output:
0111000
1101000
0101000
0110100
1100100
0100100
1111101
0111101
1111100
0110010
0100011
0111011
1100010
1110011
1110011