Results 1 to 5 of 5

Thread: Ceasar's Box

  1. #1
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default 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[0].equals("-encrypt")){
            System.out.println(encrypt(args[1]));
        }
        else if(args[0].equals("-decrypt")){
            System.out.println(decrypt(args[1]));
        }
    }
    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.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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.

  3. #3
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Ceasar's Box

    double post my bad
    Last edited by d a r i u s; 04-14-2013 at 01:14 PM.

  4. #4
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Ceasar's Box

    Java Code:
    package CaesarBox;
    
    public class yeh {
    
    public static void main(String[] args) {
        // CaesarsBox <-encrypt|-decrypt>
        if (args[0].equals("-encrypt")) {
            System.out.println(encrypt(args[1]));
        } else if (args[0].equals("-decrypt")) {
            System.out.println(decrypt(args[1]));
        }
    }
    
    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.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Ceasar's Box

    Please reread post #2. It basically tells you what to do.

    Regards.

Similar Threads

  1. Need help with Ceasar Cipher question
    By Gigi in forum New To Java
    Replies: 3
    Last Post: 01-21-2009, 03:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •