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.

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.

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;
}



}

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()

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.

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

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

commented: Make your own thread, unnecessarily reviving an old one. -1

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.

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

commented: Because you can't take a hint and start a new thread -1

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

Jesus letsae! 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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.