implement key generation
This commit is contained in:
parent
05b8cfa9bc
commit
6c149f0176
@ -1,9 +1,16 @@
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Main {
|
||||
static BigInteger cipher (BigInteger message, BigInteger key, BigInteger module) {
|
||||
return message.modPow(key, module);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
BigInteger prime = Prime.generate(32);
|
||||
System.out.println("Finished - number is "+prime);
|
||||
|
||||
RSA test = new RSA (32);
|
||||
test.createKeys();
|
||||
System.out.println(String.format("public key: 0x%X", test.getPublicKey()));
|
||||
System.out.println(String.format("private key: 0x%X", test.getPrivateKey()));
|
||||
System.out.println(String.format("RSA module: 0x%X", test.getRSAModule()));
|
||||
}
|
||||
}
|
||||
|
60
src/RSA.java
60
src/RSA.java
@ -1,4 +1,3 @@
|
||||
public class RSA {
|
||||
// p = 5, q = 11
|
||||
// RSA-Modul N = p * q = 55
|
||||
// Phi(N) = Phi(p)*Phi(q) | weil eigenschaft eulersche phi funktion und p,q teilerfremd
|
||||
@ -19,10 +18,65 @@ public class RSA {
|
||||
// ----
|
||||
// Entschlüsseln
|
||||
// m = c^d (mod N) => 52^27 (mod 55) = 13 = m
|
||||
public RSA() {
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class RSA {
|
||||
private int bitLength;
|
||||
private BigInteger p;
|
||||
private BigInteger q;
|
||||
private BigInteger n;
|
||||
private BigInteger phiP;
|
||||
private BigInteger phiQ;
|
||||
private BigInteger phiN;
|
||||
private BigInteger e;
|
||||
private BigInteger d;
|
||||
|
||||
public RSA (int bitLength) {
|
||||
this.bitLength = bitLength;
|
||||
this.p = Prime.generate(bitLength);
|
||||
this.q = Prime.generate(bitLength);
|
||||
this.n = p.multiply(q);
|
||||
this.phiP = p.subtract(BigInteger.ONE);
|
||||
this.phiQ = q.subtract(BigInteger.ONE);
|
||||
this.phiN = phiP.multiply(phiQ);
|
||||
}
|
||||
|
||||
|
||||
private void findPublicKey () {
|
||||
BigInteger check;
|
||||
do {
|
||||
this.e = Prime.generate(this.bitLength*2/3);
|
||||
check = this.e.gcd(phiN);
|
||||
} while (!check.equals(BigInteger.ONE));
|
||||
}
|
||||
|
||||
private void findPrivateKey () {
|
||||
this.d = e.modInverse(phiN);
|
||||
}
|
||||
|
||||
private void endKeyCreation () {
|
||||
this.p = BigInteger.ZERO;
|
||||
this.q = BigInteger.ZERO;
|
||||
this.phiP = BigInteger.ZERO;
|
||||
this.phiQ = BigInteger.ZERO;
|
||||
this.phiN = BigInteger.ZERO;
|
||||
}
|
||||
|
||||
public void createKeys() {
|
||||
this.findPublicKey();
|
||||
this.findPrivateKey();
|
||||
this.endKeyCreation();
|
||||
}
|
||||
|
||||
public BigInteger getPublicKey () {
|
||||
return e;
|
||||
}
|
||||
|
||||
public BigInteger getPrivateKey () {
|
||||
return d;
|
||||
}
|
||||
|
||||
public BigInteger getRSAModule() {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user