Merged basic ui and key generation
Added generate, crypt and decrypt function to ui
This commit is contained in:
commit
332ccf142e
@ -2,26 +2,27 @@
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="MainWindow">
|
||||
<grid id="cbd77" binding="contentPane" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||
<constraints>
|
||||
<xy x="48" y="54" width="720" height="500"/>
|
||||
<xy x="48" y="54" width="962" height="500"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="empty">
|
||||
<size top="10" left="10" bottom="10" right="10"/>
|
||||
</border>
|
||||
<children>
|
||||
<grid id="12135" layout-manager="GridLayoutManager" row-count="9" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="12135" layout-manager="GridLayoutManager" row-count="9" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints border-constraint="Center"/>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="adfc7" class="javax.swing.JTextArea" binding="publicKeytextArea">
|
||||
<component id="adfc7" class="javax.swing.JTextArea" binding="publicKeyTextArea">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<lineWrap value="true"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
@ -31,7 +32,9 @@
|
||||
<preferred-size width="150" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<properties>
|
||||
<lineWrap value="true"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="17322" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
@ -164,6 +167,22 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="30d6d" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="RSA module:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="9cd2f" class="javax.swing.JTextArea" binding="moduleTextArea">
|
||||
<constraints>
|
||||
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
@ -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();
|
||||
|
66
src/RSA.java
66
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user