Advertisement
Ali-S0

mixed_cipher

May 5th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. //package mixed_cipher;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.lang.String;
  6. import javax.swing.*;
  7. public class Mixed {
  8.     static String pln = "abcdefghijklmnopqrstuvwxyz";
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated constructor stub
  11.         JFrame frame = new JFrame("Mixed");
  12.         frame.setLayout(new GridLayout(4, 2));
  13.         frame.setSize(500, 150);
  14.         JTextField plaintext = new JTextField();
  15.         JTextField ciphertext = new JTextField();
  16.         JLabel plaintxt = new JLabel("Plaintext");
  17.         JLabel ciphertxt = new JLabel("Ciphertext");
  18.         JLabel encrypted = new JLabel("Encrypted");
  19.         JLabel decrypted = new JLabel("Decrypted");
  20.         JButton encrypt = new JButton("Encrypt");
  21.         JButton decrypt = new JButton("Decrypt");
  22.         frame.add(plaintxt);
  23.         frame.add(plaintext);
  24.         frame.add(ciphertxt);
  25.         frame.add(ciphertext);
  26.         frame.add(encrypt);
  27.         frame.add(encrypted);
  28.         frame.add(decrypt);
  29.         frame.add(decrypted);
  30.         frame.setVisible(true);
  31.         encrypt.addActionListener(new ActionListener() {
  32.             @Override
  33.             public void actionPerformed(ActionEvent arg0) {
  34.                 String plain = plaintext.getText().toString();
  35.                 String cph = ciphertext.getText().toString();
  36.                 String ciph = "";
  37.                 String encpt = "";
  38.                 for(int i = 0; i < cph.length(); ++i) {
  39.                     if(ciph.indexOf(cph.charAt(i)) == -1){
  40.                         ciph += cph.charAt(i);
  41.                     }
  42.                 }
  43.                 for(int i = 0; i < pln.length(); ++i) {
  44.                     if(ciph.indexOf(pln.charAt(i)) == -1){
  45.                         ciph += pln.charAt(i);
  46.                     }
  47.                 }
  48.                 for(int i = 0; i < plain.length(); ++i) {
  49.                     encpt += ciph.charAt(pln.indexOf(plain.charAt(i)));
  50.                 }
  51.                 encrypted.setText(encpt);
  52.             }
  53.         });
  54.         decrypt.addActionListener(new ActionListener() {
  55.             @Override
  56.             public void actionPerformed(ActionEvent arg0) {
  57.                 // TODO Auto-generated method stub
  58.                 String cph = ciphertext.getText().toString();
  59.                 String ciph = "";
  60.                 String decpt = "";
  61.                 for(int i = 0; i < cph.length(); ++i) {
  62.                     if(ciph.indexOf(cph.charAt(i)) == -1){
  63.                         ciph += cph.charAt(i);
  64.                     }
  65.                 }
  66.                 for(int i = 0; i < pln.length(); ++i) {
  67.                     if(ciph.indexOf(pln.charAt(i)) == -1){
  68.                         ciph += pln.charAt(i);
  69.                     }
  70.                 }
  71.                 for(int i = 0; i < encrypted.getText().length(); ++i) {
  72.                     decpt += pln.charAt(ciph.indexOf(encrypted.getText().charAt(i)));
  73.                 }
  74.                 decrypted.setText(decpt);
  75.             }
  76.         });
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement