encryption and decryption

#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
#include<fstream>

using namespace std;


int main()
{

int num = 0, enter, d1, d2, d3, d4 ;
bool quit = false;

while (num <= 3 && quit == false)
{
cout << "\n\n1-Encryption\n2-Decryption\n3-Exit\n\n";
cin >> num;


if (num == 1)
{
cout << "1\nEncryption\nEnter your orignal 4-digit number: ";
cin >> enter;

d1 = ((enter / 1000) * 3) % 10;
d2 = ((enter / 100) * 3) % 10;
d3 = ((enter / 10) * 3) % 10;
d4 = ((enter / 1) * 3) % 10;



cout << "Your encrypted number is " << d3 << d4 << d1 << d2;
}
else if (num == 2)
{

cout << "2\nDecryption\nEnter your 4-digits encrypted number: ";
cin >> enter;

d1 = ((enter / 1000) % 10) / 3;
d2 = ((enter / 100) % 10) / 3;
d3 = ((enter / 10) % 10) / 3;
d4 = ((enter / 1) % 10) / 3;




cout << "Your decrypted original number is " << d3 << d4 << d1 << d2;
}
else
{
quit = true;
return 0;
}

}
}


this is my program , but my decrypted number get wrong , i key in 9236 it should be 1234 , but it come out 1230 , which line or my calculation wrong?
Last edited on
The problem is that your encryption is actually x * 3 % 10 and not just x * 3, so you can't reverse it by just dividing by 3.
During the forward step, you used this mapping:
1
2
3
4
5
6
7
8
9
10
11
x    x * 3 % 10
0        0
1        3
2        6
3        9
4        2
5        5
6        8
7        1
8        4
9        7
The reverse table has the columns swapped and the rows sorted by the first column:
1
2
3
4
5
6
7
8
9
10
11
y    f(y)
0     0
1     7
2     4
3     1
4     8
5     5
6     2
7     9
8     6
9     3
Can you guess what f() is by looking at the second column of this table?
Last edited on
i try so many times already still cannot , can giv me some hints?
It's something of the form (x * n) % m.
Topic archived. No new replies allowed.