예제 #1
0
        private static byte[][] GenerateRoundKeys(byte[] baseKey, int roundsCount, int Nb, int Nk)
        {
            int wordsCount = (roundsCount + 1) * Nb;

            byte[][] words = new byte[wordsCount][];
            for (int i = 0; i < Nk; i++)
            {
                words[i] = new byte[4];
                Array.Copy(baseKey, 4 * i, words[i], 0, 4);
            }

            for (int i = Nk; i < wordsCount; i++)
            {
                words[i] = new byte[4];

                byte[] temp = new byte[4];
                Array.Copy(words[i - 1], temp, 4);

                if (i % Nk == 0)
                {
                    RotWord(temp);
                    SubBytes(temp);
                    temp[0] ^= RC(i / Nk - 1);
                }
                else if (Nk == 8 && i % Nk == 4)
                {
                    SubBytes(temp);
                }

                Array.Copy(words[i - Nk], words[i], 4);
                for (int j = 0; j < 4; j++)
                {
                    words[i][j] ^= temp[j];
                }
            }

            byte[][] roundKeys = ArrayEx.Transform(words, roundsCount + 1);
            return(roundKeys);
        }