From 225c87bc31d319faac5f8dfb703ea1dbc9a7d547 Mon Sep 17 00:00:00 2001 From: Christian Loch Date: Thu, 7 May 2020 19:06:13 +0200 Subject: [PATCH] Add move number to encrypted text field 2 button Limit length of keys to 50 bits Add basic input validation --- src/MainWindow.form | 106 ++++++++++++++++++++++++-------------------- src/MainWindow.java | 35 ++++++++++----- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/MainWindow.form b/src/MainWindow.form index 1b6c41e..3d59136 100644 --- a/src/MainWindow.form +++ b/src/MainWindow.form @@ -60,14 +60,6 @@ - - - - - - - - @@ -76,14 +68,6 @@ - - - - - - - - @@ -92,14 +76,6 @@ - - - - - - - - @@ -116,22 +92,6 @@ - - - - - - - - - - - - - - - - @@ -143,14 +103,6 @@ - - - - - - - - @@ -181,8 +133,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/MainWindow.java b/src/MainWindow.java index 225c02b..c55adee 100644 --- a/src/MainWindow.java +++ b/src/MainWindow.java @@ -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();