Thread: decryption>> need help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Exclamation decryption>> need help

    hey everyone
    this board sounds great

    i have a question that has been bothering me for a while
    and it due next saterday

    the question is about encrytption and decryption,, i have done the encryption part and its working just fine, but when i come to the decryption one , i am stuck

    the question is asking to encrypt a 4 digit integer and replace each integer with (the sum of that digit plus 7) modulus 10
    then swap first digit with the third ,, and the fourth digit with the second

    then afterwards right a program that decrypts the integer

    heres the program i wrote about the encryption , hope u could help me with the decryption one


    Code:
    // encryption.cpp
    // how to encrypt a 4-digit integer
    
    #include <iostream.h>
    
    
    int main()
    {
    
    int data;
    int digit;
    int divider = 1000;
    int encrypted = 0;
    cout << "Enter a four-digit number to be encrypted: "<< endl;
    
    cin>> data;
    
    
    
    /* extract the fourth digit from the data by dividing it by 1000
    e.g. 4567, we want to extract 4 so we divide 1000, the answer will be 4.567 but only 4 
    will remain because we declared it as an integer.*/
    
    for (int X = 0; X <= 3; X++) {
    
    digit = data / divider;
    
    data = data - (digit * divider);
    
    digit = (digit + 7 ) % 10;
    
    if (X == 0) 
    	encrypted = encrypted + (digit * 10);
    
    else if (X == 1)
    	encrypted = encrypted + digit  ;
    
    
    else if (X == 2)
    	encrypted = encrypted + (digit*1000) ;
    
    else if (X == 3)
    
    	encrypted = encrypted + (digit * 100);
    
    divider = divider / 10;
    
    
    }
    cout<< "the encrypted number is: " << encrypted;
    
    
    return 0;
    }
    thankx eveyone

    byoo

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question Maybe this will help....

    I assume the problem is reversing this part: (the sum of that digit plus 7) modulus 10.

    I made a chart of that operation:

    0 -> 7
    1 -> 8
    2 -> 9
    3 -> 0
    4 -> 1
    5 -> 2
    6 -> 3
    7 -> 4
    8 -> 5
    9 -> 6

    To reverse that, it looks like you add 3, and then "throw-away" any digits in the 10's place.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    hey doug

    can u explain alittel bit more , cause i didnt get the i dea how to throw away and digits in the 10's place???????????

    thx again

    byoo

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Know what operator % is??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    any one can help???????

    i need to finish my assgment

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but when i come to the decryption one , i am stuck
    Just read the instructions backwards.

    > then swap first digit with the third ,, and the fourth digit with the second
    What's the reverse of this?

    > digit = (digit + 7 ) % 10;
    Here's a hint
    digit = digit + 7; if ( digit >= 10 ) digit = digit - 10;
    Is a lot easier to do when you need to start thinking about the decrypt process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    hey
    thx for the hint
    and i think that i have fount the soluation
    but i need someone to check it out

    Code:
    // deycryption.cpp
    // how to decrypte a 4-digit integer
    
    #include <iostream.h>
    
    
    int main()
    {
    
    int data;
    int digit;
    int divider = 1000;
    int decrypted = 0;
    
    
    cout << "Enter the four digit encrypted number to be decrypted: "<< endl;
    
    cin>> data;
    
    for (int X = 0; X <= 3; X++) {
    
    	
    digit = data / divider;
    
    data = data - (digit * divider);
    
    
    digit = ((digit + 10 ) % 10) + 3;
    
    if (X == 0) 
    	decrypted = decrypted + (digit * 10);
    
    	else if (X == 1)
    		decrypted = decrypted + digit  ;
    
    
    		else if (X == 2)
    			decrypted = decrypted + (digit*1000) ;
    
    				else if (X == 3)
    					decrypted = decrypted + (digit * 100);
    
    divider = divider / 10;
    
    
    }
    cout<< "the encrypted number is: " << decrypted;
    
    
    return 0;
    }

    byooo

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    anyone ????

    help!!!!!!!!!

  9. #9
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Why not just test it yourself? Either by hand, working through it step by step, or easier, compile and run it, put in numbers, see if you get the same back out.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Absolutely, takes all of 10 seconds to compile it and try some stuff.
    If you can't be bothered to test your own code, give up now.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed