943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 47229
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 19th, 2003
0

Need help with Caesar Cipher program

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 15
Solved Threads: 3
Junior Poster
rasputinj is offline Offline
103 posts
since Sep 2003
Feb 7th, 2004
0

Re: Need help with Caesar Cipher program

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xlogan777 is offline Offline
15 posts
since Jan 2004
Jan 28th, 2008
0

Re: Need help with Caesar Cipher program

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


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
natedoggy is offline Offline
1 posts
since Jan 2008
Jan 28th, 2008
0

Re: Need help with Caesar Cipher program

Quote ...
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()
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 28th, 2008
0

Re: Need help with Caesar Cipher program

Click to Expand / Collapse  Quote originally posted by Phaelax ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,758 posts
since May 2007
Jan 29th, 2008
0

Re: Need help with Caesar Cipher program

well, StringTokenizer had been deprecated for years back in 2004
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 20th, 2009
-1

Re: Need help with Caesar Cipher program

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

Re: Need help with Caesar Cipher program

Click to Expand / Collapse  Quote originally posted by letsae ...
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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 21st, 2009
-1

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

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

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

Click to Expand / Collapse  Quote originally posted by letsae ...
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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to make a MECHANICAL TURTLE( * ) IN JAVA
Next Thread in Java Forum Timeline: JAVA Reference Casting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC