![]() |
| ||
| 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.*; 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. |
| ||
| 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. |
| ||
| 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; } } |
| ||
| Re: Need help with Caesar Cipher program Quote:
|
| ||
| Re: Need help with Caesar Cipher program Quote:
Also, the one that nate tacked on to this little piece of history should probably use StringBuilder instead of += for constructing the string. |
| ||
| Re: Need help with Caesar Cipher program well, StringTokenizer had been deprecated for years back in 2004 ;) |
| ||
| 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 |
| ||
| Re: Need help with Caesar Cipher program Quote:
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. |
| ||
| 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? |
| ||
| Re: How about a java code for connecting a client and a server? Quote:
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