2d (4x4) array insdie a 1d array. (Block cipher)
Hi guys
I am trying to create my block cipher as part of a uni project and what the block cipher consists of is putting in the characters in blocks of 4x4 arrays and to map it it has a look up. What i think i needto create is a 2d array inside each cell of a 1d array. Please see photo
http://i57.photobucket.com/albums/g2...ne2k/array.jpg
Thats what i am tryiring to create ^^ . Has anyone got any suggestions of how i can do this. I eventually need to be able to subsititue different cells from differnet 4x4 arrays
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Alternatively, you can create a large 2d array, and then a second array that contains reference numbers to the start of each "block".
For example:
Code java:
//Sentence
String sentence = "The quick brown fox jumps over the lazy dog";
//Number of Columns needed
int numColumns=sentence.length()/4;
//Finish last entire block of 4 columns
while(numColumns%4!=0)
numColumns++;
//2D Array for Sentence
String[][] array = new String[4][numColumns];
//1D Array for references
int[] references = new int[numColumns/4];
//Fill references
for(int i=0;i<references.length;i++)
references[i] = i*4;
I think that is right. Haven't compiled it.
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
I can get that code to work im taking it a step at a time.
What i have is i divde the number of chareacters by 16 to give a double number how ever i need to figure out how to make the double always round up to its whole number ( 12.4 > 13, 3.3 >4, 5.9>6). This will allow me to know how many 4x4 blocks to create then i can use the reference for loop to point to the stating coloum of each 4x4 block.. How can i round the double up ?
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Quote:
Originally Posted by
fortune2k
I can get that code to work im taking it a step at a time.
What i have is i divde the number of chareacters by 16 to give a double number how ever i need to figure out how to make the double always round up to its whole number ( 12.4 > 13, 3.3 >4, 5.9>6). This will allow me to know how many 4x4 blocks to create then i can use the reference for loop to point to the stating coloum of each 4x4 block.. How can i round the double up ?
What you need to do is find out what the next number divisible by 16 from the number of characters. This can be done with the if statement I had above, were you use mod ( % ) to determine if it doesnt divide evenly.
For example, if sentence.length()%16 == 0, the number is divisible by 16 and you just need to do sentence.length()/16. Otherwise, you want to do (sentence.length()/16)+1.
That is the most exact way I know of.
Other ways would be by doing double division and adding .9 to your decimal, but if you have a double like 5.01, then it will not round up.
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
im a little confussed on how id implement it
i want somethinbg like
WHILE text is NOT EVENLY DIVDED BY 16
{
plaintext +=d 'a'
}
if text is evenly divded by 16
{
array [4][blockammount*4]
}
so the while loop will keep adding the letter a to the text string until it divides evenly if it does divide eaisly time that number by 4 to give the number of coloums needed in the array
i have never used mod before so i am a little stuck
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Code java:
//Sentence
String sentence = "The quick brown fox jumps over the lazy dog";
//Declare variable for number of columns
int numColumns;
/* Mod the number of characters in the Sentence by 16.
* If the Modding returns 0, it means it is divisible
* and we do not need to round up. If the Modding returns
* anything other than 0, it means it is not divisible and
* we need to round up. To round up, we add 1 "block" (or 16)
* then divide by 16.
*/
if(sentence.length() % 16 == 0)
numColumns = sentence.length()/16;
else
numColumns = (sentence.length()+16)/16;
The above code will give you the number of columns, rounded up to the nearest block of 16.
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
In Java, a multidimensional array is nothing but an array of arrays. So, a char[] is an array where each element holds a char, char[][] is an array where each element is a char[] and char[][][] is an array of char[][].
For your requirement, you need to declare the array as
Code :
char[][][] myArray = new char[3][4][4];
Now each of the three elements of myArray is a two dimensional char array of size 4 X 4 -- a char[4][4]
db
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Quote:
Originally Posted by
Darryl.Burke
In Java, a multidimensional array is nothing but an array of arrays. So, a char[] is an array where each element holds a char, char[][] is an array where each element is a char[] and char[][][] is an array of char[][].
For your requirement, you need to declare the array as
Code :
char[][][] myArray = new char[3][4][4];
Now each of the three elements of myArray is a two dimensional char array of size 4 X 4 -- a char[4][4]
db
right at the moment i have
Code :
int w=0;
while(w !=hexarray.length)
{
for(int x=0; x< BlockArray.length; x++)
{
for(int y=0;y<4;y++)
{
for(int z=0;z<4;z++)
{
System.out.print( BlockArray[x][y][z] = hexarray[w]);
}
}
}
w++;
}
im also tying to fill;e each block coloum by coloum as shown in my pic..
am I on the right lines to achive this ?
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
im pretty sure that the 3d array isnt what im after ive never used 3d arrays before im thinking it like a cube am i right ?
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Quote:
Originally Posted by
fortune2k
right at the moment i have
Code :
int w=0;
while(w !=hexarray.length)
{
for(int x=0; x< BlockArray.length; x++)
{
for(int y=0;y<4;y++)
{
for(int z=0;z<4;z++)
{
System.out.print( BlockArray[x][y][z] = hexarray[w]);
}
}
}
w++;
}
im also tying to fill;e each block coloum by coloum as shown in my pic..
am I on the right lines to achive this ?
I have a suggestion which can make things easier if you ever do a multi-dimensional array on a larger scale. Purely for the reason of making things more readable and easier to debug, I have gotten into the habit of breaking down multi-dimensional arrays when accessing indexes. While it provides to actual advantage from the computer's point of view, it makes it a hell of a lot easier to deal with as a human if you have very complex loops.
Based on your code above, breaking down the array would look like this:
Code java:
int w=0;
while(w !=hexarray.length)
{
for(int x=0; x< BlockArray.length; x++)
{
char[][] tempArr1 = BlockArray[x];
for(int y=0;y<4;y++)
{
char[] tempArr2 = tempArr1[y];
for(int z=0;z<4;z++)
{
System.out.print(tempArr2[z] = hexarray[w]);
}
}
}
w++;
}
While it is unnecessary for a loop as basic as the one you have above, it can be extremely useful for very complex loops that use multi-dimensional arrays.
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
it needs to be one big array because i need to substitutae a cell in 1 4x4 block with another one in a different 4x4 array
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
I have just took a manual aproach to print out what comes in each block i was trying to put a print statement in the loop but this seems to work
code:
Code :
for(int x=0; x< BlockArray.length; x++)
{
for(int y=0;y<4;y++)
{
for(int z=0;z<4;z++)
{
BlockArray[x][z] [y] = hexarray[w++];
}
}
}
System.out.println("Length: "+hexarray.length+" Hash array: ");
for(int d=0;d<hexarray.length;d++)
{
System.out.print(hexarray[d]+" ");
}
System.out.println("\nBlock 1: \n");
System.out.printf("%4s %4s %4s %4s", BlockArray[0][0][0], BlockArray[0][0][1],BlockArray[0][0][2],BlockArray[0][0][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[0][1][0], BlockArray[0][1][1],BlockArray[0][1][2],BlockArray[0][1][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[0][2][0], BlockArray[0][2][1],BlockArray[0][2][2],BlockArray[0][2][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[0][3][0], BlockArray[0][3][1],BlockArray[0][3][2],BlockArray[0][3][3]+"\n");
System.out.println("Block 2: \n");
System.out.printf("%4s %4s %4s %4s", BlockArray[1][0][0], BlockArray[1][0][1],BlockArray[1][0][2],BlockArray[1][0][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[1][1][0], BlockArray[1][1][1],BlockArray[1][1][2],BlockArray[1][1][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[1][2][0], BlockArray[1][2][1],BlockArray[1][2][2],BlockArray[1][2][3]+"\n");
System.out.printf("%4s %4s %4s %4s", BlockArray[1][3][0], BlockArray[1][3][1],BlockArray[1][3][2],BlockArray[1][3][3]+"\n");
Output:
Code :
Length: 32 Hash array:
41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34
Block 1:
41 45 49 4d
42 46 4a 4e
43 47 4b 4f
44 48 4c 50
Block 2:
51 33 37 31
30 34 38 32
31 35 39 33
32 36 30 34
Lenghth of block array 2
string length: 32
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
Quote:
Originally Posted by
fortune2k
I have just took a manual aproach to print out what comes in each block i was trying to put a print statement in the loop but this seems to work
Check out the API for Arrays#deepToString()
Arrays#deepToString (Java Platform SE 6)
db
Re: 2d (4x4) array insdie a 1d array. (Block cipher)
brilliant i got it working thanks for your help