I am trying to get back the proper exponent, and while the example numbers (exampleA, exampleB, exampleP) all return what they should when running through the loop, plugging in long number(A, B, p) do not. Any way I can fix this?
public static void main(String[] args) {
//g = 5//
//let i run from 0 to p -1//
//compute g^a mod p and see if it is A, if you find A, then a is the solution for A//
//compute g^a mod p and see if it is B, if you find B, then a is the solution for B//
long A = 1958258942L;
long B = 670001116L;
long p = 3267000013L;
//example p to plug in for example a and example b//
long exampleP = 23;
//plugging this in should return 4//
long exampleA = 4;
//plugging this in should return 3//
long exampleB = 10;
int newNum;
int a = 0;
int g = 5;
for (int i = 0; i < (p - 1); i++) {
a = i;
newNum = powMod(g, a, exampleP);
if (newNum == exampleB) break;
}
System.out.println(a);
}
public static int powMod(int g, int exponent, long p) {
int result = 1;
while (exponent > 0)
{
// exponent is odd
if (exponent % 2 == 1)
{
result = (int) ((result * g) % p);
}
// divide exponent in half
exponent /= 2;
// square base and take remainder
g = (int) ((g * g) % p);
}
return result;
}