I am playing around with simple encryption using RSA algorithms and found a strange bug.
private static Integer testEnc(Integer value){
Integer val = (int)Math.pow(value, 37);
return val % 437;
}
private static Integer testDec(Integer value){
Integer val = new Integer((int)Math.pow(value, 289));
return val % 437;
}
public static void main(String[] args) {
System.out.print("Encode 55 = ");
Integer encoded = testEnc(2);
System.out.println(encoded + "\n");
System.out.print(encoded + " decoded = ");
Integer decoded = testDec(3977645);
System.out.println(decoded + "n");
}
Both of the following functions return 97 regardless of input. If I comment out the modulus and just return val, the returned value is 2147483647.
Type casting double to int seems to be the issue but I am not sure why this is.
These methods are static only because I was calling them from a main method.
2147483647 is the int max value, aka 2^31-1.
For any value >=2 you get an overflowm because 2^37 > 2^31-1
In order to get the power modulo 437 you should get the modulo at every step. E.g. like: