Ejemplo n.º 1
0
        public List <StateMatrix> Encrypt()
        {
            if (Text.Length % 16 != 0)
            {
                int padding = -(Text.Length % 16 - 16);
                Text = Text.PadRight(Text.Length + padding, (char)0);
            }

            List <StateMatrix> matrixBlocks = new List <StateMatrix>();

            for (int i = 0, j = 0; i < Text.Length / 16; i++, j += 16)
            {
                matrixBlocks.Add(new StateMatrix(Text.Substring(j, 16)));
            }

            List <StateMatrix> result = new List <StateMatrix>();

            foreach (StateMatrix matrix in matrixBlocks)
            {
                List <Round> rounds = new List <Round>();
                Round        round  = new Round();
                round.Matrixes.Add(matrix.RoundsToString("Original"));
                StateMatrix state = AddRoundKey(matrix, 0);
                round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                rounds.Add(round);
                TextRounds.Add(rounds);
                for (int i = 1; i <= 9; i++)
                {
                    rounds = new List <Round>();
                    round  = new Round();
                    state  = SubBytes(state);
                    round.Matrixes.Add(state.RoundsToString("SubBytes"));
                    state = ShiftRows(state);
                    round.Matrixes.Add(state.RoundsToString("ShiftRows"));
                    state = MixColumns(state);
                    round.Matrixes.Add(state.RoundsToString("MixColumns"));
                    state = AddRoundKey(state, i);
                    round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                    rounds.Add(round);
                    TextRounds.Add(rounds);
                }

                rounds = new List <Round>();
                round  = new Round();
                state  = SubBytes(state);
                round.Matrixes.Add(state.RoundsToString("SubBytes"));
                state = ShiftRows(state);
                round.Matrixes.Add(state.RoundsToString("ShiftRows"));
                state = AddRoundKey(state, 10);
                round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                rounds.Add(round);
                TextRounds.Add(rounds);
                result.Add(state);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public string Decrypt(List <StateMatrix> stateMatrix)
        {
            string result = "";

            foreach (StateMatrix matrix in stateMatrix)
            {
                List <Round> rounds = new List <Round>();
                Round        round  = new Round();
                round.Matrixes.Add(matrix.RoundsToString("Original"));
                StateMatrix state = AddRoundKey(matrix, 10);
                round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                rounds.Add(round);
                InvRounds.Add(rounds);
                for (int i = 1; i <= 9; i++)
                {
                    rounds = new List <Round>();
                    round  = new Round();
                    state  = InvShiftRows(state);
                    round.Matrixes.Add(state.RoundsToString("ShiftRows"));
                    state = InvSubBytes(state);
                    round.Matrixes.Add(state.RoundsToString("SubBytes"));
                    state = AddRoundKey(state, 10 - i);
                    round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                    state = InvMixColumns(state);
                    round.Matrixes.Add(state.RoundsToString("MixColumns"));
                    rounds.Add(round);
                    InvRounds.Add(rounds);
                }

                rounds = new List <Round>();
                round  = new Round();
                state  = InvShiftRows(state);
                round.Matrixes.Add(state.RoundsToString("ShiftRows"));
                state = InvSubBytes(state);
                round.Matrixes.Add(state.RoundsToString("SubBytes"));
                state = AddRoundKey(state, 0);
                round.Matrixes.Add(state.RoundsToString("AddRoundKey"));
                rounds.Add(round);
                InvRounds.Add(rounds);
                result += state.ToString();
            }
            return(result);
        }