the polyalphabetic cipher that uses a word as the key for shifting the plaintext
ID: 3738117 • Letter: T
Question
the polyalphabetic cipher that uses a word as the key for shifting the plaintext and creating ciphertext. When the keyword is smaller than the plaintext, the keyword is repeated. A variation on this algorithm uses the keyword to shift the plaintext, and if the keyword is smaller than the plaintext, the keyword is used, followed by the reverse of the keyword, and then the keyword again until all letters in the plaintext are shifted. For example, given the following plaintext and keyword:
plaintext = ‘computerscience’
keyword = ‘code’
we would encrypt the plaintext by shifting each letter by the corresponding letter of the keyword followed by the reverse of the keyword:
computerscience
codeedoccodeedo
c would be shifted by c, o would be shifted by o, m would be shifted by d, p would be shifted by e, the u would be shifted by e, and so on.
The resulting ciphertext of the example above would be:
ecptywstuqlirfs
Write a function that implements this variation on the polyalphabetic cipher. The parameters of the function will be the plaintext and the keyword. The function will return the ciphertext. Write another function that deciphers this variation of hte polyalphabetic cipher.
Test your functions with the following examples:
polyReverse("computerscience","code")
"ecptywstuqlirfs"
unPolyReverse(“ecptywstuqlirfs”,”code)
“computer science”
polyReverse(“supercalifragilistic”,”poppins”)
“hietzpsdvngpuxawhiqp”
unPolyReverse(“hietzpsdvngpuxawhiqp”, “poppins”)
“supercalifragilistic"
Explanation / Answer
This Problem can be solved using Vigenere Cipher Algorithm.In the below program, the lower case alphabets are converted to upper case using "strlen()", for implementing the optimized algorithm. The keyword is converted to the required pattern in three steps, first creating the replica of keyword and then reversing it and finally combining the original and reversed one. This new keyword is extended till the length of the message.Finally, the Algorithm is implemented using the message and the keyword.
The Algorithm uses the idea of ASCII values of 'A, B, C ....Z' from '0 to 25'. and makes the correspondence between Keyword and the Message. The mathematical equation for encryption is Ei = (Pi + Ki) mod 26 ( where Pi is Message text and Ki is Keyword text) and similarly for decryption is Pi = (Ei – Ki + 26) mod 26.
The functions are written in C++, along with the main function.
---------------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<string.h>
using namespace std;
void polyReverse(char msg[], char key[]){
strupr(msg);
strupr(key);
int msgLen = strlen(msg),i, j;
char newKey[msgLen], encryptedMsg[msgLen];
char rkey[strlen(key)]; // creating copy of keyword
strcpy(rkey, key);
strrev(rkey); // reversing the string
strcat(key,rkey); // finally combining the both
int keyLen = strlen(key);
//generating new key to meet the length of the message
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '';
//encryption algorithm
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
encryptedMsg[i] = '';
strlwr(msg);
strlwr(key);
strlwr(newKey);
strlwr(encryptedMsg);
cout<<"Original Message: "<<msg;
cout<<" Key: "<<key;
cout<<" New Generated Key: "<<newKey;
cout<<" Encrypted Message: "<<encryptedMsg;
}
void unPolyReverse(char msg[], char key[]){
strupr(msg);
strupr(key);
int msgLen = strlen(msg),i, j;
char newKey[msgLen], decryptedMsg[msgLen];
char rkey[strlen(key)];
strcpy(rkey, key);
strrev(rkey);
strcat(key,rkey);
int keyLen = strlen(key);
//generating new key
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '';
//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((msg[i] - newKey[i]) + 26) % 26) + 'A';
decryptedMsg[i] = '';
strlwr(msg);
strlwr(key);
strlwr(newKey);
strlwr(decryptedMsg);
cout<<"Original Message: "<<msg;
cout<<" Key: "<<key;
cout<<" New Generated Key: "<<newKey;
cout<<" Decrypted Message: "<<decryptedMsg;
}
int main(){
char msg[] = "supercalifragilistic";
char key[] = "poppins";
char cryp[] = "hietzpsdvngpuxawhiqp";
polyReverse(msg, key);
cout<<endl<<endl;
unPolyReverse(cryp, key);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.