Advertisement
Cryptologica

Caesar Cipher Encryption/Decryption

May 7th, 2012
2,773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package CaesarCipher;
  2.  
  3. import java.awt.Container;
  4. import java.awt.FlowLayout;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.HashMap;
  9. import javax.swing.BorderFactory;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import javax.swing.JTextField;
  17. import javax.swing.ScrollPaneConstants;
  18.  
  19. /**
  20.  * Encrypts and Decrypts text using the Caesar Cihper algorithm.
  21.  * @author Invisible Computer, JTN
  22.  *
  23.  */
  24. public class CaesarGUI extends JFrame implements ActionListener {
  25.  
  26.     private static final long serialVersionUID = 1L;
  27.     private static String alphabet = "abcdefghijklmnopqrstuvwxyz";
  28.     private JTextField shiftFactor;
  29.     private JTextArea inputTA;
  30.     private JTextArea outputTA;
  31.  
  32.  
  33.     /**
  34.      * @param args
  35.      */
  36.     public static void main(String[] args) {
  37.         new CaesarGUI().setVisible(true);
  38.     }
  39.    
  40.     public void encryptText() throws InterruptedException {
  41.         //Create a HashMap
  42.         //A hash map takes keys and values, which are both Characters in this case.
  43.         HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
  44.         int shift;
  45.         //Get the text from the app and store it in a String variable.
  46.         String textNum = this.shiftFactor.getText();
  47.         //Check to see if a "Shift Factor" value was entered.
  48.         //If there wasn't, set shift to zero,
  49.         //Otherwise parse the input value to an integer so we can use it.
  50.         if(!textNum.equals("")){
  51.             shift = Integer.parseInt(textNum)%26;
  52.         }
  53.         else{
  54.             shift = 0;
  55.         }
  56.         //Map every letter of the alphabet to another letter in the alphabet, shifted by x places.
  57.         for(int i=0; i<alphabet.length(); i++){
  58.             alphaMap.put(alphabet.charAt(i), alphabet.charAt((i+shift)%26));
  59.         }
  60.         //Get input text and put it all to lower-case so it's easy to convert
  61.         String inputText = inputTA.getText().toLowerCase();
  62.         String outputText = "";
  63.         //Go to each letter and switch it with it's shifted counterpart
  64.         for(int j=0; j<inputText.length(); j++){
  65.             outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
  66.         }
  67.         //Output the encrypted text
  68.         outputTA.setText(outputText);
  69.     }
  70.    
  71.     public void decryptText() throws InterruptedException{
  72.         HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
  73.         int shift;
  74.         String textNum = this.shiftFactor.getText();
  75.         if(!textNum.equals("")){
  76.             shift = Integer.parseInt(textNum)%26;
  77.         }
  78.         else{
  79.             shift = 0;
  80.         }
  81.         for(int i=0; i<alphabet.length(); i++){
  82.             alphaMap.put(alphabet.charAt((i+shift)%26), alphabet.charAt(i));
  83.         }
  84.         String inputText = inputTA.getText().toLowerCase();
  85.         String outputText = "";
  86.         for(int j=0; j<inputText.length(); j++){
  87.             outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
  88.         }
  89.         outputTA.setText(outputText);
  90.     }
  91.    
  92.     public CaesarGUI(){
  93.         setTitle("Caesar Cipher");
  94.         setVisible(true);
  95.         setDefaultCloseOperation(3);
  96.  
  97.         Container content = getContentPane();
  98.         GridLayout layout = new GridLayout(3, 0, 0, 10);
  99.         content.setLayout(layout);
  100.  
  101.         inputTA = new JTextArea("Insert the text to be encrypted/decrypted here, then press the appropriate button.", 12, 40);
  102.         inputTA.setLineWrap(true);
  103.         inputTA.setWrapStyleWord(true);
  104.         inputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  105.         JScrollPane scroller = new JScrollPane(inputTA);
  106.         scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  107.         content.add(scroller);
  108.        
  109.         outputTA = new JTextArea("Output text.",12, 40);
  110.         outputTA.setLineWrap(true);
  111.         outputTA.setWrapStyleWord(true);
  112.         outputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  113.         JScrollPane scroller2 = new JScrollPane(outputTA);
  114.         scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  115.         content.add(scroller2);
  116.        
  117.         JPanel box1 = new JPanel();
  118.         box1.setLayout(new FlowLayout());
  119.         JButton decryptButton = new JButton("Decrypt");
  120.         JButton encryptButton = new JButton("Encrypt");
  121.         decryptButton.addActionListener(this);
  122.         encryptButton.addActionListener(this);
  123.         box1.add(decryptButton);
  124.         box1.add(encryptButton);
  125.         box1.add(new JLabel("Shift Factor"));
  126.         box1.add(this.shiftFactor = new JTextField(20));
  127.         content.add(box1);
  128.        
  129.         pack();
  130.     }
  131.  
  132.     @Override
  133.     public void actionPerformed(ActionEvent e) {
  134.         if(e.getActionCommand().equals("Encrypt")){
  135.             try{
  136.                 encryptText();
  137.             }
  138.             catch(InterruptedException e1){
  139.                 e1.printStackTrace();
  140.             }
  141.         }
  142.         if (e.getActionCommand().equals("Decrypt"))
  143.               try {
  144.                 decryptText();
  145.               } catch (InterruptedException e1) {
  146.                 e1.printStackTrace();
  147.               }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement