I have the method to calculate D(private key) for RSA. The method uses extended Euclidean algorithm. So sometimes I get negative D and it isn't right how I can fix it? D * e mod (p-1)(q-1)=1; here is the code of the method
public static BigInteger extEuclid(BigInteger a, BigInteger b)
{
BigInteger x = BigInteger.ZERO, y = BigInteger.ONE, lastx = BigInteger.ONE, lasty = BigInteger.ZERO, temp;
while (!b.equals(BigInteger.ZERO))
{
BigInteger q = a.divide(b);
BigInteger r = a.mod(b) ;
a = b;
b = r;
temp = x;
x = lastx.subtract(q.multiply(x));
lastx = temp;
temp = y;
y = lasty.subtract(q.multiply(y));
lasty = temp;
}
return lastx;
}
public void calcD(){
d = extEuclid(e, fEuler);
}