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