Write a c++ program that will prompt the user to input a string of characters an
ID: 3605888 • Letter: W
Question
Write a c++ program that will prompt the user to input a string of characters and prints an encrypted message back to the user as a string of ascii value base 2 , or it could be the other way round, promt the user to input an ASCII value in base 2 which then prints the decrypted string of character backs to the user.
Here is an example:
Do you want to encrypt or decrypt a message: encrypt
Enter the string to encrypt: pet
01110000 01100101 01110100
Repeat again ? (y or n)? y
Do you want to encrypt or decrypt a message: decrypt
Enter the string to encrypt: 01110000 01100101 01110100
pet
Repeat again ? (y or n)? y
If the user wishes to input a string of text e.g “hello world”, it must be able to successfully decrypt it.
Print an error message when:
The encrypted message (binary) cannot be separated into bytes
The encrypted message (binary) isn’t 1s and 0s
Not using the extended ASCII chart (128-255). Make sure the binary numbers will produce characters within 0-127.
After error message keep prompting using to enter correct binary number.
Create using this 4 function:
1: Create a bin_to_ascii() function that takes a string of 1s and 0s as input and
prints/returns the binary string as an ascii character.
2: Create a ascii_to_bin() function that takes an ascii character as input and print/returns the character as a binary value.
3: Create a get_user_inpu() function that gets the input from the user.
4: Create a check_bin_number() function checks that the user entered a binary number
meeting the three requirements specified above.
No global variables are allowed in this case.
Explanation / Answer
Given below is the code with output. Hope it helps. If it did, please do rate the answer. Thank you.
#include <iostream>
#include <cmath>
using namespace std;
string bin_to_ascii(string bin);
string ascii_to_bin(string ascii);
string get_user_input(string prompt);
bool check_bin_number(string bin);
int main()
{
string bin, ascii;
string conversion;
string ans = "y";
while(ans == "y" || ans == "Y")
{
cout << "Do you want to encrypt or decrypt a message: ";
cin >> conversion;
cin.ignore(); //ignore newline
if(conversion == "encrypt")
{
ascii = get_user_input("Enter the string to encrypt: ");
bin = ascii_to_bin(ascii);
cout << bin << endl;
}
else if(conversion == "decrypt")
{
bin = get_user_input("Enter the string to decrypt: ");
if(check_bin_number(bin))
{
ascii = bin_to_ascii(bin);
cout << ascii << endl;
}
}
cout << "Repeat again (y/n)? ";
cin >> ans;
}
}
char byte_to_char(string bytestr)
{
int byteval = 0;
int power2 = 1;
for(int i = 7; i >= 0; i--)
{
if(bytestr[i] == '1') //mulitply only if the bit is a 1
byteval += power2;
power2 *= 2;//next power of 2
}
return (char)byteval;
}
string bin_to_ascii(string bin)
{
int start = 0;
string ascii = "";
for(start = 0; start < bin.size(); start += 9)
{
string bytestr = bin.substr(start, 8); //extract 8 bits
ascii += byte_to_char(bytestr);
}
return ascii;
}
string ascii_to_bin(string ascii)
{
string bin = "";
for(int i = 0; i < ascii.size(); i++)
{
string bytestr = "";
int n = ascii[i];
for(int j = 1; j <= 8; j++) //8times, repeated division and accumulat remainder
{
int rem = n % 2;
bytestr = to_string(rem) + bytestr;
n /= 2;
}
if(i != 0)
bin += " ";
bin += bytestr;
}
return bin;
}
string get_user_input(string prompt)
{
string input;
cout << prompt;
getline(cin, input);
return input;
}
bool check_bin_number(string bin)
{
int bits = 0;
//chekc if only 0 and 1 is present
for(int i = 0; i < bin.size(); i++)
{
if(bin[i] != '0' && bin[i] != '1')
{
if(bin[i] != ' ')
{
cout << "The encrypted message (binary) isn't 1s and 0s " << endl;
return false;
}
}
else
bits ++;
}
if(bits % 8 != 0)
{
cout << "The encrypted message (binary) cannot be separated into bytes" << endl;
return false;
}
return true;
}
output
Do you want to encrypt or decrypt a message: encrypt
Enter the string to encrypt: pet
01110000 01100101 01110100
Repeat again (y/n)? y
Do you want to encrypt or decrypt a message: decrypt
Enter the string to decrypt: 01110000 01100101 01110100
pet
Repeat again (y/n)? y
Do you want to encrypt or decrypt a message: decrypt
Enter the string to decrypt: 01110000 01100101 01110
The encrypted message (binary) cannot be separated into bytes
Repeat again (y/n)? y
Do you want to encrypt or decrypt a message: decrypt
Enter the string to decrypt: 01110000 01100101 abcd
The encrypted message (binary) isn't 1s and 0s
Repeat again (y/n)? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.