Encryption Program Help!!

The program instructions are as follows:

For years, the Radio Orphan Annie's Secret Society has entrusted their members with special Little Orphan Annie decoder pins so that they can decode secret messages from Orphan Annie. For this program, the system of decoding messages will be modernized so that a C++ program can do the decoding rather than having to rely on a physical pin.

The information to be decoded is contained in a text file. The file will be processed one character at a time. Each character will be decoded by calling an appropriate function and then the decoded character will be displayed


Heres what I have so far but I keep getting a compiler error and I'm stuck. Any hel p would be greatly appreciated!!

Link to encrypted message if you wish: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/encoded_quotes.txt



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <fstream>
#include <iostream>

using namespace std;

bool isspecial(char ch){
   return ch == 20 || ch == 22;
}

char changeLower(char ch){
   return (((ch - 'a') + 27) % 26) + 'A';
}

char changeUpper(char ch){
   return (((ch - 'A') + 25) % 26) + 'a';
}

char changePunct(char ch){
   switch(ch){
   case ',':
       return '9';
   case '"':
       return '8';
   case '!':
       return '7';
   case ';':
       return '6';
   case '?':
       return '5';
   case '\'':
       return '4';
   case '(':
       return '3';
   case ')':
       return '2';
   case '.':
       return '1';
   case '-':
       return '0';
   }
}

char changeDigit(char ch){
   switch(ch){
   case '0':
       return ')';
   case '1':
       return '!';
   case '2':
       return '@';
   case '3':
       return '#';
   case '4':
       return '$';
   case '5':
       return '%';
   case '6':
       return '^';
   case '7':
       return '&';
   case '8':
       return '*';
   case '9':
       return '(';
   }
}

char changeSpecial(char ch){
   if(ch == 20){
       return '\n';
   }
   else if(ch == 22){
       return ' ';
   }
}

int main(){
   ifstream infile1("file1.txt");
   char ch;
      ofstream out("file1.txt");
      cout << "encrypted character--------> " >>ch<<" "<<(int)ch<<endl;	

   if(infile1.is_open() && out.is_open()){
       
	   
	   while(infile1 >> ch){
           if(isupper(ch)){
               out << changeUpper(ch);
           }
           else if(islower(ch)){
               out << changeLower(ch);
           }
           else if(isdigit(ch)){
               out << changeDigit(ch);
           }
           else if(isspecial(ch)){
               out << changeSpecial(ch);
           }
           else if(ispunct(ch)){
               out << changePunct(ch);
           }
           else{
               out << ch;
           }
       }
       infile1.close();
   }
   else{
       cout << "unable to open file" << endl;
   }
   return 0;
}
Last edited on
Line 81: >> should be <<

After fixing this, I get reasonable output except that some of the punctuation looks wrong. If you post a description of how the encryption/decryption is supposed to work, we might be able to diagnose that problem.
@dhayden here is the rules:

Any character in message that was an uppercase character was converted to lowercase and then had 1 subtracted from it. So 'B' became 'a', 'C' became 'b', ... The first uppercase letter was the exception to the rule. The 'A' was simply wrapped around to the end of the alphabet so that it became 'z'.

Any character in message that was a lowercase character was converted to uppercase and then had 1 added it. So 'a' became 'B', 'b' became 'C', ... The last lowercase letter was the exception to the rule. The 'z' was simply wrapped around to the beginning of the alphabet so that it became 'A'.

Any character in message that was a digit ('0' ... '9') was converted as follows:

