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.