Question: I need assistance with m y code for this encryption / decryption . I am using c + + and the encryption method of choice

I need assistance with my code for this encryption/decryption. I am using c++ and the encryption method of choice is the row transposition. So far everything is running correctly but I need some help with the reading of the output once the user has encrypted it. I dont know why its not reaading it out correctly but please someone help.
code below :
vector generateTranspositionMatrix(const string& key){
vector transpositionMatrix;
string numStr;
for (char c : key){
if (isdigit(c)){
numStr += c;
}
else if (!numStr.empty()){
transpositionMatrix.push_back(stoi(numStr));
numStr.clear();
}
}
if (!numStr.empty()){
transpositionMatrix.push_back(stoi(numStr));
}
sort(transpositionMatrix.begin(), transpositionMatrix.end(), greater());
return transpositionMatrix;
}
//remove spaces from the input text
string removeSpaces(const string& text){
string result;
for (char c : text){
if (!isspace(c)){
result += c;
}
}
return result;
}
//add padding to the plaintext
string addPadding(const string& text, int numRows){
string paddedText = text;
int padding = numRows -(text.length()% numRows);
if (padding != numRows){
for (int i =0; i < padding; ++i){
paddedText +='X'; // Add 'X' as padding character
}
}
return paddedText;
}
//encrypt plaintext using row transposition
string encryptRowTransposition(const string& plaintext, const string& key){
string modifiedText = removeSpaces(plaintext);
vector transpositionMatrix = generateTranspositionMatrix(key);
int numRows = transpositionMatrix.size();
string paddedText = addPadding(modifiedText, numRows);
int numCols = paddedText.length()/ numRows;
string ciphertext;
// Create a matrix to store the plaintext
vector> matrix(numRows, vector(numCols));
int index =0;
for (int col =0; col < numCols; ++col){
for (int row : transpositionMatrix){
matrix[row -1][col]= paddedText[index++];
}
}
// Read out the ciphertext column by column
for (int row =0; row < numRows; ++row){
for (int col =0; col < numCols; ++col){
ciphertext += matrix[row][col];
}
}
return ciphertext;
}
// Function to perform row transposition decryption
string decryptRowTransposition(const string& ciphertext, const string& key){
string modifiedText = removeSpaces(ciphertext);
vector transpositionMatrix = generateTranspositionMatrix(key);
int numRows = transpositionMatrix.size();
string paddedText = addPadding(modifiedText, numRows);
int numCols = paddedText.length()/ numRows;
string plaintext;
// Create a matrix to store the ciphertext
vector> matrix(numRows, vector(numCols));
int index =0;
// Fill the matrix column by column, following the key order
for (int col : transpositionMatrix){
for (int row =0; row < numRows; ++row){
matrix[row][col -1]= paddedText[index++];
}
}
// Read out the plaintext from the matrix in numerical column order
for (int col =0; col < numCols; ++col){
for (int row =0; row < numRows; ++row){
plaintext += matrix[row][col];
}
}
return plaintext;
}
//skeleton of code -- asks user which algorithm they want and then goes on to ask if they want to encrypt or decrypt either while providing the plaintext and key of their choice
int main()
{
cout << "Choose an algorithm to use : "<< endl;
int choice1;
cout <<"1. Playfair Cipher" << endl <<"2. Row Transposition Cipher" << endl << "Enter: ";
cin >> choice1;
cin.ignore();
if (choice1==1){
cout << "Playfair Cipher" << endl;
int choice2;
cout <<"1. Encryption" << endl <<"2. Decryption" << endl << "Enter: ";
cin >> choice2;
cin.ignore();
string plaintext;
cout << "Enter plaintext: ";
getline(cin, plaintext);
cout << "Plain text: "<< plaintext << endl;
string key;
cout << "Enter key: ";
getline(cin, key);
string modText = plainText(plaintext);
char matrix[5][5];
createPlayfairMatrix(key, matrix);
string result;
if (choice2==1){
for (size_t i =0; i < modText.size(); i +=2){
char a = modText[i];
char b =(i +1< modText.size())? modText[i +1] : 'X';
result += encryptDiagram(matrix, a, b);
}
}
else if (choice2==2){
for (size_t i =0; i < modText.size(); i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!