Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting single dimension array to a 2D array in C# for AES data matrix

I'm trying to make a 2D array from a single dimension array to make a data state in Rijndael or AES cryptographical process. I've been trying using this code here;

public byte[,] MakeAState(byte[] block){
byte[,] State;

foreach (byte i in block)
    for (int row = 0; row < 4; row++)
        for (int column = 0; column < 4; column++)
            State[column, row] = i;

return State;
}

and I intend to make the result to be like this

//Original Sequence
[99 111 98 97 112 97 115 115 99 111 98 97 112 97 115 115]

//Desired Sequence
[99 112 99 112]
[111 97 111 97]
[98 115 98 115]
[97 115 97 115]

The results always comes out as if the elements of Block used as if the index of the State array, causing an 'out-of boundary' error message appearing. any idea on how to manage this?

like image 479
undip_student Avatar asked Jul 11 '26 14:07

undip_student


2 Answers

This should be what you want, and it's working with division and modulo to determine column and row(just switch "i % 4" with "i / 4" if you want to turn the matrix):

class Program
{
    static void Main(string[] args)
    {
        byte[] original = new byte[] { 99, 111, 98, 97, 112, 97, 115, 115, 99, 111, 98, 97, 112, 97, 115, 115 };
        byte[,] result = MakeAState(original);

        for (int row = 0; row < 4; row++)
        {
            for (int column = 0; column < 4; column++)
            {
                Console.Write(result[row,column] + "   ");
            }
            Console.WriteLine();
        }


    }

    public static byte[,] MakeAState(byte[] block)
    {
        if (block.Length < 16)
        {
            return null;
        }

        byte[,] state = new byte[4,4];

        for (int i = 0; i < 16; i++)
        {
            state[i % 4, i / 4] = block[i];
        }

        return state;
    }
}

}

Output:

99 112 99 112

111 97 111 97

98 115 98 115

97 115 97 115

like image 148
Florian Schmidinger Avatar answered Jul 14 '26 05:07

Florian Schmidinger


You likely flipped row and column on State[column, row] = i; which might be what's causing your out of bounds exception. Can't tell without more information about your variables, though.

But that's not the only issue here. Assuming you just want the array to be split into groups of four. This is your current situation if you flip row/columnand get past your exception.

//Original sequence: 
[0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15]

//Desired sequence:
[0  4  8  12]
[1  5  9  13]
[3  6  10 14]
[4  7  11 15]

//What you are currently getting:
[15 15 15 15]
[15 15 15 15]
[15 15 15 15] //<-- Last value of original sequence, everywhere.

What's happening in your code is every position in Block is placed in every position in the new array, which means that you'll end up with an array filled with the last value of Block when the algorithm is finished.

Changing it to something like this would return the result you want.

public static byte[,] State(byte[] Block)
{
    if (Block.Length % 16 != 0)
        throw new Exception("Byte array length must be divisible by 16.");

    var rowCount = Block.Length / 4;
    var State = new byte[rowCount, 4];

    for (int column = 0, block = 0; column < 4; column++)
        for (int row = 0; row < rowCount; row++, block++)
            State[row, column] = Block[block];

    return State;
}
like image 36
Hjalmar Z Avatar answered Jul 14 '26 05:07

Hjalmar Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!