Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I have posted this question for couple times, but still has not got answer ye

ID: 3721074 • Letter: H

Question

Hi I have posted this question for couple times, but still has not got answer yet. Please someone help to write and add in the description in this Columnar Transposition cipher? The encryption part is working. Just FYI: I can not use string or class in this program Thanks for the time.

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include<cstring>

using namespace std;

void cipher(int i, int c);

int findMin();

char arr[22][22], darr[22][22], emessage[111], retmessage[111], key[55];

char temp[55], temp2[55];

int k = 0;

int main()

{ int i, j, lenKey, emlen, flag = 0;

int r, c, index, rows;

char letter;

cout << "Enter the key ";

cin.getline(key, 55);

cout << " Enter message to be ciphered ";

cin.getline(emessage, 111);

strcpy(temp, key);

lenKey = strlen(key);

k = 0;

for (i = 0;; i++)

{

if (flag == 1)

break;

for (j = 0; key[j] != NULL; j++) //?? the message

{

if (emessage[k] == NULL)

{ flag = 1; arr[i][j] = '-';// if the there is a space at end of the martrix, use "-" instead of space }

else {

arr[i][j] = emessage[k++];

}

}

}

r = i;

c = j;

char answs;

cout << "would you like to encrypt or decrypt?" << endl;

cin >> answs;

answs = toupper(answs);

if (answs == 'E')

{

k = 0;

for (i = 0; i < lenKey; i++)

{

index = findMin();//

cipher(index, r);

}

emessage[k] = '';

for (i = 0; emessage[i] != NULL; i++) {

}

cout <<"Encrypted message is: " << emessage << endl;

}

if (answs == 'D')

{

k = 0;

for (i = 0; i < lenKey; i++)

{

index = findMin();//find the min.

}

emessage[k] = '';

cout << "decrypted message is ";

for (i = 0; emessage[i] != NULL; i++)

{

}

cout << emessage << endl; }

system("pause");

return 0;

}

void cipher(int i, int r)//encrypt the msg

{

int j; for (j = 0; j < r; j++)

{

{ emessage[k++] = arr[j][i]; }

}

}

int findMin()

{

int i, j, min, index;

min = temp[0];

index = 0;

for (j = 0; temp[j] != NULL; j++)

{

if (temp[j] < min) { min = temp[j]; index = j;

}

}

temp[index] = 123;

return (index);

}

Explanation / Answer

A)

#include<bits/stdc++.h>
using namespace std;
string const key = "HACK";
map<int,int> keyMap;
void setPermutationOrder()
{
for(int i=0; i < key.length(); i++)
{
keyMap[key[i]] = i;
}
}
string decryptMessage(string cipher)
{
int col = key.length();

int row = cipher.length()/col;
char cipherMat[row][col];
for (int j=0,k=0; j<col; j++)
for (int i=0; i<row; i++)
cipherMat[i][j] = cipher[k++];
int index = 0;
for( map<int,int>::iterator ii=keyMap.begin(); ii!=keyMap.end(); ++ii)
ii->second = index++;
char decCipher[row][col];
map<int,int>::iterator ii=keyMap.begin();
int k = 0;
for (int l=0,j; key[l]!=''; k++)
{
j = keyMap[key[l++]];
for (int i=0; i<row; i++)
{
decCipher[i][k]=cipherMat[i][j];
}
}
string msg = "";
for (int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
if(decCipher[i][j] != '_')
msg += decCipher[i][j];
}
}
return msg;
}
int main(void)
{
string msg = "Geeks for Geeks";  
setPermutationOrder();
string cipher = encryptMessage(msg);
cout << "Encrypted Message: " << cipher << endl;
cout << "Decrypted Message: " << decryptMessage(cipher) << endl;
return 0;
}