Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 y code for this encryptiondecryption 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 generateTranspositionMatrixconst string& key
vector transpositionMatrix;
string numStr;
for char c : key
if isdigitc
numStr c;
else if numStr.empty
transpositionMatrix.pushbackstoinumStr;
numStr.clear;
if numStr.empty
transpositionMatrix.pushbackstoinumStr;
sorttranspositionMatrixbegin transpositionMatrix.end greater;
return transpositionMatrix;
remove spaces from the input text
string removeSpacesconst string& text
string result;
for char c : text
if isspacec
result c;
return result;
add padding to the plaintext
string addPaddingconst string& text, int numRows
string paddedText text;
int padding numRows textlength numRows;
if padding numRows
for int i ; i padding; i
paddedText X; Add X as padding character
return paddedText;
encrypt plaintext using row transposition
string encryptRowTranspositionconst string& plaintext, const string& key
string modifiedText removeSpacesplaintext;
vector transpositionMatrix generateTranspositionMatrixkey;
int numRows transpositionMatrix.size;
string paddedText addPaddingmodifiedText numRows;
int numCols paddedText.length numRows;
string ciphertext;
Create a matrix to store the plaintext
vector matrixnumRows vectornumCols;
int index ;
for int col ; col numCols; col
for int row : transpositionMatrix
matrixrow col paddedTextindex;
Read out the ciphertext column by column
for int row ; row numRows; row
for int col ; col numCols; col
ciphertext matrixrowcol;
return ciphertext;
Function to perform row transposition decryption
string decryptRowTranspositionconst string& ciphertext, const string& key
string modifiedText removeSpacesciphertext;
vector transpositionMatrix generateTranspositionMatrixkey;
int numRows transpositionMatrix.size;
string paddedText addPaddingmodifiedText numRows;
int numCols paddedText.length numRows;
string plaintext;
Create a matrix to store the ciphertext
vector matrixnumRows vectornumCols;
int index ;
Fill the matrix column by column, following the key order
for int col : transpositionMatrix
for int row ; row numRows; row
matrixrowcol paddedTextindex;
Read out the plaintext from the matrix in numerical column order
for int col ; col numCols; col
for int row ; row numRows; row
plaintext matrixrowcol;
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 choice;
cout Playfair Cipher" endl Row Transposition Cipher" endl "Enter: ;
cin choice;
cin.ignore;
if choice
cout "Playfair Cipher" endl;
int choice;
cout Encryption" endl Decryption" endl "Enter: ;
cin choice;
cin.ignore;
string plaintext;
cout "Enter plaintext: ;
getlinecin plaintext;
cout "Plain text: plaintext endl;
string key;
cout "Enter key: ;
getlinecin key;
string modText plainTextplaintext;
char matrix;
createPlayfairMatrixkey matrix;
string result;
if choice
for sizet i ; i modText.size; i
char a modTexti;
char b i modText.size modTexti : X;
result encryptDiagrammatrix a b;
else if choice
for sizet i ; i modText.size; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started