in and out stream

I am having to do a caesars cipher program and when I encrypt a file it works fine but the problem is when I decrypt.

Example
I have a file called "plain" that has ABCDE in it.
When I encrypt, i name the new file, "cipher" and use a key of 5. It stores FGHIJ in it. When I try to decrypt the "cipher" file, it just hangs. So I stop the program. If I create a file called "cipher" and put FGHIJ in it, and decrypt it with a key of 5, it works perfectly. I had everything working perfect then I went through to try and simplify the code and now this is happening. Please help!

closed account (48T7M4Gy)
Maybe showing us your code would be a good move?
clear
Last edited on
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

#include <cstdlib>
#include <ctype.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

/* ============================================================================
Function :write my info
Parameters :none
Return :none void
Description :writes my info
============================================================================
*/
void outPut();
/* ============================================================================
Function :encrypt
Parameters :input and output
Return :none void
Description :encrypts plaintext
============================================================================
*/
void doEncrypt(char input[32], char output[32]);
/* ============================================================================
Function :decrypt
Parameters :none
Return :none void
Description :decrypts ciphertext
============================================================================
*/
void doDecrypt(char input[32], char output[32]);

ifstream in_stream;//global variables
ofstream out_stream;
char orig_symbol;
char new_symbol;

int main()
{
	char response;
	char input[32];
	char output[32];
	
	outPut();//calling function

	cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";//ask what they want to do
	cin >> response;
	
	while (response != 'e' && response != 'd' && response != 'D' && response != 'E')//if they put in wrong letter, keep prompting
	{
	cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";
	cin >> response; 
	}
	
	if(response == 'e' || response == 'E')//if they want to encrypt
	{
		cout << "Enter the name of your input file you want to encrypt: ";
		cin >> input;
		cout << "Enter the name of the output file to write the ciphertext: ";
		cin >> output;
		in_stream.open(input);//open stream
		out_stream.open(output);//open stream
		if(in_stream.fail())//if opening fails
        	{
                	cout << "Input file opening failed.\n";
                	exit(EXIT_FAILURE);
        	}
		doEncrypt(input, output);//calling function
		out_stream.close( );//close stream
        	in_stream.close( );//close stream
	}
	if(response == 'd' || response == 'D')//if they want to decrypt
        {
                cout << "Enter the name of your input file you want to decrypt: ";
                cin >> input;
		cout << "Enter the name of the output file to write the plaintext: ";
                cin >> output;
		in_stream.open(input);//open stream
                out_stream.open(output);//open stream
		if(in_stream.fail())//if opeining fails
        	{
                	cout << "Input file opening failed.\n";
                	exit(EXIT_FAILURE);
        	}
        	doDecrypt(input, output);//calling function
		out_stream.close( );//close stream
        	in_stream.close( );//close stream
        }
	return 0;
}
void doEncrypt(char input[32], char output[32])
{
	cout << "YOU WANT TO ENCRYPT" << endl;
	int shiftvalue;
	cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";
	cin >> shiftvalue;
	in_stream.get(orig_symbol);//get character
	while(orig_symbol != '\n')//while the character is not on next line
        {
                if(isupper(orig_symbol)||islower(orig_symbol)||isdigit(orig_symbol))//if the character is uppercase,lowercase, or numeric
                {
                        if(isupper(orig_symbol))
                        {
                                new_symbol=((orig_symbol+shiftvalue - 65) % 26) + 65;//65 is the offset of ASCII
                        	out_stream.put(new_symbol);
			}
                        if(islower(orig_symbol))
                        {
                                new_symbol=((orig_symbol+shiftvalue - 97) % 26) + 97;//97 is the offset of ASCII
                        	out_stream.put(new_symbol);
			}
                        if(isdigit(orig_symbol))
                        {
                                new_symbol=((orig_symbol+shiftvalue - 48) % 10) + 48;//48 is the offset of ASCII
                  		out_stream.put(new_symbol);
			}
                }
		in_stream.get(orig_symbol);                
        }
}
void doDecrypt(char input[32], char output[32])
{
	cout << "YOU WANT TO DECRYPT" << endl;
	int shiftvalue = 0;
	int tempvalue;
	cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";
	cin >> shiftvalue; 
	in_stream.get(orig_symbol);
	while(orig_symbol != '\n')//while the character is not on next line
        {
                if(isupper(orig_symbol)||islower(orig_symbol)||isdigit(orig_symbol))//if the character is uppercase,lowercase, or numeric
                {
                        if(isupper(orig_symbol))
                        {
				if(shiftvalue >= 26)
				{
					tempvalue = shiftvalue % 26;	
				}else
				{
					tempvalue = shiftvalue;
				}
                                new_symbol=((26 + orig_symbol-tempvalue - 65) % 26) + 65;//65 is the offset of ASCII
                                out_stream.put(new_symbol);
			}
                        if(islower(orig_symbol))
                        {
                                if(shiftvalue >= 26)
                                {
                                        tempvalue = shiftvalue % 26;
                                }else
                                {
                                        tempvalue = shiftvalue;
                                }
				new_symbol=((26 +orig_symbol-tempvalue - 97) % 26) + 97;//97 is the offset of ASCII
                                out_stream.put(new_symbol);
			}
                        if(isdigit(orig_symbol))
                        {
                                if(shiftvalue >= 10)
                                {
                                        tempvalue = shiftvalue % 10;
                                }else
                                {
                                        tempvalue = shiftvalue;
                                }
				new_symbol=((10 +orig_symbol-tempvalue - 48) % 10) + 48;//48 is the offset of ASCII
                                out_stream.put(new_symbol);
				
		        }
		}
		in_stream.get(orig_symbol);        	
	}
}
Last edited on
closed account (48T7M4Gy)
Well, as a suggestion I would get the file read and write process working as a separate test program.

You currently have 188 lines of code with mountains of processes going on which makes debugging basically a waste of time. You should narrow the problem down into a simple test module and then incorporate that into the main code. When it works.
Lol, I think we are in the same class. Anyway, this is the decryption key is as follows:

((26 + int(c) - key - 65) % 26 + 65)

26 is # of letters.
int(c) is taking your letter and turning it into its ascii code.
subtract your key.
65 is the ascii code for capital 'A'.
%26 is to loop those letters.

change 65 to whatever ascii code you need to use.
Last edited on
The following version works, but it's very basic:

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ============================================================================
Name: homework4.cpp
Author: Robert Ehrlish
Version:
Copyright: 2017
Description: This program encrypts and decrypts
============================================================================ */
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>

/* ============================================================================
Function: write my info
Parameters: none
Return: none void
Description: writes my info
============================================================================
*/
void outPut();
/* ============================================================================
Function: encrypt
Parameters: input and output
Return: none void
Description: encrypts plaintext
============================================================================
*/
void doEncrypt(std::ifstream& in_stream, std::ofstream& out_stream);
/* ============================================================================
Function: decrypt
Parameters: none
Return: none void
Description: decrypts ciphertext
============================================================================
*/
void doDecrypt(std::ifstream& in_stream, std::ofstream& out_stream);

void askForFileNames(char encdec, std::ifstream& in_stream,
                     std::ofstream& out_stream);

int main()
{

   outPut();

   std::cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";
   std::string response;
   std::cin >> response;

   while (response.at(0) != 'e' && response.at(0) != 'E'
          && response.at(0) != 'd' && response.at(0) != 'D')
   {
      std::cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";
      std::cin >> response;
   }

   std::ifstream in_stream;
   std::ofstream out_stream;
   askForFileNames(response.at(0), in_stream, out_stream);

   if(response.at(0) == 'e' || response.at(0) == 'E') //if they want to encrypt
   {
      doEncrypt(in_stream, out_stream);
   }

   if(response.at(0) == 'd' || response.at(0) == 'D') //if they want to decrypt
   {
      doDecrypt(in_stream, out_stream);
   }

   out_stream.close();
   in_stream.close();

   return 0;
}

void doEncrypt(std::ifstream& in_stream, std::ofstream& out_stream)
{
   std::cout << "YOU WANT TO ENCRYPT" << std::endl;
   std::cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";
   int shiftvalue {0};
   std::cin >> shiftvalue;

   char orig_symbol {}, new_symbol {};
   while(in_stream >> std::noskipws >> orig_symbol)
   {
      new_symbol = orig_symbol;
      if(std::isupper(orig_symbol))
      {
         //65 is the offset of ASCII
         new_symbol=((orig_symbol+shiftvalue - 65) % 26) + 65;
      }
      if(std::islower(orig_symbol))
      {
         //97 is the offset of ASCII
         new_symbol=((orig_symbol+shiftvalue - 97) % 26) + 97;
      }
      if(std::isdigit(orig_symbol))
      {
         //48 is the offset of ASCII
         new_symbol=((orig_symbol+shiftvalue - 48) % 10) + 48;
      }
      out_stream << new_symbol;
   }
}

void doDecrypt(std::ifstream& in_stream, std::ofstream& out_stream)
{
   std::cout << "YOU WANT TO DECRYPT" << std::endl;
   std::cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";
   int shiftvalue = 0;
   std::cin >> shiftvalue;

   char orig_symbol {}, new_symbol {};
   while(in_stream >> std::noskipws >> orig_symbol)
   {
      int tempvalue {};
      new_symbol = orig_symbol;
      if(isupper(orig_symbol))
      {
         if(shiftvalue >= 26)
         {
            tempvalue = shiftvalue % 26;
         } else {
            tempvalue = shiftvalue;
         }
         //65 is the offset of ASCII
         new_symbol=((26 + orig_symbol-tempvalue - 65) % 26) + 65;
      }
      if(islower(orig_symbol))
      {
         if(shiftvalue >= 26)
         {
            tempvalue = shiftvalue % 26;
         }else
         {
            tempvalue = shiftvalue;
         }
         //97 is the offset of ASCII
         new_symbol=((26 +orig_symbol-tempvalue - 97) % 26) + 97;
      }
      if(isdigit(orig_symbol))
      {
         if(shiftvalue >= 10)
         {
            tempvalue = shiftvalue % 10;
         }else
         {
            tempvalue = shiftvalue;
         }
         //48 is the offset of ASCII
         new_symbol=((10 +orig_symbol-tempvalue - 48) % 10) + 48;
      }
      out_stream << new_symbol;
   }
}

void outPut()
{
   std::cout << "+---------------------------------------------------------+\n";
   std::cout << "|            Computer Science and Engineering             |\n";
   std::cout << "|             CSCE 1030 - Computer Science I              |\n";
   std::cout << "|  Robert Ehrlish    rte0023    robertehrlish@my.unt.edu  |\n";
   std::cout << "+---------------------------------------------------------+\n";
}

void askForFileNames(char encdec, std::ifstream& in_stream,
                     std::ofstream& out_stream)
{
   std::string input, output;
   if(encdec == 'e') {
      std::cout << "Enter the name of your input file you want to encrypt: ";
      std::cin >> input;
      std::cout << "Enter the name of the output file to write the ciphertext: ";
      std::cin >> output;
   } else {
      std::cout << "Enter the name of your input file you want to decrypt: ";
      std::cin >> input;
      std::cout << "Enter the name of the output file to write the plaintext: ";
      std::cin >> output;
   }

   in_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
   try {
      in_stream.open(input, std::ios_base::in);
   } catch(...) {
      std::cerr << "Exception opening file: \"" << input << "\""
                << std::strerror(errno) << '\n';
   }

   out_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
   try {
      out_stream.open(output, std::ios_base::out);
   } catch(...) {
      std::cerr << "Exception opening file: \"" << output << "\""
                << std::strerror(errno) << '\n';
   }
}


Note: code updated after having eliminated a try-catch block used for debugging.
Last edited on
Topic archived. No new replies allowed.