Beispiel #1
0
        private byte[] Expand(byte[] key)
        {
            int expandedKeyLength = LengthAfterExpansion(key.Length);

            _expandedKey = new List <byte>(key);

            int iteration = 1;

            while (_expandedKey.Count < expandedKeyLength)
            {
                byte[] lastFourBytes = _expandedKey.LastElements(4);
                lastFourBytes = lastFourBytes.ShiftToLeft();
                lastFourBytes = AesSBox.Transform(lastFourBytes);
                Rcon.Apply(lastFourBytes, iteration);
                IEnumerable <byte> fourBytes = _expandedKey.GetRange(_expandedKey.Count - key.Length, 4);
                lastFourBytes = lastFourBytes.XOR(fourBytes);
                _expandedKey.AddRange(lastFourBytes);

                for (int i = 0; i < 3; i++)
                {
                    lastFourBytes = _expandedKey.LastElements(4);
                    fourBytes     = _expandedKey.GetRange(_expandedKey.Count - key.Length, 4);
                    lastFourBytes = lastFourBytes.XOR(fourBytes);
                    _expandedKey.AddRange(lastFourBytes);
                }

                if (key.Length == 32 && _expandedKey.Count < 240)
                {
                    lastFourBytes = _expandedKey.LastElements(4);
                    lastFourBytes = AesSBox.Transform(lastFourBytes);
                    fourBytes     = _expandedKey.GetRange(_expandedKey.Count - key.Length, 4);
                    lastFourBytes = lastFourBytes.XOR(fourBytes);
                    _expandedKey.AddRange(lastFourBytes);
                }

                int repeat = 0;
                if (key.Length == 24 && _expandedKey.Count < 208)
                {
                    repeat = 2;
                }
                else if (key.Length == 32 && _expandedKey.Count < 240)
                {
                    repeat = 3;
                }

                for (int i = 0; i < repeat; i++)
                {
                    lastFourBytes = _expandedKey.LastElements(4);
                    fourBytes     = _expandedKey.GetRange(_expandedKey.Count - key.Length, 4);
                    lastFourBytes = lastFourBytes.XOR(fourBytes);
                    _expandedKey.AddRange(lastFourBytes);
                }

                iteration++;
            }

            return(_expandedKey.ToArray());
        }
Beispiel #2
0
 public static byte[][] SubBytes(byte[][] array)
 {
     return(AesSBox.Transform(array));
 }