I have been working on a simple program that will perform encryption/decryption based on a simple 1:1 pattern. You enter the program, choose the encryption pattern you prefer (which currently only supports one option) and then choose to encrypt or decrypt your text. When you choose to encrypt, you are prompted to enter text.
The program then reads the character inputs that you enter, and uses a repeating switch loop to translate each letter into its encrypted letter (a becomes z, b becomes y, etc) and then prints the encrypted text to the screen. at the same time, it writes the encrypted text to an external .txt file. this part of the program works fine. However, when I go to decrypt my text, i have a problem. The decryptor reads the text from the external file into an array, and then translates it one character at a time, in the same fashion as the encryptor. however, it will not write back to the txt file. i have also tried to write the decrypted text to a new txt file, and the program crashes at decryption time.
when i am attempting to write the decrypted text back to the original txt file, the decrypted text does print to the screen, but does not write to the txt file. no error messages or crashes are presented/occur in this case.
i know that my coding style is imperfect, and that it doesn't follow all accepted formatting protocol. I am only looking for an idea as to why the second write function does not carry out. Any suggestions would be greatly appreciated. Thanks in advance.
Main menu .cpp file: (only needed if you intend to run the file yourself)
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
|
#include<iostream>
#include <fstream>
#include <string>
#include"HalfFold.h"
using namespace std;
int main( void )
{ //open main
char pattern='a'; //encryption pattern selection
char encDec='a'; //encryption/decryption selector
int incOrDec=0;
Code myCode;
cout<< "Hello, welcome to the encryptor!" << endl;
cout<< "When typing a sentence for encryption, remember to use \n a comma (,) instead of a space between words."<<endl;
cout<< "When you have finished using an encryption method, enter 1"<<endl<<endl;
for (int codeSelect=1; codeSelect>0; codeSelect++){ //open code select For
cout<< "Please select cryptographic pattern:" << endl << "a= geocache style half-fold" << endl;
cout<< "b= random pattern" <<endl;
cin>>pattern;
cout<<endl;
switch (pattern){ //open Switch
case'a':
cout<< "Select operation: \n e=encrypt d=decrypt"<< endl;
while (incOrDec==0)
incOrDec++;
cin>> encDec;
if(!encDec== 'e' || !encDec== 'd')
{
cout<<"Invalid Entry. Re-enter selection"<<endl;
incOrDec=0;
}
if (encDec=='e')
{
myCode.foldEnc();
}
if (encDec=='d')
{
myCode.foldDec();
}
break;
default:
cout<< "Please review the list of options and make a new selection."<<endl;
break;
} //close pattern selection Switch
} //close encryption pattern For loop
return 0;
} //close main
|
Header file containing the encryption/decryption program (THIS IS WHERE THE ERROR APPARENTLY LIES):
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Code
{ //open class Code
public:
void foldEnc( void ){ //open fold Encrypt method
fstream myfile;
myfile.open("crypto.txt");
cout<< endl<< "Enter your text for encryption:"<< endl;
char in='a'; //letter to swap
for(int x=1; x>0 ;x++){ //open infinite For loop
cin >> in;
switch (in){ //open Switch loop
case'_':
cout<< " ";
myfile<< " " ;
break;
case',':
cout<<",";
myfile<< "," ;
break;
case'a':
cout<< "z";
myfile<< "z" ;
break;
case'b':
cout<< "y";
myfile<< "y" ;
break;
case'c':
cout<< "x";
myfile<< "x" ;
break;
case'd':
cout<< "w";
myfile<< "w" ;
break;
case'e':
cout<< "v";
myfile<< "v" ;
break;
case'f':
cout<< "u";
myfile<< "u" ;
break;
case'g':
cout<< "t";
myfile<< "t" ;
break;
case'h':
cout<< "s";
myfile<< "s" ;
break;
case'i':
cout<< "r";
myfile<< "r" ;
break;
case'j':
cout<< "q";
myfile<< "q" ;
break;
case'k':
cout<< "p";
myfile<< "p" ;
break;
case'l':
cout<< "o";
myfile<< "o" ;
break;
case'm':
cout<< "n";
myfile<< "n" ;
break;
case'n':
cout<< "m";
myfile<< "m" ;
break;
case'o':
cout<< "l";
myfile<< "l" ;
break;
case'p':
cout<< "k";
myfile<< "k" ;
break;
case'q':
cout<< "j";
myfile<< "j" ;
break;
case'r':
cout<< "i";
myfile<< "i" ;
break;
case's':
cout<< "h";
myfile<< "h" ;
break;
case't':
cout<< "g";
myfile<< "g" ;
break;
case'u':
cout<< "f";
myfile<< "f" ;
break;
case'v':
cout<< "e";
myfile<< "e" ;
break;
case'w':
cout<< "d";
myfile<< "d" ;
break;
case'x':
cout<< "c";
myfile<< "c" ;
break;
case'y':
cout<< "b";
myfile<< "b" ;
break;
case'z':
cout<< "a";
myfile<< "a" ;
break;
case'1':
cout<< endl <<endl << "Exiting half fold encryptor."<<endl<<endl<<endl;
x=-1;
myfile<< "1" ;
myfile.close();
break;
default:
cout<< "Invalid entry";
break;
} //close Switch
} //close infinite For
} //close Fold Decrypt method
void foldDec( void )
{ //open method FoldDec
fstream myfile;
myfile.open("crypto2.txt");
int input=0;
int index=0;
char inputArray[1000];
while ( !myfile.eof() )
{
inputArray[index]= myfile.get();
index++;
}
myfile.close();
fstream myDecode;
myDecode.open("cryptoDecoded.txt");
for (input=0; input>-1; input++)
{ //open For input loop
switch (inputArray[input]){ //open Switch loop
case'_':
cout<< " ";
myDecode<< " " ;
break;
case',':
cout<<" ";
myDecode<< " " ;
break;
case'a':
cout<< "z";
myDecode<< "z" ;
break;
case'b':
cout<< "y";
myDecode<< "y" ;
break;
case'c':
cout<< "x";
myDecode<< "x" ;
break;
case'd':
cout<< "w";
myDecode<< "w" ;
break;
case'e':
cout<< "v";
myDecode<< "v" ;
break;
case'f':
cout<< "u";
myDecode<< "u" ;
break;
case'g':
cout<< "t";
myDecode<< "t" ;
break;
case'h':
cout<< "s";
myDecode<< "s" ;
break;
case'i':
cout<< "r";
myDecode<< "r" ;
break;
case'j':
cout<< "q";
myDecode<< "q" ;
break;
case'k':
cout<< "p";
myDecode<< "p" ;
break;
case'l':
cout<< "o";
myDecode<< "o" ;
break;
case'm':
cout<< "n";
myDecode<< "n" ;
break;
case'n':
cout<< "m";
myDecode<< "m" ;
break;
case'o':
cout<< "l";
myDecode<< "l" ;
break;
case'p':
cout<< "k";
myDecode<< "k" ;
break;
case'q':
cout<< "j";
myDecode<< "j" ;
break;
case'r':
cout<< "i";
myDecode<< "i" ;
break;
case's':
cout<< "h";
myDecode<< "h" ;
break;
case't':
cout<< "g";
myDecode<< "g" ;
break;
case'u':
cout<< "f";
myDecode<< "f" ;
break;
case'v':
cout<< "e";
myDecode<< "e" ;
break;
case'w':
cout<< "d";
myDecode<< "d" ;
break;
case'x':
cout<< "c";
myDecode<< "c" ;
break;
case'y':
cout<< "b";
myDecode<< "b" ;
break;
case'z':
cout<< "a";
myDecode<< "a" ;
break;
case'1':
cout<< endl<< endl<< "Exiting half fold decryptor."<<endl<<endl<<endl;
input=-2;
myDecode<< "1";
myDecode.close();
break;
default:
cout<< "Invalid entry";
break;
} //close Switch
} //close For input loop
} //close method foldDec
}; //close class Code
|
Thanks again,
Lukipaela