Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have a code encrypting a message, but unsure how to decrypt it with the same passcode. Code in C++ below #include #include using namespace std;

Have a code encrypting a message, but unsure how to decrypt it with the same passcode. Code in C++ below

#include #include using namespace std; char encryptLetter(char normalLetter, char passcodeLetter){ int shift = 0; if('a'<=passcodeLetter && passcodeLetter<='z') shift = passcodeLetter - 'a'; if('A'<=passcodeLetter && passcodeLetter<='Z') shift = passcodeLetter - 'A'; char encryptedLetter = normalLetter + shift; if('a'<=normalLetter && normalLetter<='z'){ if(encryptedLetter>'z') encryptedLetter = 'a' + (encryptedLetter-'z'-1); } else if ('A'<=normalLetter && normalLetter<='Z'){ if(encryptedLetter>'Z') encryptedLetter = 'A' + (encryptedLetter-'Z'-1); }else{ encryptedLetter = normalLetter; } return encryptedLetter; } char decryptLetter(char normalLetter, char passcodeLetter){ int shift = 0; if('a'<=passcodeLetter && passcodeLetter<='z') shift = passcodeLetter - 'a'; if('A'<=passcodeLetter && passcodeLetter<='Z') shift = passcodeLetter - 'A'; char decryptedLetter = normalLetter + shift; if('a'<=normalLetter && normalLetter<='z'){ if(decryptedLetter>'z') decryptedLetter = 'a' + (decryptedLetter+'z'+1); } else if ('A'<=normalLetter && normalLetter<='Z'){ if(decryptedLetter>'Z') decryptedLetter = 'A' + (decryptedLetter+'Z'+1); }else{ decryptedLetter = normalLetter; } return decryptedLetter; } void convertOpposite(string& str) { int ln = str.length(); // Conversion according to ASCII values for (int i = 0; i < ln; i++) { if (str[i] >= 'a' && str[i] <= 'z') // Convert lowercase to uppercase str[i] = str[i] - 32; else if (str[i] >= 'A' && str[i] <= 'Z') // Convert uppercase to lowercase str[i] = str[i] + 32; } } int main(){ int choice = 0; cout << "Enter a number, 1 to encrypt, 2 to decrypt" << endl; cin >> choice; cin.ignore (); if (choice == 1) { string msg, pass, encrypted; cout << "Enter the message to encrypt: "; getline(cin, msg, ' '); convertOpposite(msg); cout << "Enter passcode: "; getline(cin, pass, ' '); int i, j; for (int i = 0; i < msg.length(); i++) { if (('a' <= msg[i] && msg[i] <= 'z') || ('A' <= msg[i] && msg[i] <= 'Z')) { encrypted.push_back(encryptLetter(msg[i], pass[j])); j = (j + 1) % pass.length(); } else encrypted.push_back(msg[i]); } cout << "Encrypted message: " << encrypted; } else if (choice ==2){ string msg, pass, decrypted; cout << "Enter the message to decrypt: "; getline(cin, msg, ' '); convertOpposite(msg); cout << "Enter passcode: "; getline(cin, pass, ' '); int i, j; for (int i = 0; i < msg.length(); i++) { if (('a' <= msg[i] && msg[i] <= 'z') || ('A' <= msg[i] && msg[i] <= 'Z')) { decrypted.push_back(decryptLetter(msg[i], pass[j])); j = (j - 1) % pass.length(); } else decrypted.push_back(msg[i]); } cout << "decrypted message: " << decrypted; } return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions