Exemplo n.º 1
0
 public byte[] AESTest(byte[] Block, byte[] Password)  //Тестирование для контрольных значений
 {
     In    = Block;
     key   = Password;
     State = new GF[4, Nb];
     for (int r = 0; r < 4; r++)
     {
         for (int c = 0; c < Nb; c++)
         {
             State[r, c] = new GF();
             State[r, c] = In[r + (4 * c)];
         }
     }
     KeyExpansion();
     return(Cipher(Block));
 }
Exemplo n.º 2
0
 public byte[] Cipher(byte[] Block)
 {
     In    = Block;
     State = new GF[4, Nb];
     for (int r = 0; r < 4; r++)
     {
         for (int c = 0; c < Nb; c++)
         {
             State[r, c] = new GF();
             State[r, c] = In[r + (4 * c)];
         }
     }
     //
     //Print(State);
     AddRoundKey(0);
     //Print(State);
     for (int round = 1; round < Nr; round++)
     {
         SubBytes();
         //Print(State);
         ShiftRows();
         //Print(State);
         MixColumns();
         //Print(State);
         AddRoundKey(round);
         //Print(State);
     }
     //Print(State);
     SubBytes();
     //Print(State);
     ShiftRows();
     //Print(State);
     AddRoundKey(Nr);
     //Print(State);
     Out = new byte[In.Length];
     for (int r = 0; r < 4; r++)
     {
         for (int c = 0; c < Nb; c++)
         {
             Out[r + (4 * c)] = State[r, c].Value;
         }
     }
     return(Out);
 }
Exemplo n.º 3
0
        private void KeyExpansion()
        {
            Rcon = new GF(0x01);
            w    = new byte[Nb * (Nr + 1), 4];
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    w[i, j] = key[4 * i + j];
                }
            }
            byte[] temp = new byte[4];

            for (int i = Nk; i < Nb * (Nr + 1); i++)
            {
                for (int k = 0; k < temp.Length; k++)
                {
                    temp[k] = w[i - 1, k];
                }
                if (i % Nk == 0)
                {
                    temp    = SubWord(RotWord(temp));
                    temp[0] = (byte)(temp[0] ^ Rcon.Value);
                    Rcon    = Rcon * 0x02;
                }
                else if (Nk > 6 && i % Nk == 4)
                {
                    temp = SubWord(temp);
                }
                for (int j = 0; j < 4; j++)
                {
                    w[i, j] = (byte)(w[i - Nk, j] ^ temp[j]);
                }
                //Print(w);
                //Console.WriteLine("______");
                //Print(Rcon.Value);
            }
        }