📄 keygen.java
字号:
package SimpleRSA;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
public class KeyGen
{
private BigInteger getPQ()
{
BigInteger temp;
temp = BigInteger.probablePrime(5,rnd);
return temp;
}
private BigInteger getN()
{
BigInteger temp;
temp = p.multiply(q);
return temp;
}
private BigInteger getE()
{
BigInteger temp = new BigInteger(7,rnd);
return temp;
}
private BigInteger getD()
{
BigInteger p_1,q_1,n_1;
p_1 = p.subtract(BigInteger.valueOf(1));
q_1 = q.subtract(BigInteger.valueOf(1));
n_1 = p_1.multiply(q_1);
try
{
d = e.modInverse(n_1);
flag = true;
}
catch(Exception e1)
{
//System.out.println("No inverse");
}
return d;
}
static void keygen(String args) throws IOException
{
KeyGen key= new KeyGen();
int public_key[] = new int [2];
int private_key[] = new int [3];
while(flag == false)
{
p = key.getPQ();
q = key.getPQ();
n = key.getN();
e = key.getE();
d = key.getD();
if(e.equals(d)) flag =false;
}flag = false;
public_key[0] = n.intValue();
public_key[1] = e.intValue();
String destination = args + "public_keys.txt";
Input_Output.Output_Key(destination, public_key);
private_key[0] = n.intValue();
private_key[1] = e.intValue();
private_key[2] = d.intValue();
destination = args + "private_keys.txt";
Input_Output.Output_Key(destination, private_key);
System.out.println("The n is: "+ n);
System.out.println("The public key is: "+ e);
System.out.println("The private key is: "+ d);
}
private Random rnd = new Random();
private static BigInteger p,q,n,e,d;
private static boolean flag = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -