encryption / decryption program

This is a discussion on encryption / decryption program within the C++ Programming forums, part of the General Programming Boards category; I am following this book and an asignment but I just cant figure the decryption part out! can anyone help? ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    encryption / decryption program

    I am following this book and an asignment but I just cant figure the decryption part out! can anyone help?

    Code:
    /*
    (Cryptography) A company wants to transmit data over the telephone,
    but is concerned that its phones could be tapped.
    All of the data are transmitted as four-digit integers.
    The company has asked you to write a program that encrypts
    the data so that it can be transmitted more securely.
    Your program should read a four-digit integer and encrypt it as follows:
    
    Replace each digit by (the sum of that digit plus 7) modulus 10.
    Then, swap the first digit with the third,
    swap the second digit with the fourth and print the encrypted integer.
    
      Write a separate program that inputs an encrypted
      fourdigit integer and decrypts it to form the original number.
    
      */
    
    #include <iostream>
    int Encrypt (int value)
    {
    
    	// compute seperate digits.
    	// 1234
    	int First,Second,Third,Forth;
    
    	First = value/1000; // 1
    
    
    	Second = (value%1000) / 100; // 2
    	Third = (value%100) / 10; // 3
    	Forth =  value % 10; // 4
    	
    	First = (First+7) % 10; // 8
    	Second = (Second+7) % 10; // 9
    	Third = (Third+7) % 10; // 0
    	Forth = (Forth+7) % 10; // 1
    
    	if ( Third <= 0 ) { Third = 1; } // if third = zero we get a 3digit back
    	int Encrypted_number = (Third*1000)+(Forth*100)+(First*10)+(Second); // 1189
    	return (Encrypted_number);
    
    }
    int Decrypt ( int value )
    {
    	// 1189
    	int First,Second,Third,Forth;
    	First = value/1000; // 1
    	Second = (value%1000) / 100; // 1 
    	Third = (value%100) / 10; // 8
    	Forth =  value % 10; // 9
    
    	First = (First%7) % 10; //  1
    	Second = (Second%7) % 10; // 
    	Third = (Third%7) % 10; // 
    	Forth = (Forth%7) % 10; // 
    	if ( Third <= 0 ) { Third = 1; } 
    	int Decrypted_number = ((First*1000)+(Second*100)+(Third*10)+(Forth));
    	return (Decrypted_number);
    
    }
    
    void Printmenu()
    {
    	std::cout <<"[1] To encrypt a 4 digit number\n[2] To decrypt a 4 digit number" << std::endl;
    }
    int GetChoise ()
    {
    	int choise;
    	std::cin >> choise;
    	while (choise != 1 && choise != 2)
    	{
    		std::cout <<"Please enter a menu item!\n";
    		Printmenu();
    		std::cin >> choise;
    	}
    	return choise;
    
    }
    int main ()
    {
    
    	
    	Printmenu();
    	int Choise;
    	Choise = GetChoise ();
    	if (Choise == 1)
    	{
    		int to_be_encrypted,encrypted; 
    		std::cout <<"Please enter a 4 digit integer number for encryption\n";
    		std::cin >> to_be_encrypted;
    
    		encrypted = Encrypt(to_be_encrypted);
    
    		std::cout <<"The original number: "  << to_be_encrypted << std::endl;
    		std::cout <<"The encrypted number: " << encrypted << std::endl;
    		
    	}
    	else if (Choise == 2)
    	{
    		int to_be_decrypted,decrypted; 
    		std::cout <<"Please enter a 4 digit integer number for Decryption\n";
    		std::cin >> to_be_decrypted;
    
    		decrypted = Decrypt (to_be_decrypted);
    
    		std::cout <<"Decrypted: "  << decrypted << std::endl;
    		
    
    
    	}
    	std::cin.get();
    	std::cin.get();
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The opposite of + is -, not %. And you're not unshuffling the numbers.

    On a side note, this line
    Code:
    if ( Third <= 0 ) { Third = 1; } // if third = zero we get a 3digit back
    makes it impossible to decrypt the number, since there are multiple inputs going to the same output.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    When doing the opposite of a series of operations, you not only have to do the opposite operation (like - instead of +) but you have to go in the opposite order.

    Unfortunately, there's no direct opposite of &#37;. You have to think about your encryption and the possible values before and after the call to % during that encryption, then use that knowledge to figure out how to go back for that step.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    oh man I cant get it figured out

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are only 10 possible digits (0-9). Encrypt each digit on paper and find out what the result would be just for the digit encryption (don't rearrange the order like in your program).

    Then, figure out how to go backwards. It isn't a simple exercise, although it is just logic.

    Once you've figured that out, go back to your program. Your encryption encrypts each digit, then rearranges them. To decrypt, you need to rearrange the digits back into the correct order, then decrypt each digit individually like you figured out on paper.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    ok man tnx i will try

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  2. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 05:41 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 10:01 AM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21