Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Caesar Cipher

  Asked By: Willie    Date: Jun 16    Category: Java    Views: 6421
  

I'm really a newbie to Java and am taking a class on it now. I was
wondering if anyone could help me out. I'm trying to write a Caesar
Cipher program in Java. All the program has to do is encode input
text and show it on the screen. I have to use my own encryption
though.

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Madeeha Malik     Answered On: Jun 16

there is a webpage on the internet that we call "google".
It is a good example of what we call a "search engine".
You can access it from the address http://www.google.com
Ý even did a search for you, you can reach it from:
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=Caesar+Cipher
if you want the same results, you can fill in the textbox in the
middle of the page, right above the two buttons:
"Google Search" and "I'm Feeling Lucky". You see?

If you *want* to learn java, you have to *think* about
your homework *yourself*. Not ask for anybody else to do it.

There are even people who are learning java  just by reading
tutorials and books, at least you have classnotes and teachers.

And searching from google is even easier than posting a mail
to a maillist and waiting for an answer.

 
Answer #2    Answered By: Aaminah Khan     Answered On: Jun 16

If I remember correctly, the Caesar Cipher is a very basic encryption
algorithm. All you need to do is add any constant number to each character,
then mod Character.MAX_VALUE to make sure it doesn't go over the boundaries,
e.g.
newchar = (oldchar + 10) % Character.MAX_VALUE;

 
Answer #3    Answered By: Anne Powell     Answered On: Jun 16

well for your information I've already looked on google, and my
teacher is no help  really. he's only keeps himself 1 lesson ahead of
us in java, and we have no text books because the district is too
cheap for the compsci classes seeing as how they don't like to fund
the computer science departments at the schools here. And state funds
for the schools have been cut so we have no books for this.

 
Answer #4    Answered By: Tate Thompson     Answered On: Jun 16

Caesar Cypher is one the algorithm technique which is used for encryption the
data.
I also done course namely "Network Security" in which i studies many encryption
algorithm technique...
No-one can tell u on a single email about Caesar Cypher becoz algorithm can be
only learn by books or any friend face to face.
If u need help  from me i can recommend a book for u tht we read as a course book
in which many alogs have discuss breifly...
now im in office i dont remeber itz name, if u really wants then reply me i will
definitely help u if u want to call me ur welcome.

 
Answer #5    Answered By: Charlie Evans     Answered On: Jun 16

the following code gets the multiplier and constant from the user and uses
them for encoding.


import java.io.*;

public class  CaesarCipher
{
private static int multi;
private static int constant;

private static int findNum(char a)
{
String S="abcdefghijklmnopqrstuvwxyz";
int ret_val=S.indexOf(a);
return ret_val;
}

private static char findChar(int a)
{
String S="abcdefghijklmnopqrstuvwxyz";
char ret_val=S.charAt(a);
return ret_val;
}


public static void initMultiplier() throws IOException
{
String S;
DataInputStream dis = new DataInputStream(System.in);
System.out.println(" Enter the multiplier ");
S=dis.readLine();
multi=Integer.parseInt(S);
}

public static void initConstant() throws IOException
{
String S;
DataInputStream dis = new DataInputStream(System.in);
System.out.println(" Enter the constant ");
S=dis.readLine();
constant=Integer.parseInt(S);
}

public static String encoder(String c)
{
int coded;
int temp;
StringBuffer S=new StringBuffer();
String C;
c=c.toLowerCase();
for(int index=0;index < c.length();index++)
{
temp=findNum(c.charAt(index));
if(temp!=-1)
{
coded = multi * findNum(c.charAt(index)) + constant;
if(coded > 25)
{
temp = coded/26;
coded = coded - (temp*26);
}
S.append(findChar(coded));
}
else
S.append(" ");
}
C=new String(S);
C=C.toUpperCase();
return C;
}

public static String decoder(String C)
{
double decoded;
String c;
StringBuffer s=new StringBuffer();
double multi_recip = 1.0 / multi;

C=C.toLowerCase();
for(int index=0; index < C.length(); index++)
{
decoded = findNum(C.charAt(index));
if(decoded!=-1)
{
decoded=decoded - constant;
while(decoded % multi != 0 || decoded < 0)
{
decoded=decoded+26;
}
decoded = decoded / multi;
if(decoded > 25)
{
multi_recip = decoded/26;
decoded = decoded - (multi_recip*26);
}
s.append(findChar((int)decoded));
}
else
s.append(" ");
}
c=new String(s);
c=c.toUpperCase();
return c;
}


public static void main(String[] args)
{
String S;
String encoded;
String decoded;
DataInputStream dis=new DataInputStream(System.in);

try
{
S=dis.readLine();
System.out.println("String ="+ S);
initMultiplier();
initConstant();
encoded=encoder(S);
System.out.println(" Encoded format ="+ encoded);
decoded=decoder(encoded);
System.out.println(" Decoded format ="+ decoded);
}
catch(IOException e)
{
e.printStackTrace();
}

}
}

 
Answer #6    Answered By: Vishal Lad     Answered 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/

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

Related Topics:



Tagged:    

 

Related Post