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">
|
<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">
|
<grid id="cbd77" binding="contentPane" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="48" y="54" width="720" height="500"/>
|
<xy x="48" y="54" width="962" height="500"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="empty">
|
<border type="empty">
|
||||||
<size top="10" left="10" bottom="10" right="10"/>
|
<size top="10" left="10" bottom="10" right="10"/>
|
||||||
</border>
|
</border>
|
||||||
<children>
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints border-constraint="Center"/>
|
<constraints border-constraint="Center"/>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<children>
|
||||||
<component id="adfc7" class="javax.swing.JTextArea" binding="publicKeytextArea">
|
<component id="adfc7" class="javax.swing.JTextArea" binding="publicKeyTextArea">
|
||||||
<constraints>
|
<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">
|
<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"/>
|
<preferred-size width="150" height="50"/>
|
||||||
</grid>
|
</grid>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
|
<lineWrap value="true"/>
|
||||||
<text value=""/>
|
<text value=""/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
@ -31,7 +32,9 @@
|
|||||||
<preferred-size width="150" height="50"/>
|
<preferred-size width="150" height="50"/>
|
||||||
</grid>
|
</grid>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties/>
|
<properties>
|
||||||
|
<lineWrap value="true"/>
|
||||||
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
<component id="17322" class="javax.swing.JLabel">
|
<component id="17322" class="javax.swing.JLabel">
|
||||||
<constraints>
|
<constraints>
|
||||||
@ -164,6 +167,22 @@
|
|||||||
<text value=""/>
|
<text value=""/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</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>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
</children>
|
</children>
|
||||||
|
@ -3,10 +3,11 @@ import javax.swing.event.ChangeEvent;
|
|||||||
import javax.swing.event.ChangeListener;
|
import javax.swing.event.ChangeListener;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class MainWindow extends JDialog implements ChangeListener, ActionListener {
|
public class MainWindow extends JDialog implements ChangeListener, ActionListener {
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JTextArea publicKeytextArea;
|
private JTextArea publicKeyTextArea;
|
||||||
private JTextArea privateKeyTextArea;
|
private JTextArea privateKeyTextArea;
|
||||||
private JTextField plainNumber;
|
private JTextField plainNumber;
|
||||||
private JTextField encryptedNumber2;
|
private JTextField encryptedNumber2;
|
||||||
@ -17,7 +18,9 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
|
|||||||
private JTextField decryptedNumber;
|
private JTextField decryptedNumber;
|
||||||
private JSlider keylength;
|
private JSlider keylength;
|
||||||
private JLabel keylengthLabel;
|
private JLabel keylengthLabel;
|
||||||
|
private JTextArea moduleTextArea;
|
||||||
private JButton buttonOK;
|
private JButton buttonOK;
|
||||||
|
private RSA rsa;
|
||||||
|
|
||||||
public MainWindow() {
|
public MainWindow() {
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
@ -48,14 +51,29 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
|
|||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
JButton source = (JButton) e.getSource();
|
JButton source = (JButton) e.getSource();
|
||||||
if (source == this.cryptButton) {
|
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) {
|
} 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) {
|
} 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) {
|
public static void main(String[] args) {
|
||||||
MainWindow dialog = new MainWindow();
|
MainWindow dialog = new MainWindow();
|
||||||
dialog.pack();
|
dialog.pack();
|
||||||
|
62
src/RSA.java
62
src/RSA.java
@ -1,4 +1,3 @@
|
|||||||
public class RSA {
|
|
||||||
// p = 5, q = 11
|
// p = 5, q = 11
|
||||||
// RSA-Modul N = p * q = 55
|
// RSA-Modul N = p * q = 55
|
||||||
// Phi(N) = Phi(p)*Phi(q) | weil eigenschaft eulersche phi funktion und p,q teilerfremd
|
// Phi(N) = Phi(p)*Phi(q) | weil eigenschaft eulersche phi funktion und p,q teilerfremd
|
||||||
@ -19,10 +18,69 @@ public class RSA {
|
|||||||
// ----
|
// ----
|
||||||
// Entschlüsseln
|
// Entschlüsseln
|
||||||
// m = c^d (mod N) => 52^27 (mod 55) = 13 = m
|
// 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