Question

Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

Study the VIGENÈRE CIPHER and implemented it with c++

1.In your program, you should have two functions: encryption and decryption.

2. Use any key you like. (Don't use deceptive in the slides)

3. choose a sentence or a paragraph you like as the plaintext.

I have the code I just need the implementation in a different way

// C++ code to implement Vigenere Cipher
#include<bits/stdc++.h>
using namespace std;

// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string str, string key)
{
   int x = str.size();

   for (int i = 0; ; i++)
   {
       if (x == i)
           i = 0;
       if (key.size() == str.size())
           break;
       key.push_back(key[i]);
   }
   return key;
}

// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
   string cipher_text;

   for (int i = 0; i < str.size(); i++)
   {
       // converting in range 0-25
       char x = (str[i] + key[i]) %26;

       // convert into alphabets(ASCII)
       x += 'A';

       cipher_text.push_back(x);
   }
   return cipher_text;
}

// This function decrypts the encrypted text
// and returns the original text
string originalText(string cipher_text, string key)
{
   string orig_text;

   for (int i = 0 ; i < cipher_text.size(); i++)
   {
       // converting in range 0-25
       char x = (cipher_text[i] - key[i] + 26) %26;

       // convert into alphabets(ASCII)
       x += 'A';
       orig_text.push_back(x);
   }
   return orig_text;
}

// Driver program to test the above function
int main()
{
   string str = "GEEKSFORGEEKS";
   string keyword = "AYUSH";

   string key = generateKey(str, keyword);
   string cipher_text = cipherText(str, key);

   cout << "Ciphertext : "
       << cipher_text << "\n";

   cout << "Original/Decrypted Text : "
       << originalText(cipher_text, key);
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I've taken your code and edited it in a manner that suits the question. Hope this helps :)


 // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; //Converts a string to uppercase string upper_string(string str) {     for(int i=0;str[i]!='\0';i++)   {               if (str[i] >= 'a' && str[i] <= 'z') //checking for lowercase characters                   str[i] = str[i] - 32; //converting lowercase to uppercase       }       return str; } // This function generates the key in // a cyclic manner until it's length isi'nt // equal to the length of original text string generateKey(string str, string key) { int x = str.size(); for (int i = 0; ; i++) { if (x == i) i = 0; if (key.size() == str.size()) break; key.push_back(key[i]); } return key; } // This function returns the encrypted text // generated with the help of the key string encryption(string str, string key) { string cipher_text; for (int i = 0; i < str.size(); i++) { // converting in range 0-25 char x = (str[i] + key[i]) %26; // convert into alphabets(ASCII) x += 'A'; cipher_text.push_back(x); } return cipher_text; } // This function decrypts the encrypted text // and returns the original text string decryption(string cipher_text, string key) { string orig_text; for (int i = 0 ; i < cipher_text.size(); i++) { // converting in range 0-25 char x = (cipher_text[i] - key[i] + 26) %26; // convert into alphabets(ASCII) x += 'A'; orig_text.push_back(x); } return orig_text; } // Driver program to test the above function int main() { //string str = "ATTACKATDAWN"; //string keyword = "LEMON"; string str,keyword,cipher_text; int ch; cout<<"\nEnter the keyword:"; cin>>keyword; keyword=upper_string(keyword); cout<<"\nEnter 1 to Encrypt plain text.\nEnter 2 to decrypt encrypted text.\n"; cin>>ch; cout<<"\nEnter the string:"; cin>>str; str=upper_string(str); string key = generateKey(str, keyword); if(ch==1) { cipher_text = encryption(str, key); cout << "Ciphertext : "<< cipher_text << "\n"; } else if(ch==2) { cout << "Original/Decrypted Text : "<< decryption(str, key); } else { cout<<"\nInvalid Option!!!"; } return 0; }

Outputs:

Enter the keyword: lemon Enter 1 to Encrypt plain text. Enter 2 to decrypt encrypted text. 1 Enter the string:attackatdawn Ci

Enter the keyword: lemon Enter 1 to Encrypt plain text. Enter 2 to decrypt encrypted text. 2 Enter the string:lxfopvefrnhr Or

Add a comment
Know the answer?
Add Answer to:
Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • 12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print...

    12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT