mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 00:57:36 +11:00
65 lines
1.3 KiB
Java
65 lines
1.3 KiB
Java
import java.math.BigInteger;
|
|
import java.security.SecureRandom;
|
|
|
|
public class Q2RSA {
|
|
|
|
private static SecureRandom randomNum = new SecureRandom();
|
|
private BigInteger e;
|
|
private BigInteger p;
|
|
private BigInteger q;
|
|
private BigInteger n;
|
|
private BigInteger m;
|
|
private BigInteger d;
|
|
|
|
Q2RSA()
|
|
{
|
|
generateRSA();
|
|
}
|
|
|
|
private void generateRSA()
|
|
{
|
|
//Public Key:
|
|
e = new BigInteger("65537");
|
|
//Generate p
|
|
|
|
p = BigInteger.probablePrime(1024, randomNum);
|
|
|
|
//Generate q
|
|
q = BigInteger.probablePrime(1024, randomNum);
|
|
|
|
//Generate n
|
|
n = p.multiply(q);
|
|
|
|
//Generate phi n
|
|
m = ((p.subtract(BigInteger.valueOf(1))).multiply((q.subtract(BigInteger.valueOf(1)))));
|
|
|
|
//Generate d (private key)
|
|
d = e.modInverse(m);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Returns an array that represents a public key
|
|
* 0 - n
|
|
* 1 - e
|
|
* @return
|
|
*/
|
|
public BigInteger[] getPublicKey()
|
|
{
|
|
BigInteger[] publicKey = new BigInteger[2];
|
|
publicKey[0] = n;
|
|
publicKey[1] = e;
|
|
|
|
return publicKey;
|
|
}
|
|
|
|
/**
|
|
* @return the private key
|
|
*/
|
|
public BigInteger getPrivateKey()
|
|
{
|
|
return d;
|
|
}
|
|
}
|