JUST DO NUMBER THREE C++ THANK YOU. Write decryption for the first function. The
ID: 3586897 • Letter: J
Question
JUST DO NUMBER THREE C++ THANK YOU. Write decryption for the first function.
The Caesar cipher is a simple and widely known encryption technique. The action of a Caesar cipher is to replace each letter in the unencrypted message (called "plaintext" in cryptography) with a different one a fixed number of places down the alphabet. The resulting message is called "ciphertext". For example: Plaintext: Hello, world! Ciphertext: Ebiil, tloia! The cipher illustrated above uses a left shift of three (or a right shift of 23), so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. Write a C++ function to implement the Caesar cipher, using the prototype: precond: numpos>=0 and rshift <=26 postcond: the return value is the encrypted version of orig using the caesar(rshift) cipher string enc rypt Caesar(string orig , int rshift ); Your function should preserve case, and any non-alphabetic characters should be left unchanged. For example, encryptCaesar("Hello, world!",23) = "Ebiil, tloia!". You may write additional helper functions if you wish (and it will help you in later tasks). For example, one useful function may be to shift a single character some number of positions. Remember: Separation of Concerns!
In the Caesar cipher, each letter is always shifted by the same value. What if we shifted each letter by a different value? A cipher known as the Vigenere cipher consists of several Caesar ciphers in sequence with different shift values. For example, we might shift the first letter of the plaintext to the right by five, the second by 17, etc. The sequence is defined by a keyword where each letter defines the shift value. If a letter in the keyword is the nth letter in the alphabet, then each ciphertext letter will be the corresponding plaintext letter shifted to the right by n-1. For example, suppose that the plaintext to be encrypted is: Hello, world! ...and the person sending the message chooses the keyword "cake". The first letter of the keyword is 'c', and 'c' is the third letter of the alphabet. That means we shift the first letter of the plaintext to the right by 3-1 = 2, which makes the first letter of the ciphertext 'J'. Then repeat for the remaining letters. If we reach the end of the keyword, go back and use the first letter of the keyword. Following these steps, the resulting ciphertext is: Jevpq, wyvnd! Write a C++ function to implement the Vigenere cipher. You may make your own prototype though it should return the encrypted string as above. Again, your function should preserve case, and any non-alphabetic characters should be left unchanged.
3)))))Write decryption functions corresponding to the above.
Explanation / Answer
#include<iostream>
#include<string>
std::string encryption(std::string plain,std::string key)
{
int key_len = key.length();//taking key_length
int plain_len = plain.length();//taking plain text length
std::string encr;//final cipher text to be generated
int i,j;
for(i=0,j=0;i<plain_len;i++)//Iterating each character in the plain text
{
char p = plain[i];
int inc = key[j%key_len] - 'a';//Fetching increment value from the key
if((p>='a' && p<='z') )//Validating if the character is small letter
{
encr.push_back((p-'a'+inc)%26+'a');//Converting according to incremet
j++;
}
else if((p>='A' && p<='Z'))//Validating if the character is captial letter
{
encr.push_back((p-'A'+inc)%26+'A');
j++;
}
else
{
encr.push_back(p);//If character is not alpahabet, assign it intact
}
}
return encr;
}
std::string decryption(std::string plain,std::string key)//decryption function
{
int key_len = key.length();
int plain_len = plain.length();
std::string encr;
int i,j;
for(i=0,j=0;i<plain_len;i++)
{
char p = plain[i];
int inc = key[j%key_len] - 'a';
if((p>='a' && p<='z') )
{
encr.push_back((p-'a'-inc)%26+'a');
j++;
}
else if((p>='A' && p<='Z'))
{
encr.push_back((p-'A'-inc)%26+'A');
j++;
}
else
{
encr.push_back(p);
}
}
return encr;
}
int main()
{
std::string a = encryption("Hello, world","cake");
std::string b = decryption(a,"cake");
std::cout << a << std::endl;
std::cout << b << std::endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.