mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 04:37:37 +11:00
59 lines
1.3 KiB
Java
59 lines
1.3 KiB
Java
import java.math.BigInteger;
|
|
|
|
public class Q2Tools {
|
|
|
|
Q2Tools()
|
|
{
|
|
//Nothing to do here
|
|
}
|
|
|
|
public void generateRSA()
|
|
{
|
|
|
|
int e = 65537;
|
|
|
|
//Generate p
|
|
|
|
//Generate q
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Performs a fast modular exponation, (base^exponent)%modulus.
|
|
* Takes input values as strings containg the numbers
|
|
* @param base
|
|
* @param exponent
|
|
* @param modulus
|
|
* @return
|
|
*/
|
|
public BigInteger fme(String base, String exponent, String modulus)
|
|
{
|
|
BigInteger baseVal = new BigInteger(base);
|
|
BigInteger exponentVal = new BigInteger(exponent);
|
|
BigInteger modulusVal = new BigInteger(modulus);
|
|
|
|
if(modulusVal.equals(BigInteger.valueOf(1)))
|
|
{
|
|
return BigInteger.valueOf(0);
|
|
}
|
|
|
|
BigInteger result = new BigInteger("1");
|
|
|
|
while(exponentVal.compareTo(BigInteger.valueOf(0))==1)
|
|
{
|
|
if(exponentVal.and(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(1))==0)
|
|
{
|
|
result = result.multiply(baseVal);
|
|
result = result.mod(modulusVal);
|
|
|
|
}
|
|
|
|
exponentVal = exponentVal.shiftRight(1);
|
|
baseVal = (baseVal.multiply(baseVal)).mod(modulusVal);
|
|
}
|
|
return result;
|
|
}
|
|
}
|