need help with modulo ecuation in simple encryption algorithm

closed account (E3h7X9L8)
i've made this simple encryption algorithm that takes a number and replace every digit with (digit+17)%10 while the digits are placed reversed in the new number

but i'm having trouble trying to make the decryption function..
i know that digit = (17+x)%10; but im not sure how to implement this ecuation into code... i cant figure the mathematic way since i never met modulo(%) in ecuations


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include <iostream>
using std::cin;
using std::endl;
using std::cout;

int encryption(int number)
{
	// replace digit with digit + 7 modulus 10
	int newNumber = 0;
	int digit;
	
	while (number != 0)
	{
		digit = number % 10;
		newNumber = ((digit + 17) % 10) + newNumber * 10;
		number /= 10;
	}
	return newNumber;
}

int decryption(int number)
{
	return 0;
}

int main()
{
	cout << encryption(1234);

	cin.ignore();
	cin.get();

	return 0;
}

Last edited on
#1 Property: (a + b) % c = (a % c + b) % c = (a % c + b) % c = (a % c + b % c) % c
#2 Corollary: (a + c) % c = (a % c + c % c) % c = (a % c + 0) % c = (a % c) % c = a % c
#3 Property: if 0 <= a < b then a % b = a

Therefore:
0 <= x < n
[by #3]
x % n = x
[by #2]
(x + n) % n = x
(x + k + n - k) % n = x
[by #1]
((x + k) % n + n - k) % n = x

Therefore: if
f(x) = (x + k) % n
and
g(x) = (x + n - k) % n
then g(f(x)) = x for all 0 <= x < n

Note: If b > c:
(a + b) % c =[by definition of integer division] (a + c * d + b % c) % c = (a + (c * d) % c + b % c) % c = (a + 0 + b % c) % c = (a + b % c) % c
Therefore (x + 17) % 10 = (x + 7) % 10
Last edited on
closed account (E3h7X9L8)
thanks
Topic archived. No new replies allowed.