예제 #1
0
        public static string DecryptMessage(byte[] inputPlain, Key k, int nrOfIterations)
        {
            State outputState = new State(inputPlain);

            outputState = outputState.addRoundKey(k, nrOfIterations);

            for (int i = nrOfIterations - 1; i > 0; i--)
            {
                outputState = outputState.shiftRowsInv();
                outputState = outputState.subBytesInv();
                outputState = outputState.addRoundKey(k, i);
                outputState = outputState.mixColumnsInv();
            }
            outputState = outputState.shiftRowsInv();
            outputState = outputState.subBytesInv();
            outputState = outputState.addRoundKey(k, 0);
            return(outputState.ToString());
        }
예제 #2
0
        public static string EncryptMessage(byte[] inputPlain, Key k, int nrOfIterations)
        {
            State inputState = new State(inputPlain);

            inputState = inputState.addRoundKey(k, 0);
            for (int i = 1; i < nrOfIterations; i++)
            {
                inputState = inputState.subBytes();
                inputState = inputState.shiftRows();
                inputState = inputState.mixColumns();
                inputState = inputState.addRoundKey(k, i);
            }
            inputState = inputState.subBytes();
            inputState = inputState.shiftRows();
            inputState = inputState.addRoundKey(k, nrOfIterations);
            Console.Out.WriteLine(inputState.ToMatrixString());
            return(inputState.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 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());
        }