Question
i need help fixing this code so that it incrypts the message correctly and after its done encrypting the message it asks the user to
i need help fixing this code so that it incrypts the message correctly and after its done encrypting the message it asks the user to input a message to be decrypted with the code that was already entered.
Write a new C++ program named Vigerene.
Here is the Code.org site with the widget
Code.org (Links to an external site.)
Write the C++ program, Vigerene, which will prompt the user for a string message and a string secret code. The code must be shorter than the message. Then write a function which takes the message and code as parameters, and converts the two strings into a encrypted code using the rules on the code.org website. Return the encrypted message and print it in the Main class.
Now write a new function which takes in an encrypted message and a secret code as two parameters and decrypts the the message and returns it to Main for printing.
Here is sample output:
Enter your messge: my name is rick Enter your secret code: helloworld TBKYOHSQTVGVTNY
Enter your encrypted message TBKYOHSQTVGVTNY MY NAME IS RICK
code:
#include
using namespace std;
string encrypt(string message, string code) { string encrypted = ""; int j = 0; for (int i = 0; i < message.length(); i++) { char c = message[i]; if (isalpha(c)) { int shift = code[j] - 'a'; char encrypted_char = (c + shift - 'a') % 26 + 'A'; encrypted += encrypted_char; j = (j + 1) % code.length(); } else { encrypted += c; } } return encrypted; }
string decrypt(string message, string code) { string decrypted = ""; int j = 0; for (int i = 0; i < message.length(); i++) { char c = message[i]; if (isalpha(c)) { int shift = code[j] - 'a'; char decrypted_char = (c - shift - 'A' + 26) % 26 + 'a'; decrypted += decrypted_char; j = (j + 1) % code.length(); } else { decrypted += c; } } return decrypted; }
int main() { string message, code; cout << "Enter your message: "; getline(cin, message); cout << "Enter your secret code: "; getline(cin, code); string encrypted = encrypt(message, code); // encrypt the message using the code cout << "Encrypted message: " << encrypted << endl; // print the encrypted message string decrypted = decrypt(encrypted, code); // decrypt the encrypted message using the code cout << "Decrypted message: " << decrypted << endl; // print the decrypted message return 0; }
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