1. Member Join Date
Apr 2013
Posts
6
Rep Power
0

## Ceasar's Box

Hello everyone I am very new to java and have only just picked it up course but i have feel behind due to illness and I am slightly daunted by where the rest of my year is at. I know i am new to these forums but i promise to stay an active member and will contribute if I can .

The task that has been set for me is to construct a Ceasar's box in java. here is the skeleton code that has been provided:

Java Code:
```public class CaesarBox {
public static void main(String[] args) {
// CaesarsBox <-encrypt|-decrypt>
if(args.equals("-encrypt")){
System.out.println(encrypt(args));
}
else if(args.equals("-decrypt")){
System.out.println(decrypt(args));
}
}
public static String encrypt(String plainText) {
// TODO put encryption code below this line
// Put encryption code above this line
}
public static String decrypt(String cypherText) {
// TODO put decryption code below this line
// Put decryption code above this line
}
}```
I don't have much knowledge of encryption or decryption which has made the task almost impossible for me in the time I have had to catch up. Any kind of help would be greatly appreciated.  Reply With Quote

2. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10

## Re: Ceasar's Box

Hi d a r i u s, welcome to the forums.

Ceasar's box seems like a pretty straight forward algorithm. All you really need is a 2D array of size equal to the squared root of the input. Take each letter within the input and populate the array with each letter starting at the top left and working through, from top to bottom, left to right.
Read the array from left to right, top to bottom would give you the encrypted string. Decrypting is just the reverse.

To test make a simple string with a determined output. Put the input into the encrypt method and check the output.

Regards.  Reply With Quote

3. Member Join Date
Apr 2013
Posts
6
Rep Power
0

## Re: Ceasar's Box

Last edited by d a r i u s; 04-14-2013 at 01:14 PM.  Reply With Quote

4. Member Join Date
Apr 2013
Posts
6
Rep Power
0

## Re: Ceasar's Box

Java Code:
```package CaesarBox;

public class yeh {

public static void main(String[] args) {
// CaesarsBox <-encrypt|-decrypt>
if (args.equals("-encrypt")) {
System.out.println(encrypt(args));
} else if (args.equals("-decrypt")) {
System.out.println(decrypt(args));
}
}

public static String encrypt(String plaintext) {
// TODO put encryption code below this line
plaintext = plaintext.replaceAll("\\s+", "");// removes white space
plaintext = plaintext.toLowerCase();// converts capitol letters to lower
// case

char[] charArray = plaintext.toCharArray();
// takes individual characters from the arguments and puts them into an
// array

int x = charArray.length; // assigns the length of charArray to x
int y = 0;
while (y < x) {
++y;
if ((y == Math.floor(y)) && y * y >= x) {
// tests if y is an integer
// increases y until it is an integer
break;
}// above code finds the the length of the sides of the box
}
char[][] box = new char[y][y];// creates a 2d array
int pos = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
if (pos < plaintext.length()) {
box[i][j] = plaintext.charAt(pos);
pos++;
// fills the array with the characters from the text to be
// encrypted
}
}
}
String encrypted = "";
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box.length; j++) {
if (box[j][i] != 0) {// tells the program to ignore null values
encrypted += box[j][i];
}
// prints out the letters in the box by column
}

}
return encrypted;

// Put encryption code above this line

}

public static String decrypt(String cyphertext) {
// TODO put decryption code below this line
cyphertext = cyphertext.replaceAll("\\s+", "");// removes white space
cyphertext = cyphertext.toLowerCase();// converts capitol letters to
// lower
// case

char[] charArray = cyphertext.toCharArray();
// takes individual characters from the arguments and puts them into an
// array

int x = charArray.length; // assigns the length of charArray to x
int y = 0;
while (y < x) {
++y;
if ((y == Math.floor(y)) && y * y >= x) {
// tests if y is an integer
// increases y until it is an integer
break;
}// above code finds the the length of the sides of the box
}
char[][] box = new char[y][y];// creates a 2d array
int pos = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
if (pos < cyphertext.length()) {
box[i][j] = cyphertext.charAt(pos);
pos++;
}
}
// fills the array with the characters from the text to be
// encrypted
}
String decrypted = "";
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
if (box[j][i] != 0) {// tells the program to ignore null values
decrypted += box[j][i];
// prints out the letters in the box by column
}

}
}

return decrypted;
// Put decryption code above this line
}

}```
Last edited by d a r i u s; 04-14-2013 at 01:15 PM.  Reply With Quote

5. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10

## Re: Ceasar's Box  Reply With Quote