Long, modulo and RSA-algorithm

As a school project, I have to make a program that can encrypt and decrypt code, whilst also registering the encoded code in a database.

But I’ve encountered a problem:

To encrypt a message using RSA, you need to lift the message to a power and then take the modulo. That’s all fun and games, until I start using double digit numbers. The result of the exponent quickly reaches the long max value, 9,223,372,036,854,775,807.

This is a major problem, since taking the modulo of 9,223,372,036,854,775,807 will always result in the same result, making every bit of message the same.

Does anybody have a way to work around this? The math program Maple is able to do the calculations perfectly, but it’s quite a bother to be switching between the two programs in an assignment.

Thanks in advance

1 Like

Check out BigInteger it’s designed for storing numbers that exceed the max size of a long. There’s also BigDecimal for floating point numbers.

import java.math.BigInteger;

void setup() {
  BigInteger bigInt = new BigInteger(String.valueOf(Long.MAX_VALUE));
  println(bigInt);
  println(bigInt.multiply(new BigInteger(String.valueOf(Long.MAX_VALUE))));
}

//Output
9223372036854775807
85070591730234615847396907784232501249
1 Like

Thank you very much, might just work :smiley:

1 Like