You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.2KB

  1. import javax.swing.*;
  2. import javax.swing.event.ChangeEvent;
  3. import javax.swing.event.ChangeListener;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.math.BigInteger;
  7. public class MainWindow extends JDialog implements ChangeListener, ActionListener {
  8. private JPanel contentPane;
  9. private JTextArea publicKeyTextArea;
  10. private JTextArea privateKeyTextArea;
  11. private JTextField plainNumber;
  12. private JTextField encryptedNumber2;
  13. private JButton cryptButton;
  14. private JButton decryptButton;
  15. private JButton generateKeysButton;
  16. private JTextField encryptedNumber;
  17. private JTextField decryptedNumber;
  18. private JSlider keylength;
  19. private JLabel keylengthLabel;
  20. private JTextArea moduleTextArea;
  21. private JButton buttonOK;
  22. private RSA rsa;
  23. public MainWindow() {
  24. setContentPane(contentPane);
  25. setModal(true);
  26. getRootPane().setDefaultButton(buttonOK);
  27. generateKeysButton.addActionListener(this);
  28. cryptButton.addActionListener(this);
  29. decryptButton.addActionListener(this);
  30. keylength.setMinimum(8);
  31. keylength.setMaximum(64);
  32. keylength.setMajorTickSpacing(10);
  33. keylength.setMinorTickSpacing(5);
  34. keylength.setValue(32);
  35. keylength.setPaintTicks(true);
  36. keylength.setPaintLabels(true);
  37. keylength.addChangeListener(this);
  38. keylengthLabel.setText(String.valueOf(keylength.getValue()));
  39. }
  40. public void stateChanged(ChangeEvent e){
  41. JSlider source = (JSlider)e.getSource();
  42. if (source == this.keylength) {
  43. int value = source.getValue();
  44. this.keylengthLabel.setText(String.valueOf(value));
  45. }
  46. }
  47. public void actionPerformed(ActionEvent e) {
  48. JButton source = (JButton) e.getSource();
  49. if (source == this.cryptButton) {
  50. BigInteger message = new BigInteger(this.plainNumber.getText());
  51. BigInteger key = new BigInteger(this.publicKeyTextArea.getText());
  52. BigInteger module = new BigInteger(this.moduleTextArea.getText());
  53. String encrypted = RSA.cipher(message, key, module).toString();
  54. this.encryptedNumber.setText(encrypted);
  55. } else if (source == this.decryptButton) {
  56. BigInteger encryNumber = new BigInteger(this.encryptedNumber2.getText());
  57. BigInteger key = new BigInteger(this.privateKeyTextArea.getText());
  58. BigInteger module = new BigInteger(this.moduleTextArea.getText());
  59. String decrypted = RSA.cipher(encryNumber, key, module).toString();
  60. this.decryptedNumber.setText(decrypted);
  61. } else if (source == this.generateKeysButton) {
  62. this.rsa = new RSA(this.keylength.getValue());
  63. this.rsa.createKeys();
  64. this.publicKeyTextArea.setText(this.rsa.getPublicKey()+"");
  65. this.privateKeyTextArea.setText(this.rsa.getPrivateKey()+"");
  66. this.moduleTextArea.setText(this.rsa.getRSAModule()+"");
  67. }
  68. }
  69. public static void main(String[] args) {
  70. MainWindow dialog = new MainWindow();
  71. dialog.pack();
  72. dialog.setVisible(true);
  73. dialog.setSize(800, 600);
  74. System.exit(0);
  75. }
  76. }