Add move number to encrypted text field 2 button

Limit length of keys to 50 bits
Add basic input validation
This commit is contained in:
Christian Loch 2020-05-07 19:06:13 +02:00 committed by Loch Christian (uib05376)
parent 332ccf142e
commit 225c87bc31
2 changed files with 81 additions and 60 deletions

View File

@ -60,14 +60,6 @@
</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"/>
@ -76,14 +68,6 @@
<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"/>
@ -92,14 +76,6 @@
<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">
@ -116,22 +92,6 @@
<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"/>
@ -143,14 +103,6 @@
<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"/>
@ -181,8 +133,66 @@
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties>
<lineWrap value="true"/>
</properties>
</component>
<component id="2a7e2" class="javax.swing.JTextField" binding="decryptedNumber">
<constraints>
<grid row="5" column="3" 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="df950" class="javax.swing.JLabel">
<constraints>
<grid row="4" 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="Decrypted number:"/>
</properties>
</component>
<component id="b4aca" class="javax.swing.JLabel">
<constraints>
<grid row="6" 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="Encrypted number:"/>
</properties>
</component>
<component id="f31a0" class="javax.swing.JTextField" binding="encryptedNumber2">
<constraints>
<grid row="7" column="3" 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="e0956" class="javax.swing.JButton" binding="moveNumberButton">
<constraints>
<grid row="7" column="1" 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="-------------------&gt;"/>
</properties>
</component>
<component id="717e0" class="javax.swing.JButton" binding="decryptButton" default-binding="true">
<constraints>
<grid row="8" column="3" 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="Decrypt"/>
</properties>
</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>
</children>
</grid>
</children>

View File

@ -19,6 +19,7 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
private JSlider keylength;
private JLabel keylengthLabel;
private JTextArea moduleTextArea;
private JButton moveNumberButton;
private JButton buttonOK;
private RSA rsa;
@ -30,7 +31,7 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
cryptButton.addActionListener(this);
decryptButton.addActionListener(this);
keylength.setMinimum(8);
keylength.setMaximum(64);
keylength.setMaximum(50);
keylength.setMajorTickSpacing(10);
keylength.setMinorTickSpacing(5);
keylength.setValue(32);
@ -38,6 +39,7 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
keylength.setPaintLabels(true);
keylength.addChangeListener(this);
keylengthLabel.setText(String.valueOf(keylength.getValue()));
moveNumberButton.addActionListener(this);
}
public void stateChanged(ChangeEvent e){
@ -51,18 +53,26 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
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);
try {
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);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid value(s), check your input and try again!", "RSA Test", JOptionPane.ERROR_MESSAGE);
}
} 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);
try {
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);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid value(s), check your input and try again!", "RSA Test", JOptionPane.ERROR_MESSAGE);
}
} else if (source == this.generateKeysButton) {
this.rsa = new RSA(this.keylength.getValue());
@ -70,10 +80,11 @@ public class MainWindow extends JDialog implements ChangeListener, ActionListene
this.publicKeyTextArea.setText(this.rsa.getPublicKey()+"");
this.privateKeyTextArea.setText(this.rsa.getPrivateKey()+"");
this.moduleTextArea.setText(this.rsa.getRSAModule()+"");
} else if (source == this.moveNumberButton) {
this.encryptedNumber2.setText(this.encryptedNumber.getText());
}
}
public static void main(String[] args) {
MainWindow dialog = new MainWindow();
dialog.pack();