Java examples for Collection Framework:Array Convert
Takes a linear array of size 16 and converts it into a 4x4 state array
//package com.java2s; import java.util.zip.DataFormatException; public class Main { public static void main(String[] argv) throws Exception { byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out/*from w w w .j a v a2 s .c o m*/ .println(java.util.Arrays.toString(arrayTo4xArray(array))); } /** * Takes a linear array of size 16 and converts it into a 4x4 state array * As defined in FIPS197 * * Idea of linearising/squaring arrays taken from watne.seis720.project.AES_Utilities * Watne uses array indexes and some unpleasant looking maths, so I've rewritten it * to use arraycopy instead, which is much more efficient. * * @param array - Linear byte array of length 16 * @return array - 4x4 2D byte array * @throws DataFormatException */ public static byte[][] arrayTo4xArray(byte[] array) throws DataFormatException { if (array.length != 16) throw new DataFormatException("Array must be of length 16"); byte[][] array4x = new byte[4][4]; for (int i = 0; i < 4; i++) { System.arraycopy(array, (i * 4), array4x[i], 0, 4); } return array4x; } }