简体   繁体   English

将单维数组转换为C#中的2D数组,用于AES数据矩阵

[英]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. 我正在尝试从单维数组生成2D数组,以在Rijndael或AES加密过程中生成数据状态。 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. 结果总是出现,好像Block的元素一样,就像State数组的索引一样,导致出现“out-of boundary”错误消息。 any idea on how to manage this? 关于如何管理这个的任何想法?

You likely flipped row and column on State[column, row] = i; 你可能会在State[column, row] = i;上翻转rowcolumn 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 / column and get past your exception. 如果您翻转row / column并超出异常,这是您当前的情况。

//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. 你的代码中发生的事情是Block中的每个位置都放在新数组的每个位置,这意味着当算法结束时,你最终会得到一个填充了Block的最后一个值的数组。

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;
}

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): 这应该是你想要的,并且它使用除法和模来确定列和行(如果你想转动矩阵,只需用“i / 4”切换“i%4”):

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 99 112 99 112

111 97 111 97 111 97 111 97

98 115 98 115 98 115 98 115

97 115 97 115 97 115 97 115

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM