public void TestMixColumns()
        {
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0x33, 0x34, 0x39, 0x3a,
                                         0x31, 0x28, 0x38, 0x23,
                                         0x63, 0x64, 0x69, 0x6a,
                                         0x6f, 0x68, 0x75, 0x7e  };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);

            start = start.mixColumns();

            Console.Out.WriteLine("mix:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestShiftRows()
        {
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0x31, 0x36, 0x63, 0x68,
                                         0x35, 0x62, 0x67, 0x34,
                                         0x61, 0x66, 0x33, 0x39,
                                         0x65, 0x32, 0x38, 0x64  };
            State expectedState = new State(expectedStateData);
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);

            start = start.shiftRows();

            Console.Out.WriteLine("shift:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestSubBytes()
        {
            // test vector: according to Casper Schellekens
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0xc7, 0x23, 0xc3, 0x18,
                                     0x96, 0x05, 0x07, 0x12,
                                     0xef, 0xaa, 0xfb, 0x43,
                                     0x4d, 0x33, 0x85, 0x45 };
            State expectedState = new State(expectedStateData);
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);

            start = start.subBytes();

            Console.Out.WriteLine("subBytes:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestAddRoundKey0()
        {
            string s = "12345689abcdefgh";
            byte[] inputKey = { 0x11, 0x22, 0x33, 0x44,
                                0x55, 0x66, 0x77, 0x88,
                                0x99, 0x00, 0xaa, 0xbb,
                                0xcc, 0xdd, 0xee, 0xff };
            byte[] expectedStateData = { 32, 16, 0, 112, 96, 80, 79, 177, 248, 98, 201, 223, 169, 187, 137, 151 };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            Key key = new Key(inputKey);
            State start = new State(inputPlain);

            start = start.addRoundKey(key, 0);

            Console.Out.WriteLine("add0:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestMixColumnsInv()
        {
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0x1b, 0x0c, 0x11, 0x02,
                                         0x15, 0x04, 0x1c, 0x0f,
                                         0x4b, 0x5c, 0x41, 0x52,
                                         0x07, 0x30, 0x1d, 0x26 };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);

            start = start.mixColumnsInv();

            Console.Out.WriteLine("mix:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestSubBytesInv()
        {
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0x2e, 0xa1, 0x66, 0x28,
                                     0xd9, 0x24, 0x76, 0x5b,
                                     0xd8, 0xab, 0x00, 0x8c,
                                     0xbc, 0xd3, 0x0a, 0xf7 };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);

            start = start.subBytesInv();

            Console.Out.WriteLine("subBytes:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestShiftRowsInv()
        {
            string s = "12345689abcdefgh";
            byte[] expectedStateData = { 0x31, 0x66, 0x63, 0x39,
                                         0x35,0x32,0x67,0x64,
                                         0x61,0x36,0x33,0x68,
                                         0x65,0x62,0x38,0x34 };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            State start = new State(inputPlain);
            start = start.shiftRowsInv();
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
        public void TestAddRoundKey7()
        {
            string s = "12345689abcdefgh";
            byte[] inputKey = { 0x11, 0x22, 0x33, 0x44,
                                0x55, 0x66, 0x77, 0x88,
                                0x99, 0x00, 0xaa, 0xbb,
                                0xcc, 0xdd, 0xee, 0xff };
            byte[] expectedStateData = { 42, 45, 180, 25, 155, 188, 117, 243, 155, 228, 10, 60, 124, 122, 249, 208 };
            State expectedState = new State(expectedStateData);
            Encoding encoding = Encoding.UTF8;
            byte[] inputPlain = encoding.GetBytes(s);

            Key key = new Key(inputKey);
            State start = new State(inputPlain);
            start = start.addRoundKey(key, 7);
            Console.Out.WriteLine("add7:\n" + start);
            Assert.AreEqual(start.ToString(), expectedState.ToString());
        }
 public State addRoundKey(Key key, int round)
 {
     State s = new State();
     for (int c = 0; c < nrofCol; c++)
     {
         for (int r = 0; r < nrofRow; r++)
         {
             s.buf[r, c] = (byte)(buf[r, c] ^ key.w[r, c + 4 * round]);
         }
     }
     return (s);
 }
 public State mixColumnsInv()
 {
     State s = new State();
     for (int c = 0; c < nrofCol; c++)
     {
         s.buf[0, c] = (byte)(mul14[buf[0, c]] ^ mul11[buf[1, c]] ^ mul13[buf[2, c]] ^ mul09[buf[3, c]]);
         s.buf[1, c] = (byte)(mul09[buf[0, c]] ^ mul14[buf[1, c]] ^ mul11[buf[2, c]] ^ mul13[buf[3, c]]);
         s.buf[2, c] = (byte)(mul13[buf[0, c]] ^ mul09[buf[1, c]] ^ mul14[buf[2, c]] ^ mul11[buf[3, c]]);
         s.buf[3, c] = (byte)(mul11[buf[0, c]] ^ mul13[buf[1, c]] ^ mul09[buf[2, c]] ^ mul14[buf[3, c]]);
     }
     return (s);
 }
        public State shiftRowsInv()
        {
            State s = new State();

            for (int i = 0; i < nrofRow; i++)
            {
                for (int j = 0; j < nrofCol; j++)
                {
                    s.buf[i, (i + j) % 4] = buf[i, j];
                }
            }
            return (s);
        }
        public State subBytesInv()
        {
            State s = new State();

            for (int i = 0; i < nrofRow; i++)
            {
                for (int j = 0; j < nrofCol; j++)
                {
                    s.buf[i, j] = Sbox.inverseSbox[buf[i, j]];
                }
            }
            return (s);
        }
 public State mixColumns()
 {
     State s = new State();
     for (int c = 0; c < nrofCol; c++)
     {
         s.buf[0, c] = (byte)(mul2[buf[0, c]] ^ mul3[buf[1, c]] ^ buf[2, c] ^ buf[3, c]);
         s.buf[1, c] = (byte)(buf[0, c] ^ mul2[buf[1, c]] ^ mul3[buf[2, c]] ^ buf[3, c]);
         s.buf[2, c] = (byte)(buf[0, c] ^ buf[1, c] ^ mul2[buf[2, c]] ^ mul3[buf[3, c]]);
         s.buf[3, c] = (byte)(mul3[buf[0, c]] ^ buf[1, c] ^ buf[2, c] ^ mul2[buf[3, c]]);
     }
     return (s);
 }