RSA/src/RSA.java
Christian Loch 332ccf142e Merged basic ui and key generation
Added generate, crypt and decrypt function to ui
2020-05-07 18:49:41 +02:00

87 lines
2.3 KiB
Java

// 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
// = (p-1)*(q-1) | p,q primzahlen, daher nur durch 1 und sich selbst teilbar und alle anderen teilerfremd
// = 4*10 = 40
// e: 1 < e < Phi(N) und ggT(e, Phi(N)) = 1
// e elem aus {3,..} => e = 3
// (e,N) = (3, 55) public key
// -----
// d: e*d kongr 1 mod Phi(N) => e*d mod Phi(N) = 1
// 3*d mod 40 = 1
// Durch probieren: d=27 => (27, 55) private key
//
// ----
// Verschlüsseln
// 13
// c = m^e (mod N) => 13^3 (mod 55) = 52 = c
// ----
// Entschlüsseln
// m = c^d (mod N) => 52^27 (mod 55) = 13 = m
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;
}
static BigInteger cipher (BigInteger message, BigInteger key, BigInteger module) {
return message.modPow(key, module);
}
}