集册 Java实例教程 获取大小为16的线性数组并将其转换为4x4状态数组

获取大小为16的线性数组并将其转换为4x4状态数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

365
获取大小为16的线性数组并将其转换为4x4状态数组
/** 
来 自 
NowJava.com
**/


//package com.nowjava;

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

                .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

     *//*nowjava.com - 时代Java 提供*/

    public static byte[][] arrayTo4xArray(byte[] array)

            throws DataFormatException {

        if (array.length != 16)

            throw new DataFormatException("Array must be of length 16");


        byte[][] a
展开阅读全文