85 lines
3.2 KiB
Java
85 lines
3.2 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 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);
|
|
}
|
|
}
|