Need help with Caesar Cipher program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2003
Posts: 103
Reputation: rasputinj is an unknown quantity at this point 
Solved Threads: 3
rasputinj rasputinj is offline Offline
Junior Poster

Need help with Caesar Cipher program

 
0
  #1
Sep 19th, 2003
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.

  1. import javax.swing.*;
  2. import java.io.*;
  3. /**
  4. *Homework #1 CaesarCipher.java
  5. *program to encrypt and decrypt stuff
  6. *@ author Jason Rasmussen
  7. */
  8. public class ceasar1
  9. {
  10. public String dataencyptS, sInt;
  11. public int offsetI;
  12.  
  13. public void main(String [] args)
  14. {
  15.  
  16. dataencyptS=JOptionPane.showInputDialog("Input Data to encypt:");
  17. sInt=JOptionPane.showInputDialog("Input the key offset:");
  18.  
  19. offsetI = Integer.parseInt( sInt );
  20. }
  21.  
  22.  
  23. private void translate(int offsetI) {
  24. char c;
  25. while ((byte)(c = getNextChar()) != -1) {
  26. if (Character.isLowerCase(c)) {
  27. c = rotate(c, offsetI);
  28. }
  29. System.out.print(c);
  30. }
  31. }
  32.  
  33.  
  34. public char getNextChar() {
  35. char ch = ' ';
  36. try {
  37.  
  38. ch = (char)dataencyptS.read();
  39. }
  40. catch (IOException e) {
  41. System.out.println("Exception reading character");
  42. }
  43. return ch;
  44. }
  45. // rotate: translate using rotation, version with table lookup
  46. public char rotate(char c, int offsetI) { // c must be lowercase
  47. String s = "abcdefghijklmnopqrstuvwxyz";
  48.  
  49. int i = 0;
  50. while (i < 26) {
  51. // extra +26 below because key might be negative
  52. if (c == s.charAt(i))
  53. return s.charAt((i + offsetI + 26)%26);
  54. i++;
  55. }
  56. return c;
  57. }
  58.  
  59.  
  60.  
  61. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2004
Posts: 15
Reputation: xlogan777 is an unknown quantity at this point 
Solved Threads: 0
xlogan777 xlogan777 is offline Offline
Newbie Poster

Re: Need help with Caesar Cipher program

 
0
  #2
Feb 7th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 1
Reputation: natedoggy is an unknown quantity at this point 
Solved Threads: 0
natedoggy natedoggy is offline Offline
Newbie Poster

Re: Need help with Caesar Cipher program

 
0
  #3
Jan 28th, 2008
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;
}


}
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Need help with Caesar Cipher program

 
0
  #4
Jan 28th, 2008
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()
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help with Caesar Cipher program

 
0
  #5
Jan 28th, 2008
Originally Posted by Phaelax View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Need help with Caesar Cipher program

 
0
  #6
Jan 29th, 2008
well, StringTokenizer had been deprecated for years back in 2004
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: letsae is an unknown quantity at this point 
Solved Threads: 0
letsae letsae is offline Offline
Newbie Poster

Re: Need help with Caesar Cipher program

 
-1
  #7
Jan 20th, 2009
Hi there, could you please help me with a java code to break the Caesar cipher using brute force
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Need help with Caesar Cipher program

 
0
  #8
Jan 20th, 2009
Originally Posted by letsae View Post
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: letsae is an unknown quantity at this point 
Solved Threads: 0
letsae letsae is offline Offline
Newbie Poster

How about a java code for connecting a client and a server?

 
-1
  #9
Jan 21st, 2009
could you please help me with a java code for connecting a client and a server?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How about a java code for connecting a client and a server?

 
0
  #10
Jan 21st, 2009
Originally Posted by letsae View Post
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC