-Encrypt a string of text using a Caesar Square: the string should be written along the rows of a square that is as small as possible, and then the encrypted string is read down the columns like so:
"A long time ago, in a galaxy far, far away" is first processed to remove case and space, to get alongtimeago,inagalaxyfar,faraway (33 characters long) and is then laid out on a square grid:
a l o n g t
i m e a g o
, i n a g a
l a x y f a
r , f a r a
w a y
The grid is 6 x 6 as 36 is the closest square number that is no less than the number of characters in the message.
Reading form top-to-bottom we get ai,lrwlmia,aoenxfynaayagggfrtoaaa - which is of course garbage
You also have to decrypt such strings which may contain white space that needs to be removed ...
I am struggling to understand how the shifting of elements works. I know decrypting is reading from left to right and encrypting is reading from top to bottom but I can't think of a way to code it. Also I considered that the question has to do with two dimensional arrays but then I didn't know how to determine the amount of rows and columns I needed and hence didn't know what to make each dimension.
So far I have just changed the cypherText(string) into an array called decryptArray, and each element of this array contains a letter within the string. I was up to shifting the elements and I thought of using a FOR loop to do that but there was not much point until I understand how the shifting works. To be honest, I am not even sure if I am on the right track.
public class CaesarBox { public static void main(String[] args) { if(args[0].equals("-decrypt")) { System.out.println(decrypt(args[1])); } } public static char decrypt (String cypherText) { char example = 'a'+'b'; //ignore this int length = cypherText.length(); //determining the length of the string char [] decryptArray = new char [length]; //created an array using the length of what was entered for(int i=0; i < length; i++) { decryptArray[i]=cypherText.charAt(i); //making each element of what as typed in to equal an element in the array System.out.print(decryptArray[i]); } return example; //just put this in as I am not up to this part and so I can compile the program } }