Browse Source

implement key generation

master
Eva Zimmermann (uib18557) 4 years ago
parent
commit
6c149f0176
2 changed files with 65 additions and 4 deletions
  1. +8
    -1
      src/Main.java
  2. +57
    -3
      src/RSA.java

+ 8
- 1
src/Main.java View File

@@ -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()));
}
}

+ 57
- 3
src/RSA.java View File

@@ -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…
Cancel
Save