RSA/src/MainWindow.java
Christian Loch 225c87bc31 Add move number to encrypted text field 2 button
Limit length of keys to 50 bits
Add basic input validation
2020-05-07 19:06:13 +02:00

96 lines
3.9 KiB
Java

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 moveNumberButton;
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(50);
keylength.setMajorTickSpacing(10);
keylength.setMinorTickSpacing(5);
keylength.setValue(32);
keylength.setPaintTicks(true);
keylength.setPaintLabels(true);
keylength.addChangeListener(this);
keylengthLabel.setText(String.valueOf(keylength.getValue()));
moveNumberButton.addActionListener(this);
}
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) {
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) {
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());
this.rsa.createKeys();
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();
dialog.setVisible(true);
dialog.setSize(800, 600);
System.exit(0);
}
}