954,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Caesar Cipher program

I am hoping I can get some help from you guys, I am taking a computer security course and we need to write a Caesar Cipher program, where you can input the text and offset and get the the encrypted results back. I have run into a problem with it . I have not taken java in a year and so far can not find my old files or book for that matter. let me show you what I have pieced together. I believe I need to use a String Tokenizer to break up the letters.

import javax.swing.*;
   import java.io.*;
/**
*Homework #1 CaesarCipher.java
*program to encrypt and decrypt stuff
*@ author Jason Rasmussen
*/
   public class ceasar1
   {
      public String dataencyptS, sInt;
      public int offsetI;
   	
      public void main(String [] args)
      {
      
         dataencyptS=JOptionPane.showInputDialog("Input Data to encypt:");
         sInt=JOptionPane.showInputDialog("Input the key offset:");
      
         offsetI = Integer.parseInt( sInt );
      }
   
   
      private void translate(int offsetI) {
         char c;
         while ((byte)(c = getNextChar()) != -1) {
            if (Character.isLowerCase(c)) {
               c = rotate(c, offsetI);                
            }
            System.out.print(c);
         }
      }
   
   
      public char getNextChar() {
         char ch = ' ';
         try {
         
            ch = (char)dataencyptS.read();
         } 
            catch (IOException e) {
               System.out.println("Exception reading character");
            }
         return ch;
      }
   // rotate: translate using rotation, version with table lookup
      public char rotate(char c, int offsetI) { // c must be lowercase
         String s = "abcdefghijklmnopqrstuvwxyz";
      	
         int i = 0;
         while (i < 26) {
           // extra +26 below because key might be negative
            if (c == s.charAt(i)) 
               return s.charAt((i + offsetI + 26)%26);
            i++;
         }
         return c;
      }
   
   
   
   }

under get next char there is a read() that I do not think is right, the person I was working of this with suggest it. I thought read() is only for when you are pulling info from a file. I am a bit of a newbie when it comes to programming java. Any help would be appreciated thanks.

rasputinj
Junior Poster
103 posts since Sep 2003
Reputation Points: 15
Solved Threads: 3
 

check out what i do, string tokenizer is used to break up a string by sub strings

not b char's. lets say is u have a string like this
"my name is ceasar"

if u use the string tokenizer then this would be 4 strings given that u use the
delimiter to be a empty space.

my
name
is
ceasar

lets say for example
StringTokenizer st = new StringTokenizer("my name is ceasar"," ", false);

while( st.hasMoreTokens() )
System.out.println(st.nextToken());

in the above 3 lines this should tell all on how to use the string tokenizer.

here is a sample of how to encypt using the ceasar cipher but i did the the
encrypt part the decrypt i leave to u.

import javax.swing.*;

public class ceasar2

{

private String inputString;

private int intOffset;

public ceasar2()

{

inputString = null;

intOffset = 0;

}

public static void main(String [] args)

{

ceasar2 c2 = new ceasar2();

String tmp = c2.encrypt();

System.out.println( tmp );

//System.out.println( decrypt( tmp ) );

System.exit(0);

}

public String encrypt()

{

inputString = ((String)JOptionPane.showInputDialog("enter string to encrypt") ).toLowerCase().trim();

intOffset = Integer.parseInt(JOptionPane.showInputDialog("enter offset") );

String letters = "abcdefghijklmnopqrstuvwxyz";

StringBuffer sb = new StringBuffer();

int holder = inputString.length();

for(int i = 0; i < holder; i++)

{

String tmp = ""+inputString.charAt(i);

int offset = letters.indexOf(tmp);

offset += intOffset;

if( offset > 25 )

{

int newOffset = 0;

newOffset = offset % 25;

sb.append( letters.charAt(newOffset) );

}

else

{

sb.append( letters.charAt(offset) );

}

//

}//forloop

return sb.toString();

}

/*

public String decrypt( String input )

{

}

*/

}//class

