Compare commits

..

2 Commits

Author SHA1 Message Date
332ccf142e Merged basic ui and key generation
Added generate, crypt and decrypt function to ui
2020-05-07 18:49:41 +02:00
dc3918da66 Add basic UI 2020-05-07 18:24:04 +02:00
4 changed files with 278 additions and 16 deletions

View File

@ -1,16 +0,0 @@
import java.math.BigInteger;
public class Main {
static BigInteger cipher (BigInteger message, BigInteger key, BigInteger module) {
return message.modPow(key, module);
}
public static void main(String[] args) {
BigInteger prime = Prime.generate(32);
System.out.println("Finished - number is "+prime);
RSA test = new RSA (32);
test.createKeys();
System.out.println(String.format("public key: 0x%X", test.getPublicKey()));
System.out.println(String.format("private key: 0x%X", test.getPrivateKey()));
System.out.println(String.format("RSA module: 0x%X", test.getRSAModule()));
}
}

190
src/MainWindow.form Normal file
View File

@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="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="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">
<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>
<component id="2e5d8" class="javax.swing.JTextArea" binding="privateKeyTextArea">
<constraints>
<grid row="3" column="1" row-span="1" col-span="2" 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"/>
</properties>
</component>
<component id="17322" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" 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="Public key:"/>
</properties>
</component>
<component id="ffa7c" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Private key:"/>
</properties>
</component>
<component id="4861a" class="javax.swing.JTextField" binding="plainNumber">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="f31a0" class="javax.swing.JTextField" binding="encryptedNumber2">
<constraints>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="4096" class="javax.swing.JButton" binding="cryptButton" default-binding="true">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Crypt"/>
</properties>
</component>
<component id="717e0" class="javax.swing.JButton" binding="decryptButton" default-binding="true">
<constraints>
<grid row="8" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Decrypt"/>
</properties>
</component>
<component id="7dd2a" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" 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="Plain number:"/>
</properties>
</component>
<component id="b4aca" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Encrypted number:"/>
</properties>
</component>
<component id="5d2a6" class="javax.swing.JTextField" binding="encryptedNumber">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="c65b1" class="javax.swing.JLabel">
<constraints>
<grid row="6" column="0" 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="Encrypted number:"/>
</properties>
</component>
<component id="df950" class="javax.swing.JLabel">
<constraints>
<grid row="6" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Decrypted number:"/>
</properties>
</component>
<component id="2a7e2" class="javax.swing.JTextField" binding="decryptedNumber">
<constraints>
<grid row="7" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="bc95a" class="javax.swing.JSlider" binding="keylength">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<paintTicks value="false"/>
</properties>
<clientProperties>
<JSlider.isFilled class="java.lang.Boolean" value="false"/>
</clientProperties>
</component>
<component id="de444" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="1" 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="Key length:"/>
</properties>
</component>
<component id="aeaa8" class="javax.swing.JButton" binding="generateKeysButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generate keys"/>
</properties>
</component>
<component id="8ead4" class="javax.swing.JLabel" binding="keylengthLabel">
<constraints>
<grid row="0" column="2" 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=""/>
</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>
</grid>
</form>

84
src/MainWindow.java Normal file
View File

@ -0,0 +1,84 @@
import javax.swing.*;
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 privateKeyTextArea;
private JTextField plainNumber;
private JTextField encryptedNumber2;
private JButton cryptButton;
private JButton decryptButton;
private JButton generateKeysButton;
private JTextField encryptedNumber;
private JTextField decryptedNumber;
private JSlider keylength;
private JLabel keylengthLabel;
private JTextArea moduleTextArea;
private JButton buttonOK;
private RSA rsa;
public MainWindow() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
generateKeysButton.addActionListener(this);
cryptButton.addActionListener(this);
decryptButton.addActionListener(this);
keylength.setMinimum(8);
keylength.setMaximum(64);
keylength.setMajorTickSpacing(10);
keylength.setMinorTickSpacing(5);
keylength.setValue(32);
keylength.setPaintTicks(true);
keylength.setPaintLabels(true);
keylength.addChangeListener(this);
keylengthLabel.setText(String.valueOf(keylength.getValue()));
}
public void stateChanged(ChangeEvent e){
JSlider source = (JSlider)e.getSource();
if (source == this.keylength) {
int value = source.getValue();
this.keylengthLabel.setText(String.valueOf(value));
}
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source == this.cryptButton) {
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) {
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) {
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();
dialog.setVisible(true);
dialog.setSize(800, 600);
System.exit(0);
}
}

View File

@ -79,4 +79,8 @@ public class RSA {
public BigInteger getRSAModule() { public BigInteger getRSAModule() {
return n; return n;
} }
static BigInteger cipher (BigInteger message, BigInteger key, BigInteger module) {
return message.modPow(key, module);
}
} }