decrypting

This is a discussion on decrypting within the C++ Programming forums, part of the General Programming Boards category; As I said before somewhere, the modulus operator is new to me so I struggle to work algebraically with it. ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    decrypting

    As I said before somewhere, the modulus operator is new to me so I struggle to work algebraically with it.

    I input a number: 9876
    I apply an encryption method that makes it: 6543

    What I did was split that number into individual digits and applied the following: ( <number> + 7 ) % 10 = <answer>

    Examples:
    (6 + 7) % 10 = 3
    (7 + 7) % 10 = 4

    Now I am really struggling to reverse this for the decryption.

    I have tried numerous ways and failed. How in the world do I input 3 to get 6 out as an answer? This must be a valid mathematical solution that I can apply in all cases. I thought decrypting it would be simple, obviously not.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
     n | (n+7)%10
    ---+----------
     0 | 7
     1 | 8
     2 | 9
     3 | 0
     4 | 1
     5 | 2 
     6 | 3 
     7 | 4
     8 | 5
     9 | 6
    Code:
    for each digit n
        if n < 7
            add 10
        subtract 7 from n
    Last edited by XSquared; 12-28-2003 at 09:57 AM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Your suggestion makes sense. Here is the code, which does not decrypt correctly.

    Code:
    #include <iostream>
    using namespace std;
    
    // Prototype
    void encrypt();
    void decrypt();
    
    int main()
    {
       int choice;
       
       cout << "Digit ENCRYPTION & DECRYPTION Program By CC\n\n"
            << "Press 0 to quit\n"
            << "Press 1 to encrypt a 4-digit number\n"
            << "Press 2 to decrypt a number\n"
            << "Choice: ";
       cin >> choice;
       
       while ( choice != 0 )
       {
          switch (choice)
          {
             case 0:
                      break;
             case 1:
                      encrypt();
                      break;
             case 2:  
                      decrypt();
                      break;
             default:
                      cout << "Please enter a valid option...\n"
                           << "Choice: ";
                      cin >> choice;
                      break;
          }
          
          cout << "Press 0 to quit\n"
               << "Press 1 to encrypt a 4-digit number\n"
               << "Press 2 to decrypt a number\n"
               << "Choice: ";
          cin >> choice;
       }
       
       return 0;
    }
    
    void encrypt()
    {
       int number;
       int split1, split2, split3, split4;
       
       cout << "\nEnter a four digit number to be encrypted: ";
       cin >> number;
       
       // Splits the numbers into indidual digits
       split1 = number % 10;
       number = number / 10;
    
       split2 = number % 10;
       number = number / 10;
       
       split3 = number % 10;
       number = number / 10;
    
       split4 = number % 10;
       
       // Resets the value of number for use later
       number = 0;
    
       // Let's now encrypt the numbers
       split1 += 7;
       split1 %= 10;
    
       split2 += 7;
       split2 %= 10;
       
       split3 += 7;
       split3 %= 10;
       
       split4 += 7;
       split4 %= 10;
       
       // Put the digits back together in a special order
       number += split3;
       number *= 10;
       number += split4;
       number *= 10;
       number += split1;
       number *= 10;
       number += split2;
       
       cout << "Encrypted number:\t" <<  number << "\n\n";
    }
    
    void decrypt()
    {
       int number;
       int split1, split2, split3, split4;
       
       cout << "\nEnter a number to be decrypted: ";
       cin >> number;
       
       // Splits the numbers into indidual digits
       split1 = number % 10;
       number = number / 10;
    
       split2 = number % 10;
       number = number / 10;
       
       split3 = number % 10;
       number = number / 10;
    
       split4 = number % 10;
       
       // Resetting value of number
       number = 0;
       
       // Decrypting time
       if ( split1 < 7 )
       {
          split1 += 10;
          split1 -= 7;
       }
       
       if ( split2 < 7 )
       {
          split1 += 10;
          split1 -= 7;
       }
       
       if ( split3 < 7 )
       {
          split1 += 10;
          split1 -= 7;
       }
       
       if ( split4 < 7 )
       {
          split1 += 10;
          split1 -= 7;
       }
       
       // Rearrange numbers and join them back together
       number += split3;
       number *= 10;
       number += split4;
       number *= 10;
       number += split1;
       number *= 10;
       number += split2;
       
       cout << "Decrypted number:\t" <<  number << "\n\n";
    }

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    When you're decrypting...
    a) You are subtracting 10 or adding 7 to split1 every time.
    b) You need to subtract 7 even if the number is greater than 7.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Gotcha. My first attempt was to simply subtract 7 from the numbers and use absolute values to make sure that the numbers stay positive. It only worked for a few, which I now realized are for the one's greater than 7. At least you've shown me how to solve problems like this in the future.

    Much appreciated.

  6. #6
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Once you get this working, you might want to take a look at more advanced cryptography. It's a pretty interesting subject.

  7. #7
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I would definitely like to dip my toe into this area. First let me finish going through my recently purchased Deitel book and from there I'll look into other more advanced areas.

    What would you suggest CompiledMonkey?

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Look at some of the symmetric ciphers first: DES, Rijndael (AES), Blowfish, etc.

    A good introduction book is the Handbook of Applied Cryptography ( http://www.cacr.math.uwaterloo.ca/hac/ ).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Lightbulb y 2 try such a long method?

    try this code to decript 6543 to 9876.

    Code:
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        // declare variables
        
        int num,digit1,digit2,digit3,digit4,dcript1,dcript2,dcript3,dcript4;
        
        // initialize variables
        
        dcript1=0,dcript2=0,dcript3=0,dcript4=0;
        
        // read integer
        
        cout<<"\nEnter the integer to be decripted:\t";
        cin>>num;
        
        // separate integers
        
        digit1=num%10;
        num/=10;
        digit2=num%10;
        num/=10;
        digit3=num%10;
        num/=10;
        digit4=num%10;
        num/=10;
        
        // print decripted digit
        
        if((digit1<7||digit2<7)||(digit3<7||digit4<7))
        {
            dcript1=(10+digit1)-7;
            dcript2=(10+digit2)-7;
            dcript3=(10+digit3)-7;
            dcript4=(10+digit4)-7;
            
            cout<<"\nDecripted 4-digit integer:\t\t";
            cout<<dcript4<<dcript3<<dcript2<<dcript1;
        }
        else
            dcript1=(digit1-7)%10;
            dcript2=(digit2-7)%10;
            dcript3=(digit3-7)%10;
            dcript4=(digit4-7)%10;
               
    getch();
    }

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What happens when you decrypt '9821' with your algorithm?

    You get 121154. YOU FAIL!!!!!! BWA HAHAHAHAHAHAHAHAHAHAHA!!!!!!!

    That kinda popped out. Sorry.

    Also, you forgot the closing brackets on your else statement, and you don't output the decrypted number in the else either.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    encryption basics

    Is there a free resource on the net that has a more basic approach to encryption than applied cryptography.
    As being a beginner at this I feel this book is a little advanced for a first time attempt at encryption.

    [Edit1]
    How would you encrypt a character instead of a number?
    Would you use a similar process on the ascii or hexadecimal equivalent?
    [/Edit1]
    Last edited by xviddivxoggmp3; 12-30-2003 at 03:33 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  12. #12
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    You get 121154. YOU FAIL!!!!!! BWA HAHAHAHAHAHAHAHAHAHAHA!!!!!!!
    Been reading a few of the posts and that one really made us laugh! cheers xsquared
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Require help decrypting XOR.
    By mkthnx001 in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2009, 07:15 PM
  2. Decrypting OTP ciphertext
    By slippy in forum C++ Programming
    Replies: 11
    Last Post: 12-25-2007, 05:59 AM

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