**Write a program (named encrypt.cpp) that allows the user to enter a string (max 80 chars). You will encrypt the string and write a file named secret.dat. You will then write a seperate program (named decrypt.cpp) that will read the secret.dat file and print the original message. Use the following algorithm for the encryption and decryption.**

(The only valid chars are A-Z(upper case), 0-9, and a space)

1) generate a random number between 5 and 13. this will be the "shifting" factor.

2) Simply shift each character (to the right) in the original message by the shifting factor. So if the shifting factor is 8 a D would become L, H becomes P and so forth. If the charcter is a W (you have to wrap to the beginning of the characters), it would be E. and so forth.

3) For a space, look at the previous character of the original message and shift from that character by 4 to the right. So something like

IF YOU , the space would encrypt to J (because it is F shifted 4)

4) terminate the encrypted string with an '&' and then include the position of the spaces after the '&' separated by 'O'. So the last few characters might look something like

FTY&6O12O17

This would tell me that the spaces are at position 6,12,17 in the message.

5) Insert one more 'O' and the shifting factor at the very end of the encrpypted message. So in the above example

FTY&6O12O17O8, would not only tell me where my spaces were, but tell me that the shifting factor was 8

You would use the position of the spaces and the shifting factor to decrypt the message. Simply reverse the encryption.

(The only valid chars are A-Z(upper case), 0-9, and a space)

1) generate a random number between 5 and 13. this will be the "shifting" factor.

2) Simply shift each character (to the right) in the original message by the shifting factor. So if the shifting factor is 8 a D would become L, H becomes P and so forth. If the charcter is a W (you have to wrap to the beginning of the characters), it would be E. and so forth.

3) For a space, look at the previous character of the original message and shift from that character by 4 to the right. So something like

IF YOU , the space would encrypt to J (because it is F shifted 4)

4) terminate the encrypted string with an '&' and then include the position of the spaces after the '&' separated by 'O'. So the last few characters might look something like

FTY&6O12O17

This would tell me that the spaces are at position 6,12,17 in the message.

5) Insert one more 'O' and the shifting factor at the very end of the encrpypted message. So in the above example

FTY&6O12O17O8, would not only tell me where my spaces were, but tell me that the shifting factor was 8

You would use the position of the spaces and the shifting factor to decrypt the message. Simply reverse the encryption.

Instead of using all of the crap after the & sign. I am just decrypting everything up untill the & in the reverse order of how i encrypted it. The message i chose to encrypt was

"HELLO HOW ARE YOU 0600"

and here is a picture of the encrypted message and the decrypted message:

the shift factor is 5.

Heres my code. Can someone tell me what i am doing wrong?

#include <iostream> #include <fstream> using namespace std; int main() { string message, decrypt; int m, d, shift, n = 0; ifstream input; //read encrypted message from "secret.dat" input.open("secret.dat"); getline(input, decrypt, '&'); getline(input, message); input.close(); cout << "encrypted message is: " << decrypt << endl; //get shifting factor m = message.length(); d = decrypt.length(); if (message[m - 2] == 79) shift = (message[m-1] - 48); else if (message[m - 2] != 79) shift = (((message[m - 2] - 48) * 10) + (message[m-1]- 48)); for (int i = 0; i < d; i++) { //decrypt[i] = decrypt[i] - shift; if ((decrypt[i] > 64) && (decrypt[i] < 91))//letters { decrypt[i] = decrypt[i] - shift; while ((decrypt[i] > 57) && (decrypt[i] < 65)) decrypt[i] = decrypt[i] + 26; } if ((decrypt[i] > 47) && (decrypt[i] < 58))//numbers { decrypt[i] = decrypt[i] - shift; while ((decrypt[i] < 48) && (decrypt[i] > 32)) decrypt[i] = decrypt[i] + 10; } if((decrypt[i] > 64) && (decrypt[i] < 91)) if (decrypt[i-1] + 4 == decrypt[i] + shift) decrypt[i] = 32; { if (decrypt[i] + shift > 90) decrypt[i] = 32; if (decrypt[i] + shift < 48) decrypt[i] = 32; if (decrypt[i-1] + 4 == decrypt[i]) decrypt[i] = 32; } if ((decrypt[i] > 47) && (decrypt[i] < 58)) if(decrypt[i-1]==32) decrypt[i+1]=32; } cout << "decrypted message is: " << decrypt << endl; system("pause"); return 1; }