diff --git a/src/MainWindow.form b/src/MainWindow.form index c38ab95..1b6c41e 100644 --- a/src/MainWindow.form +++ b/src/MainWindow.form @@ -2,26 +2,27 @@
- + - + - + + @@ -31,7 +32,9 @@ - + + + @@ -164,6 +167,22 @@ + + + + + + + + + + + + + + + + diff --git a/src/MainWindow.java b/src/MainWindow.java index f381b54..225c02b 100644 --- a/src/MainWindow.java +++ b/src/MainWindow.java @@ -3,10 +3,11 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.math.BigInteger; public class MainWindow extends JDialog implements ChangeListener, ActionListener { private JPanel contentPane; - private JTextArea publicKeytextArea; + private JTextArea publicKeyTextArea; private JTextArea privateKeyTextArea; private JTextField plainNumber; private JTextField encryptedNumber2; @@ -17,7 +18,9 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene private JTextField decryptedNumber; private JSlider keylength; private JLabel keylengthLabel; + private JTextArea moduleTextArea; private JButton buttonOK; + private RSA rsa; public MainWindow() { setContentPane(contentPane); @@ -48,14 +51,29 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene public void actionPerformed(ActionEvent e) { JButton source = (JButton) e.getSource(); if (source == this.cryptButton) { - System.out.println("crypt button clicked!"); + BigInteger message = new BigInteger(this.plainNumber.getText()); + BigInteger key = new BigInteger(this.publicKeyTextArea.getText()); + BigInteger module = new BigInteger(this.moduleTextArea.getText()); + String encrypted = RSA.cipher(message, key, module).toString(); + this.encryptedNumber.setText(encrypted); + } else if (source == this.decryptButton) { - System.out.println("decrypt button clicked!"); + BigInteger encryNumber = new BigInteger(this.encryptedNumber2.getText()); + BigInteger key = new BigInteger(this.privateKeyTextArea.getText()); + BigInteger module = new BigInteger(this.moduleTextArea.getText()); + String decrypted = RSA.cipher(encryNumber, key, module).toString(); + this.decryptedNumber.setText(decrypted); + } else if (source == this.generateKeysButton) { - System.out.println("Generate button clicked!"); + this.rsa = new RSA(this.keylength.getValue()); + this.rsa.createKeys(); + this.publicKeyTextArea.setText(this.rsa.getPublicKey()+""); + this.privateKeyTextArea.setText(this.rsa.getPrivateKey()+""); + this.moduleTextArea.setText(this.rsa.getRSAModule()+""); } } + public static void main(String[] args) { MainWindow dialog = new MainWindow(); dialog.pack(); diff --git a/src/RSA.java b/src/RSA.java index 0253b12..b833f7f 100644 --- a/src/RSA.java +++ b/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,69 @@ 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; + } + + static BigInteger cipher (BigInteger message, BigInteger key, BigInteger module) { + return message.modPow(key, module); + } }