DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Need help with Caesar Cipher program (http://www.daniweb.com/forums/thread1135.html)

rasputinj Sep 19th, 2003 4:28 am
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.

xlogan777 Feb 7th, 2004 11:35 pm
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.

natedoggy Jan 28th, 2008 2:12 pm
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;
}


}

Phaelax Jan 28th, 2008 3:05 pm
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()

Ezzaral Jan 28th, 2008 3:43 pm
Re: Need help with Caesar Cipher program
 
Quote:

Originally Posted by Phaelax (Post 519960)
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.

jwenting Jan 29th, 2008 1:39 am
Re: Need help with Caesar Cipher program
 
well, StringTokenizer had been deprecated for years back in 2004 ;)

letsae Jan 20th, 2009 3:40 am
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

verruckt24 Jan 20th, 2009 6:09 am
Re: Need help with Caesar Cipher program
 
Quote:

Originally Posted by letsae (Post 782789)
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.

letsae Jan 21st, 2009 7:05 am
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?

javaAddict Jan 21st, 2009 7:28 am
Re: How about a java code for connecting a client and a server?
 
Quote:

Originally Posted by letsae (Post 783841)
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


All times are GMT -4. The time now is 11:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC