Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing a modulus operator for decrypting

For school, i've got an assignment to encrypt a four-digit Integer by the following requirements.

A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a method that will encrypt their data so that it may be transmitted more securely. Your method should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then, swap the second digit with the fourth. Then print the encrypted integer. Write a separate method that inputs an en-crypted four-digit integer, and decrypts it to form the original number.

The encrypting of the four-digit wasn't such of a problem, I've converted it to a string, then to a char array and then seperately encrypted the numbers by its needs.

The method I made looks like this:

public int encrypt4DigitNumber(int number) throws Exception {
    String numberAsString = String.valueOf(number);
    if (numberAsString.length() != 4) {
        throw new Exception("The digit has to be 4 digits long");
    }
    int[] numbers = new int[4];

    char[] numbersAsChars = numberAsString.toCharArray();
    for (int index = 0; index < numbersAsChars.length; index++) {
        int currentNumber = Character.getNumericValue(numbersAsChars[index]);
        int numberToReplace = (currentNumber + 7) % 10;

        numbers[index] = numberToReplace;
    }

    int second = numbers[1];
    int fourth = numbers[3];

    numbers[1] = fourth;
    numbers[3] = second;

    StringBuilder encryptedNumberStringBuilder = new StringBuilder();
    for (int encryptedNumber : numbers) {
        encryptedNumberStringBuilder.append(encryptedNumber);
    }

    String encryptedNumberString = encryptedNumberStringBuilder.toString();
    return Integer.parseInt(encryptedNumberString);
}

The problem came when I had to de-crypt the encrypted four-digit code.

I know I had to swap the 2 array elements, and add 7 to each number in the array.

The thing I didn't know how to do was reverse the modulus operator, the only thing I can come up with is multiply the current number by 10, but that won't work.

After doing some research, I have to search the left-overs from the modulus somewhere, but I have absolutely no idea how to do that. Do I need to return those in the encrypt function aswell and pass them into the decrypt function?

Can someone explain the process of reversing the modulus operator?

like image 711
Bas Avatar asked Jun 05 '26 09:06

Bas


1 Answers

If you encrypt a number from 0 to 9 (= mod 10) with an offset 7, you can of course subtract the offset from every digit during decryption and wrap around if the number is negative, but that is not very nice:

int numberToReplace = currentNumber - 7;
if (numberToReplace < 0) {
    numberToReplace += 10;
}

If the encryption offset is 7, then the decryption offset would be 3 (10 - 7). You could just add 3 to each digit and apply mod 10 in order to decrypt them.

int numberToReplace = (currentNumber + 3) % 10;
like image 155
Artjom B. Avatar answered Jun 06 '26 23:06

Artjom B.