'0' --> )
'1' --> !
'2' --> @
'3' --> #
'4' --> $
'5' --> %
'6' --> ^
'7' --> &
'8' --> *
'9' --> (
Any character in message that was one of the following punctuation characters was encoded as shown below. Any punctuation that is not shown here was not altered.

, --> '9'
" --> '8'
! --> '7'
; --> '6'
? --> '5'
' --> '4'
( --> '3'
) --> '2'
. --> '1'
- --> '0'
Two white space characters were encoded as non-standard characters:

a blank/space was encoded as an ASCII 22
a newline character was encoded as an ASCII 20
The code also isnt displaying correctly, I need to move line 81 in the while loop and when I do that there is no output. Thanks for your time.
Do you know what the supposed out is ?
I made a few changes and got the following output:
*Be sure to drink your Ovaltine!*

*Aunt Clara had for years labored under the delusion that I was not only
perpetually years old( but also a girl!*

*Immediately( my feet began to sweat as those two fluffy little bunnies
with a blue button eye stared sappily up at me!*

*He looks like a deranged Easter Bunny!*

*HO! HO! HO!*

*The snap of a few sparks( a quick whiff of ozone( and the lamp blazed
forth in unparalleled glory!*

*Oh( look at that& Will you look at that% Isn$t that glorious% It$s!!!
it$s!!! it$s indescribably beautiful& It reminds me of the Fourth of July&*

*Randy( how do the little piggies go%*

*It is a lamp( you nincompoop( but it$s a Major Award! I won it&*

*Yeah( mind power( Swede^ mind power!*

*The line waiting to see Santa Claus stretched all the way back to Terre
Haute! And I was at the end of it!*

*Football% Football% What$s a football% With unconscious will my voice
squeaked out $football$!*

*Okay( get him out of here!*

*A football% Oh no( what was I doing% Wake up( Stupid& Wake up&*

*No& No& I want an Official Red Ryder Carbine)Action Two)Hundred)Shot
Range Model Air Rifle&*

*You$ll shoot your eye out( kid!*

*Of course! Santa! The big man! The head honcho! The connection! Ha( my
mother had slipped up this time!*

*With as much dignity as he could muster( the Old Man gathered up the
sad remains of his shattered major award! Later that night( alone in
the backyard( he buried it next to the garage! Now I could never be sure(
but I thought that I heard the sound of *Taps* being played( gently!*

*Grover Dill& Farkus$s crummy little toadie! Mean& Rotten& His lips
curled over his green teeth!*

*Don$t you touch that& You were always jealous of this lamp!*

*What is the name of the Lone Ranger$s nephew$s horse%*

*Ah!!! Victor& His name is Victor!*

*The heavenly aroma still hung in the house! But it was gone( all gone&
No turkey& No turkey sandwiches& No turkey salad& No turkey gravy&
Turkey Hash& Turkey a la King& Or gallons of turkey soup& Gone( ALL GONE&*

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>


using namespace std;

bool isspecial(char ch) {
  return ch == 20 || ch == 22;
}

char changeLower(char ch) {
  return (((ch - 'a') + 27) % 26) + 'A';
}

char changeUpper(char ch) {
  return (((ch - 'A') + 25) % 26) + 'a';
}

char changePunct(char ch) {
  switch (ch) {
  case ',':
    return '9';
  case '"':
    return '8';
  case '!':
    return '7';
  case ';':
    return '6';
  case '?':
    return '5';
  case '\'':
    return '4';
  case '(':
    return '3';
  case ')':
    return '2';
  case '.':
    return '1';
  case '-':
    return '0';
  }

  return '\0';
}

char changeDigit(char ch) {
  switch (ch) {
  case '0':
    return ')';
  case '1':
    return '!';
  case '2':
    return '@';
  case '3':
    return '#';
  case '4':
    return '$';
  case '5':
    return '%';
  case '6':
    return '^';
  case '7':
    return '&';
  case '8':
    return '*';
  case '9':
    return '(';
  }

  return '\0';
}

char changeSpecial(char ch) {
  if (ch == 20) {
    return '\n';
  }
  else if (ch == 22) {
    return ' ';
  }
  return '\0';
}

int main()
{
  ifstream infile1("file1.txt");
  char ch = '\0';
  ofstream out("output.txt");

  if (!infile1 || !out)
  {
    perror(nullptr);
    return EXIT_FAILURE;
  }
    
  while (infile1.get(ch))
  {
    cout << "original character  -> " << ch << " " << (int)ch << '\n';
    if (isupper(ch)) {
      out << changeUpper(ch);
    }
    else if (islower(ch)) {
      out << changeLower(ch);
    }
    else if (isdigit(ch)) {
      out << changeDigit(ch);
    }
    else if (isspecial(ch)) {
      out << changeSpecial(ch);
    }
    else if (ispunct(ch)) {
      out << changePunct(ch);
    }
    else 
    {
      out << ch;
    }
  }
}
Last edited on
To test your program, I modified it to create decode(char ch) whose job is to decode any character, and decodeString(), which takes an encoded string and then prints the string as well as the decoded string. Finally, I modified main() to call decodeString() with different type of strings. When I run it, I see that it's decoding punctuation characters wrong. Fix this program until it decodes correctly, then replace main() with a version that reads and decodes the file.
$ ./foo
from:   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
to:     "zabcdefghijklmnopqrstuvwxy"
from:   "abcdefghijklmnopqrstuvwxyz"
to:     "BCDEFGHIJKLMNOPQRSTUVWXYZA"
from:   "0123456789"
to:     ")!@#$%^&*("
from:   "!@#$%^&*()"
to:     "732"
from:   ""
to:     ""
from:   ""
to:     ""
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <fstream>


using namespace std;

bool isspecial(char ch) {
  return ch == 20 || ch == 22;
}

char changeLower(char ch) {
  return (((ch - 'a') + 27) % 26) + 'A';
}

char changeUpper(char ch) {
  return (((ch - 'A') + 25) % 26) + 'a';
}

char changePunct(char ch) {
  switch (ch) {
  case ',':
    return '9';
  case '"':
    return '8';
  case '!':
    return '7';
  case ';':
    return '6';
  case '?':
    return '5';
  case '\'':
    return '4';
  case '(':
    return '3';
  case ')':
    return '2';
  case '.':
    return '1';
  case '-':
    return '0';
  }

  return '\0';
}

char changeDigit(char ch) {
  switch (ch) {
  case '0':
    return ')';
  case '1':
    return '!';
  case '2':
    return '@';
  case '3':
    return '#';
  case '4':
    return '$';
  case '5':
    return '%';
  case '6':
    return '^';
  case '7':
    return '&';
  case '8':
    return '*';
  case '9':
    return '(';
  }

  return '\0';
}

char changeSpecial(char ch) {
  if (ch == 20) {
    return '\n';
  }
  else if (ch == 22) {
    return ' ';
  }
  return '\0';
}

char decode(char ch)
{
    if (isupper(ch)) {
	return changeUpper(ch);
    }
    else if (islower(ch)) {
      return changeLower(ch);
    }
    else if (isdigit(ch)) {
      return changeDigit(ch);
    }
    else if (isspecial(ch)) {
      return changeSpecial(ch);
    }
    else if (ispunct(ch)) {
      return changePunct(ch);
    }
    else 
    {
      return ch;
    }
}

void decodeString(string &str)
{
    cout << "from:\t\"" << str << "\"\n";

    for (char &ch : str) {
	ch = decode(ch);
    }
    cout << "to:\t\"" << str << "\"\n";
    
}


int main()
{
    string encoded;
    encoded = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    decodeString(encoded);

    encoded = "abcdefghijklmnopqrstuvwxyz";
    decodeString(encoded);

    encoded = "0123456789";
    decodeString(encoded);

    encoded = "!@#$%^&*()";
    decodeString(encoded);

    encoded = "\22";
    decodeString(encoded);

    encoded = "\20";
    decodeString(encoded);
}
Topic archived. No new replies allowed.