1. 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. 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
subtract 7 from n```

3. 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. 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.

5. 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. Once you get this working, you might want to take a look at more advanced cryptography. It's a pretty interesting subject.

7. 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. 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/ ).

9. 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;

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. 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.

11. 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]

12. You get 121154. YOU FAIL!!!!!! BWA HAHAHAHAHAHAHAHAHAHAHA!!!!!!!
Been reading a few of the posts and that one really made us laugh! cheers xsquared