| | |
Need help with Caesar Cipher program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2003
Posts: 103
Reputation:
Solved Threads: 3
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.
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.
Java Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jan 2004
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 1
Reputation:
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;
}
}
/*
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;
}
}
•
•
•
•
Hi there, could you please help me with a java code to break the Caesar cipher using brute force
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 !!!
![]() |
Similar Threads
- Help needed with Caesar Cipher (C++)
- New to java, Caesar Cipher question. (Java)
- Need help with caesar cipher (Python)
Other Threads in the Java Forum
- Previous Thread: Writing to a file
- Next Thread: queue question
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows







