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.

96 lines
3.9KB

  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 moveNumberButton;
  22. private JButton buttonOK;
  23. private RSA rsa;
  24. public MainWindow() {
  25. setContentPane(contentPane);
  26. setModal(true);
  27. getRootPane().setDefaultButton(buttonOK);
  28. generateKeysButton.addActionListener(this);
  29. cryptButton.addActionListener(this);
  30. decryptButton.addActionListener(this);
  31. keylength.setMinimum(8);
  32. keylength.setMaximum(50);
  33. keylength.setMajorTickSpacing(10);
  34. keylength.setMinorTickSpacing(5);
  35. keylength.setValue(32);
  36. keylength.setPaintTicks(true);
  37. keylength.setPaintLabels(true);
  38. keylength.addChangeListener(this);
  39. keylengthLabel.setText(String.valueOf(keylength.getValue()));
  40. moveNumberButton.addActionListener(this);
  41. }
  42. public void stateChanged(ChangeEvent e){
  43. JSlider source = (JSlider)e.getSource();
  44. if (source == this.keylength) {
  45. int value = source.getValue();
  46. this.keylengthLabel.setText(String.valueOf(value));
  47. }
  48. }
  49. public void actionPerformed(ActionEvent e) {
  50. JButton source = (JButton) e.getSource();
  51. if (source == this.cryptButton) {
  52. try {
  53. BigInteger message = new BigInteger(this.plainNumber.getText());
  54. BigInteger key = new BigInteger(this.publicKeyTextArea.getText());
  55. BigInteger module = new BigInteger(this.moduleTextArea.getText());
  56. String encrypted = RSA.cipher(message, key, module).toString();
  57. this.encryptedNumber.setText(encrypted);
  58. } catch (NumberFormatException ex) {
  59. JOptionPane.showMessageDialog(this, "Invalid value(s), check your input and try again!", "RSA Test", JOptionPane.ERROR_MESSAGE);
  60. }
  61. } else if (source == this.decryptButton) {
  62. try {
  63. BigInteger encryNumber = new BigInteger(this.encryptedNumber2.getText());
  64. BigInteger key = new BigInteger(this.privateKeyTextArea.getText());
  65. BigInteger module = new BigInteger(this.moduleTextArea.getText());
  66. String decrypted = RSA.cipher(encryNumber, key, module).toString();
  67. this.decryptedNumber.setText(decrypted);
  68. } catch (NumberFormatException ex) {
  69. JOptionPane.showMessageDialog(this, "Invalid value(s), check your input and try again!", "RSA Test", JOptionPane.ERROR_MESSAGE);
  70. }
  71. } else if (source == this.generateKeysButton) {
  72. this.rsa = new RSA(this.keylength.getValue());
  73. this.rsa.createKeys();
  74. this.publicKeyTextArea.setText(this.rsa.getPublicKey()+"");
  75. this.privateKeyTextArea.setText(this.rsa.getPrivateKey()+"");
  76. this.moduleTextArea.setText(this.rsa.getRSAModule()+"");
  77. } else if (source == this.moveNumberButton) {
  78. this.encryptedNumber2.setText(this.encryptedNumber.getText());
  79. }
  80. }
  81. public static void main(String[] args) {
  82. MainWindow dialog = new MainWindow();
  83. dialog.pack();
  84. dialog.setVisible(true);
  85. dialog.setSize(800, 600);
  86. System.exit(0);
  87. }
  88. }