In this assignment you will write a program that encrypts a text file. You will
ID: 3594792 • Letter: I
Question
In this assignment you will write a program that encrypts a text file. You will use the following encryption scheme.
Ask the user for the name of the original file.
Ask the user for the name of the output file.
Ask the user for the encryption key, n.
Read n2 characters from the file into the n rows and n columns of a 2-dimensional array.
Transpose the array. (Exchange the rows and columns.)
Write the characters from the array to an output file.
Repeat steps 4 – 6 until the input file has been read.
This same program can be used to decrypt a file that has been encrypted in this way.
Your program should have functions for performing the following tasks.
1) Reading the characters from the file into the array. This function should be passed the file stream, the array and the key.
2)Transposing the array. This function should be passed the array and the key.
3) Writing the characters from the array to the file. This function should be passed the file stream, the array and the key.
Your program should get the filenames, open the files and close the files in main.
Your program should also:
Give detailed directions and warnings to the user.
Be readable with appropriate documentation and formatting. (Be sure to include carefully written pre and post conditions for each of your functions.)
Turn in a hard copy of your source file and an electronic copy of the source (.cpp) file to the D2L dropbox.
Explanation / Answer
#include<iostream>
#include <string>
#include<fstream>
#include <bitset>
#define HASH_SIZE 128
using namespace std;
void MatrixInplaceTranspose(char *A, int r, int c)
{
int size = r*c - 1;
char t; // holds element to be replaced, eventually becomes next element to move
int next; // location of 't' to be moved
int cycleBegin; // holds start of cycle
int i; // iterator
bitset<HASH_SIZE> b; // hash to mark moved elements
b.reset();
b[0] = b[size] = 1;
i = 1; // Note that A[0] and A[size-1] won't move
while (i < size)
{
cycleBegin = i;
t = A[i];
do
{
// Input matrix [r x c]
// Output matrix 1
// i_new = (i*r)%(N-1)
next = (i*r) % size;
swap(A[next], t);
b[i] = 1;
i = next;
} while (i != cycleBegin);
// Get Next Move (what about querying random location?)
for (i = 1; i < size && b[i]; i++)
;
cout << endl;
}
}
int main()
{
string input, output;
std::cout << "Enter the name of an original text file: ";
cin >> input; // get c-string
std::ifstream is(input); // open file
std::cout << "Enter the name of an output text file: ";
cin >> output; // get c-string
std::ofstream isout(output); // open file
int n;
cout << "Enter encryption key, n: ";
cin >> n;
char *arr = (char*)malloc(sizeof(char)*n*n);
int i = 0, j = 0;
char c;
while (is.get(c)) // loop getting single characters
{
std::cout << c;
*(arr + i*n + j) = c;
i++; j++;
if (i == n && j == n)
{
MatrixInplaceTranspose(arr, n, n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
isout << *(arr + i*n + j);
}
}
}
is.close(); // close file
system("pause");
return 0;
}
please create file input.txt for original text and output.txt for encrypted data.