Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Willie Howell   on Aug 06 In Java Category.

  
Question Answered By: Vishal Lad   on Aug 06


/*
* 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


Source: http://www.mycoding.net/2011/07/caesar-cipher-program/

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Caesar Cipher Or get search suggestion and latest updates.


Tagged: