3 Replies - 1837 Views - Last Post: 30 October 2009 - 09:39 AM Rate Topic: -----

#1 jeonsah  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 30-August 09

Decrypting a message

Posted 28 October 2009 - 06:25 PM

Alright, i made a program that encrypts a message. And now i gotta make a program that decrypts that same message. I am having some difficulty with it. Here are the directions for the program:
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.


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:
Posted Image

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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Decrypting a message

#2 jeonsah  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 30-August 09

Re: Decrypting a message

Posted 28 October 2009 - 10:18 PM

bump
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Decrypting a message

Posted 28 October 2009 - 11:39 PM

View Postjeonsah, on 28 Oct, 2009 - 05:25 PM, said:

Heres my code. Can someone tell me what i am doing wrong?
Not following the instructions. If you had, it would work.
Was This Post Helpful? 0
  • +
  • -

#4 Lower Elite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-October 09

Re: Decrypting a message

Posted 30 October 2009 - 09:39 AM

What does the code look like again? Maybe I can look for some problems with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1