Encryptor/Decryptor (Caesar Cipher)

Platform:  Java
Published  Jul 16, 2009
Updated  Jul 16, 2009
This is a Swing java program that uses the Caesar Cipher method to encrypt or decrypt text. And I used the GridBagLayout layout manager so I got some practice with that.

"It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet"

Here's an image that encrypts with a key of 2:



Here's some screens:



/*
* CaesarCipher.java
* Encrypts/Decrypts text using the Caesar Cipher method
* Made by MountainDew
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CaesarCipher extends JFrame implements ActionListener {

private static JLabel msgLabel = new JLabel("Message: ");
private static JLabel keyLabel = new JLabel("Key: ");
private static JLabel actionLabel = new JLabel("Action: ");
private static JLabel resultLabel = new JLabel("Result: ");
private static JTextField msgTextField = new JTextField(20);
private static JTextField resultTextField = new JTextField(20);
private static JSpinner keySpinner = new JSpinner( new SpinnerNumberModel(3, 1, 25, 1) );
private static JRadioButton encryptRadio = new JRadioButton("Encrypt");
private static JRadioButton decryptRadio = new JRadioButton("Decrypt");
private static JButton actionButton = new JButton("Encrypt Message");
private static JPanel panel = new JPanel();
private static ButtonGroup group = new ButtonGroup();

public static void main(String[] args) {
new CaesarCipher();
}

public CaesarCipher() {
this.setSize(310, 192);
this.setTitle("Caesar Cipher");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);

panel.setLayout(new GridBagLayout());

addComponent(panel, msgLabel, 0, 0, 1, 1, GridBagConstraints.LINE_START);
addComponent(panel, msgTextField, 1, 0, 2, 1, GridBagConstraints.LINE_START);

addComponent(panel, keyLabel, 0, 1, 1, 1, GridBagConstraints.LINE_START);
addComponent(panel, keySpinner, 1, 1, 1, 1, GridBagConstraints.LINE_START);

addComponent(panel, actionLabel, 0, 2, 1, 1, GridBagConstraints.LINE_START);
group.add(encryptRadio);
group.add(decryptRadio);
addComponent(panel, encryptRadio, 1, 2, 1, 1, GridBagConstraints.LINE_START);
addComponent(panel, decryptRadio, 2, 2, 1, 1, GridBagConstraints.LINE_START);
encryptRadio.setSelected(true);
encryptRadio.addActionListener(this);
decryptRadio.addActionListener(this);

addComponent(panel, resultLabel, 0, 3, 1, 1, GridBagConstraints.LINE_START);
addComponent(panel, resultTextField, 1, 3, 2, 1, GridBagConstraints.LINE_START);
resultTextField.setEditable(false);

addComponent(panel, actionButton, 1, 4, 1, 1, GridBagConstraints.CENTER);
actionButton.addActionListener(this);

this.add(panel);
this.setVisible(true);
}

private void addComponent(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}

private void encryptMessage(String msg, int k) {
String result = "";
resultTextField.setText("");
for (int i = 0; i < msg.length(); i++)
result += encryptChar(msg.charAt(i), k);
resultTextField.setText(result);
}

private char encryptChar(char c, int k) {
if (Character.isLetter(c))
return (char) ('A' + (c - 'A' + k) % 26);
else
return c;
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == encryptRadio)
actionButton.setText("Encrypt Message");

if (e.getSource() == decryptRadio)
actionButton.setText("Decrypt Message");

if (e.getSource() == actionButton) {
String str = msgTextField.getText();
int k = (Integer) keySpinner.getValue();
int key = 0;
String message = "";

if (str.equals("")) {
JOptionPane.showMessageDialog(null, "Please enter a message!", "Error!", JOptionPane.ERROR_MESSAGE);
msgTextField.requestFocus();
return;
}

message = str.toUpperCase();
if (encryptRadio.isSelected())
key = k;
else
key = 26 - k;

encryptMessage(message, key);
}
}
}

Comments

Sign in to comment.
alonely1990   -  Oct 18, 2011
Thank you so much. Have a nice day.
 Respond  
utjup   -  Apr 07, 2011
nuhun dulur.... kapake pisan uy... koding na keren... mansteb....
sukses selalu......
 Respond  
jmarsiglio   -  May 01, 2010
Hey man, I don't get what happens in here:

Code

 


I'm pretty smart, but errr I don't get this :( I feel really stupid aha. Why do we need to add 'A' and then subtract it? Isn't that canceled out? Or does the modulus have precedence... Please help!

*EDIT: nvm I figured out what you did, but how did your figure that out? :O very smart kid you are :) but srsly.. how?

*EDIT: Btw, I absolutely love your program, I learned a lot from it. Would you mind if I use this in my computer class project (applet). It will be 1 of 2 applets in my html page. Speaking of which... I should learn more about html :) Cheers!
 Respond  
mountaindew   -  Mar 29, 2010
Well I did, but this is like really simple encryption:

Code

 

I'm not really sure how you'd go about coding a more complex encryption.
 Respond  
verbalh   -  Mar 29, 2010
I thought you created the program. i just want to write based on specs I stated above. Thanks
 Respond  
mountaindew   -  Mar 29, 2010
I'm not really sure if I'd be able to help you with that, I don't know very much about encryption.
 Respond  
verbalh   -  Mar 25, 2010
Hi mountaindew

This is really nice. I am new to Java and was wondering how to modify to make it more complex by using random sequence and character insertion.

For example, for every 5th character we insert random ‘junk’ with any random sequence 2-letter, 3-letter just make it hard to catch on.

Your help would be greatly appreciated

Thanks

Verbalh
 Respond  
asakura   -  Aug 05, 2009
lol dw you dont get it :D

daymn thats cool

me myself am lazy n would of just used $replace :D but you know :D
 Respond  
mountaindew   -  Aug 03, 2009

Quote


ehh
if you wanna bitch about someone do it in pm ;) lol

What are you talking about?
 Respond  
asakura   -  Aug 02, 2009
ehh
if you wanna bitch about someone do it in pm ;) lol
 Respond  
mountaindew   -  Jul 17, 2009
Yeah that's mainly why I did it, to get some practice with Swing and the layout managers.
 Respond  
Hawkee   -  Jul 17, 2009
While the encryption is fairly simple, its a nice example of how to build a Java GUI.
 Respond  
Aucun50   -  Jul 17, 2009
The encrypt almost looks like a keygen for a game :)
 Respond  
zNigel-   -  Jul 16, 2009
wow.. Looks really nice and can come handy for me sometimes :) +like
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.