Question

C++ Program, Decode a Secret Message.

Your country is at war and your enemies are using a secret code to communicate with each other.You have managed to intercept a message that reads asfollows:

:mmZdxZmx]Zpgy

The message is obviously encrypted using the enemy's secret code.You have just learned that their encryption method is based upon the ASCII code.Appendix 3 of your book shows the ACII character set.Individual characters in a string are encoded using this system. For example, the letter "A" isencoded using the number 65 and "B" is encoded using the number 66.

Your enemy's secret code takes each letter of the message and encrypts it as follows:

If(OriginalChar + Key > 126) then EncryptedChar = 32 + ((OriginalChar + Key ) - 127)

else EncryptedChar = (OriginalChar + Key)

For example, if the enemy used Key=10 then the message "Hey" would be encrypted as:

Character

ASCII Code

H

72

e

101

y

121

Encrypted H = (72 + 10) = 82 = R in ASCII

Encrypted e = (101 + 10) = 111 = o in ASCII

Encrypted y = (121 + 10) - 127 = 36 = $ in ASCII

Consequently, "Hey" would be transmitted as "Ro$".

Write a program that decrypts the intercepted message.You only know that the key used is a number between 1 and 100.Your program should try to decodethe message using all possible keys between 1 and 100.When you try the valid key, the message will make sense.For all other keys the message willappear as gibberish.

Use C++-Strings for this assignment.

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

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char * argv[])
{
string decryptedString(const string&secret, int key);

string secretMsg = ":mmZ\dxZmx]Zpgy";

for (int key = 1; key < 101; ++key)
{
cout << "Key " << key << " - "
<< decryptedString(secretMsg, key)
<< endl;
}
//if none were decrypted sucessfully or no decrypted string have meaning, update the filter
//FOUND @key88: "Attack at dawn!"
cout << endl;
cin.get();
return 0;
}

//---------------------------------------------------------
bool filter(const char &chr)
{
return (toupper(chr) >= 'A' &&toupper(chr) <= 'Z') || //a-z A-Z
(chr >= '0' &&chr <= '9')|| //0-9
(chr == ' ' || chr == 't' || chr == 'n') || //white spaces
(chr == ',' || chr == '.'|| //punctuations
chr == ':' || chr == ';' ||
chr == ''' || chr == '"' ||
chr == '?' || chr == '!');
}
//---------------------------------------------------------
string decryptedString(const string&secret, int key)
{
string decoder = "";
for (int i = 0; i < secret.length(); i++)
{
if (filter(secret[i] - key))
decoder += secret[i] - key;
else if (filter(secret[i] - key + 127 - 32))
decoder += secret[i] - key + 127 - 32;
}
if (decoder.length() == secret.length()) //All encrypted chars must be decrypted
return decoder;
return decoder = "";
}

//------------------------OUTPUT-----------------------
Key 1 -
Key 2 -
Key 3 - 7jjWYauWjuZWmdv
Key 4 -
Key 5 -
Key 6 -
Key 7 -
Key 8 -
Key 9 -
Key 10 -
Key 11 -
Key 12 -
Key 13 -
Key 14 -
Key 15 -
Key 16 -
Key 17 -
Key 18 -
Key 19 -
Key 20 -
Key 21 -
Key 22 -
Key 23 -
Key 24 -
Key 25 -
Key 26 -
Key 27 -
Key 28 -
Key 29 -
Key 30 -
Key 31 -
Key 32 -
Key 33 -
Key 34 - wKK8:BV8KV;8NEW
Key 35 - vJJ79AU7JU:7MDV
Key 36 -
Key 37 - tHH57?S5HS85KBT
Key 38 -
Key 39 -
Key 40 -
Key 41 -
Key 42 -
Key 43 -
Key 44 - mAA.08L.AL1.D;M
Key 45 -
Key 46 -
Key 47 -
Key 48 -
Key 49 -
Key 50 -
Key 51 -
Key 52 -
Key 53 -
Key 54 -
Key 55 -
Key 56 -
Key 57 -
Key 58 -
Key 59 -
Key 60 -
Key 61 -
Key 62 -
Key 63 -
Key 64 -
Key 65 -
Key 66 -
Key 67 -
Key 68 -
Key 69 -
Key 70 -
Key 71 -
Key 72 -
Key 73 -
Key 74 -
Key 75 -
Key 76 -
Key 77 -
Key 78 -
Key 79 -
Key 80 -
Key 81 -
Key 82 -
Key 83 -
Key 84 -
Key 85 -
Key 86 -
Key 87 - Buubdl!bu!ebxo"
Key 88 - Attack at dawn!
Key 89 -
Key 90 -
Key 91 -
Key 92 -
Key 93 -
Key 94 -
Key 95 -
Key 96 -
Key 97 -
Key 98 - 7jjWYauWjuZWmdv
Key 99 -
Key 100 -

answered by: sans
Add a comment
Know the answer?
Add Answer to:
C++ Program, Decode a Secret Message.
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
  • Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to reada...

    Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to readable messages. To do so: Initialize a variable called decoded as an empty string . Use a for loop to loop across all characters of a presumed string variable called encoded Inside the loop, convert the character to another character o Get the unicode code point for the character (using ord o Subtract the value of key to that code point (which should be an...

  • Use C++ forehand e receiver creates a public key and a secret key as follows. Generate...

    Use C++ forehand e receiver creates a public key and a secret key as follows. Generate two distinct primes, p andq. Since they can be used to generate the secret key, they must be kept hidden. Let n-pg, phi(n) ((p-1)*(q-1) Select an integer e such that gcd(e, (p-100g-1))-1. The public key is the pair (e,n). This should be distributed widely. Compute d such that d-l(mod (p-1)(q-1). This can be done using the pulverizer. The secret key is the pair (d.n)....

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • 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...

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