-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW06.rtf
73 lines (72 loc) · 2.87 KB
/
HW06.rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{\rtf1\ansi\ansicpg1252\cocoartf2513
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 AndaleMono;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww28380\viewh17800\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs38 \cf0 /*\
\
\pard\pardeftab720\partightenfactor0
\cf0 Your country is at war and your enemies are using a secret code to com- municate with each other. You have managed to intercept a message that reads as follows:\
:mmZ\\dxZmx]Zpgy\
\
The message is obviously encrypted using the enemy\'92s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter \'93A\'94 is encoded using the number 65 and \'93B\'94 is encoded using the number 66.\
Your enemy\'92s secret code takes each letter of the message and encrypts it as follows:\
If (OriginalChar + Key > 126) then\
EncryptedChar = 32 + ((OriginalChar + Key) \'96 127)\
Else\
EncryptedChar = (OriginalChar + Key)\
For example, if the enemy uses Key = 10 then the message \'93Hey\'94 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 = 32 + ((121 + 10) \'96 127) = 36 = $ in ASCII\
Consequently, \'93Hey\'94 would be transmitted as \'93Ro$.\'94\
Write a program that decrypts the intercepted message. The ASCII codes for the unencrypted message are limited to the visible ASCII characters. You only know that the key used is a number between 1 and 100. Your program should try to decode the 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 will appear as gibberish.\
\
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\cf0 \
*/\
\
#include <iostream>\
using namespace std;\
\
void decrypt(char*, const char*, int);\
\
int main(int argc, char * argv[])\
\{\
char *secret = ":mmZ\\dxZmx]Zpgy";\
char *output = new char[strlen(secret) + 1];\
\
for (int key = 1; key <= 100; ++key)\
\{\
decrypt(output, secret, key);\
cout << "Key " << key << " - " << output << endl;\
\}\
//FOUND at key 7: "Attack at dawn!"\
\
cout << endl;\
delete [] output;\
\
return 0;\
\}\
\
//---------------------------------------------------------\
void decrypt(char *output, const char *secret, int key)\
\{\
char *out = output;\
for (const char *ptr = secret; *ptr; ptr++, out++)\
\{\
if (*ptr + key > 126)\
*out = *ptr + key - 95;\
else\
*out = *ptr + key;\
\}\
*out = '\\0';\
\}}