I just made a simple program in C that takes a 4-digit code and encrypts every digit (previously decomposed, that goes from 0 to 9) using this easy algorithm:
new_num1 = (old_num1 + 7) % 10;
new_num2 = (old_num2 + 7) % 10;
new_num3 = (old_num3 + 7) % 10;
new_num4 = (old_num4 + 7) % 10;
Now I want to make a decrypt.c program to decrypt 4-code digit previously encrypted.
In which way I can revert the number?
This is my code for crypter.c (in this example I also revert the first digit with third and second with fourth)
#include <stdio.h>
int main()
{
int codice;
int num1, num2, num3, num4, temp1, temp2, new_num1_temp, new_num2_temp;
printf("\n(LOLCrytter v0.1)\n\nInsert 4-digit code to crypt: -1 to exit ");
scanf("%d", &codice);
if(codice == -1)
return 0;
while(codice < 1000 || codice > 9999) {
printf("\nInsert NUMERIC (!) 4-digit code bru..: ");
scanf("%d", &codice);
}
// Get every digit by logic math and not by strings functions
temp1 = codice;
num1 = temp1 / 1000;
temp2 = temp1 % 1000;
num2 = temp2 / 100;
temp1 = temp2 % 100;
num3 = temp1 / 10;
num4 = temp1 % 10;
// Crypting...
num1 = (num1 + 7) % 10;
num2 = (num2 + 7) % 10;
num3 = (num3 + 7) % 10;
num4 = (num4 + 7) % 10;
// Crypting...
new_num1_temp = num3;
num3 = num1;
num1 = new_num1_temp;
new_num2_temp = num4;
num4 = num2;
num2 = new_num2_temp;
printf("\nNew code: %d%d%d%d\n\n", num1, num2, num3, num4);
return 0;
}
if input restricted to [0..9] (as code shows at the moment of the answer) then it will work: encryption of 8 is (8 + 7) % 10 = 5. Decryption is 10 + 5 - 7 = 8. your custom algorithm in general: encryption of x is (x + 7) % m == r; decryption is: m + r - 7 == x
But author in question wants to "decrypt a 4-digit number" and just in case I need to make a caution if author considers changing the code: Mod operation is not bijection - it is not reversible: (0 + 7) % 10 = 7; (10 + 7) % 10 = 7.
if algorithm's input is only from range 0..9 then , for instance: 10 % 7 = 3; 7*1 + 3 = 10. That is 7 * (10 / 7) + 3 = 10. Every number a can be represented as a = m * (a/m) + r; where m is modulus, r - remainder. "/" integral devision.
Function of form (k + n) mod m is good for hash functions, used for random numbers generation. If you want to use simple encryption for learning you can achieve better results with minimum efforts - use XOR. Generate key and XOR it with plain text. To decrypt XOR encrypted text with the same key. Read about One-time pad One-time pad - very simple in implementation encryption technique but that cannot be cracked.
UPDATED: As cryptographer I would recommend you (if you are interested in learning basics of cryptography) starting to learn and implement classical simple crypto algorithms like: Caesar cipher, simple substitution, Vigener cryptosystem (they are available in wikipedia).
Your crypto function (t + 7) % 10 is very similar to Caeser educational cipher with some changes: The encryption can be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. Encryption of a letter x by a shift n can be described mathematically as,
Decryption is performed similarly,
Goog luck!