try it to decrypt u will have to work backwords with the same offset that u encrypted , but i leave that up to u.

xlogan777
Newbie Poster
15 posts since Jan 2004
Reputation Points: 10
Solved Threads: 0
 

I had to do a program using a general Caesar Cipher. This program encrypts and decrypts together. Here you go:

/*
Programmer: Nick Anderson
Date: January 27, 2008
Filename: Hw3
Description: To encrypt and decrypt using a Casesar Cipher
*/


public class CaesarCipher
{

public static void main(String[] args) {
String str = "Nick Anderson";
int key = 3;

String encrypted = encrypt(str, key);
System.out.println(encrypted);

String decrypted = decrypt(encrypted, key);
System.out.println(decrypted);
}

public static String encrypt(String str, int key) {
String encrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
} else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}

public static String decrypt(String str, int key)
{
String decrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}

}

natedoggy
Newbie Poster
1 post since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
check out what i do, string tokenizer is used to break up a string by sub strings

StringTokenizer has been deprecated for years now, you should use String.split()

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 
StringTokenizer has been deprecated for years now, you should use String.split()


That post was from Feb 2004 :)

Also, the one that nate tacked on to this little piece of history should probably use StringBuilder instead of += for constructing the string.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

well, StringTokenizer had been deprecated for years back in 2004 ;)

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hi there, could you please help me with a java code to break the Caesar cipher using brute force

letsae
Newbie Poster
2 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 
Hi there, could you please help me with a java code to break the Caesar cipher using brute force

What do you think you are doing bringing back to life a thread that is more than five years old and has been dead for almost a year now.

If you want to ask for some help which you think might be mentioned here read the thread and the answers to it, but to post you should start a new thread.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

could you please help me with a java code for connecting a client and a server?

letsae
Newbie Poster
2 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 
could you please help me with a java code for connecting a client and a server?

Jesusletsae! Haven't read verruckt24's post. Don't you know how to create a new thread?

This is a dead thread not a make-a-wish well

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
// Emran T. Atatri
// Jenin - Palestine
// <<mail removed>>
// this is my solution of caeser cipher, but you can use a key



public class Decrypt{
public static void main(String[] args){
	Decrypt d = new Decrypt();
	d.ne();
	d.de();
}
char[] n = {'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',
'0','1','2','3','4','5','6','7','8','9',' ','.','-','_','!','@','#','$','%','^','&','*','(',')',':','<','>','?','/'};
String s0 = "Bullets may kill ... Bones may break ... Damar7 0 1 2 3 4 5 6 7 8 9"; //"ibrahim";
String s = s0.toLowerCase();
String k = "atatri";
String d = "";
String d2 = "";
char x,h;
int j,f;
public Decrypt(){
	//System.out.println(""+n.length);
}
public void ne(){
	d = "";
for(int i=0;i<s.length();i++){
	x = s.charAt(i);
	j = numberOfChar(x);
	System.out.println(x+"  "+f);
	f = (j + 19)%n.length;
	h = charOfnumber(f);
	System.out.println(h+"  "+f);
	d += h;
	}
System.out.println(d);
	}
public void de(){
	//d = "";
for(int i=0;i<d.length();i++){
	x = d.charAt(i);
	j = numberOfChar(x);
	System.out.println(x+"  "+f);
	f = (j - 19 + n.length)%n.length;
	h = charOfnumber(f);
	System.out.println(h+"  "+f);
	d2 += h;
	}
System.out.println(d2);
	}
public int numberOfChar(char m){
	int z = 0;
	for(int i=0;i<n.length;i++){
		if(n[i] == m)
		z = i;
		}
		return z;
	}
public char charOfnumber(int m){
	char z = n[m];
	return z;
	}
}
//pi = ci - 19 + 26 mod 26; -- ctosbci key = student;
fatal_error0
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

i lost the key to decipher the text and how should i know which key is the exact key
if i use brute force type to decrypt the text,how should i know which text is the actual text that came after decryption

abcd1220
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You