This program is to demonstrate the working of the Caesar Cipher, which is the simplest encryption technique. This program will convert plain text to cipher text and vice-versa.
The program runs through two loops. One to traverse each alphabet of the text and the other to compare the alphabet with alphabets from a-z and then do the needed replacements.
Program:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package caesarcipher; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * * @author Vishal */ public class CaesarCipher { JPanel encrypt,decrypt,butPanel,butPanel2; JTextField txtEncrypt,txtDecrypt,txtEncrypt1,txtDecrypt1; JButton butEncrypt,butDecrypt; CaesarCipher(){ JFrame main = new JFrame("Caesar Cipher - MyCoding.net"); main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); encrypt = new JPanel(new GridLayout(3,1,1,1)); decrypt = new JPanel(new GridLayout(3,1,1,1)); butPanel = new JPanel(); butPanel2 = new JPanel(); butEncrypt = new JButton("Encrypt"); butDecrypt = new JButton("Decrypt"); main.setLocation(200, 300); main.setVisible(true); main.setLayout(new BorderLayout()); main.add(encrypt,BorderLayout.EAST); main.add(decrypt, BorderLayout.WEST); txtEncrypt = new JTextField(15); txtDecrypt = new JTextField(15); txtEncrypt1 = new JTextField(15); txtDecrypt1 = new JTextField(15); decrypt.add(new JLabel("Plain Text: ")); decrypt.add(txtEncrypt); decrypt.add(new JLabel("Cipher Text: ")); decrypt.add(txtDecrypt); decrypt.add(butPanel); butPanel.add(butEncrypt); butEncrypt.addActionListener(new Convert()); encrypt.add(new JLabel("Cipher Text: ")); encrypt.add(txtDecrypt1); encrypt.add(new JLabel("Plain Text: ")); encrypt.add(txtEncrypt1); encrypt.add(butPanel2); butPanel2.add(butDecrypt); butDecrypt.addActionListener(new Convert()); main.pack(); } //End of Constructor /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here new CaesarCipher(); } //End main public class Convert implements ActionListener{ char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; String replace[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; public void actionPerformed(ActionEvent ae){ if(ae.getSource()== butEncrypt){ char[] plain = txtEncrypt.getText().toLowerCase().toCharArray(); String cipher = null; for(int i = 0;i<plain.length;i++){ for(int j = 0 ; j<26;j++){ if(j<=22){ if(plain[i]==chars[j]){ plain[i] = chars[j+3]; break; } }//End nested If else if(plain[i] == chars[j]){ plain[i] = chars [j-23]; } //End else } //End nested for loop } //End of For loop cipher = String.valueOf(plain); txtDecrypt.setText(cipher); } //End if if(ae.getSource() == butDecrypt){ char[] cipher = txtDecrypt1.getText().toLowerCase().toCharArray(); for(int i = 0;i<cipher.length;i++){ for(int j = 0 ; j<26;j++){ if(j>=3 && cipher[i]==chars[j]){ cipher[i] = chars[j-3]; break; } if(cipher[i] == chars[j] && j<3){ System.out.println("Replacing "+cipher[i]+" by "+chars[23+j]+" "+j+23+""); cipher[i] = chars[23+j]; break; } //End IF } //End nested for loop } //End of For loop txtEncrypt1.setText(String.valueOf(cipher)); }//End else //Convert cipher text to plain text }//End actionPerformed } //End class Convertor } //End class CaesarCipher |
Vlad
Latest posts by Vlad (see all)
- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013