예제 #1
0
        static void Main(string[] args)
        {
            ProgramMenu menu = new ProgramMenu();

            Tuple <string[], string[]> lastCipherText = null;

            while (true)
            {
                ProgramAction direction = menu.ChooseCipherAction();

                AesCipher cipherProgram = new AesCipher();
                Tuple <string[], string[]> valuesToUse = null;
                while (valuesToUse == null)
                {
                    switch (direction)
                    {
                    case ProgramAction.Cipher:
                    {
                        CipherInputAction menuStartingChoice = menu.ChooseCipherInputType();
                        valuesToUse = GetCipherProgramValuesToUse(menu, menuStartingChoice);
                        IEnumerable <RoundWords> roundKeys = GetRoundKeys(valuesToUse);
                        menu.PromptForContinue();

                        byte[]    inputBytes = valuesToUse.Item2.Select(x => CreateByteFromHexadecimal(x)).ToArray();
                        ByteArray result     = cipherProgram.Cipher(inputBytes, roundKeys);
                        lastCipherText = new Tuple <string[], string[]>(valuesToUse.Item1, result.Bytes1dArray.Select(x => CreateHexadecimalFromByte(x)).ToArray());
                        break;
                    }

                    case ProgramAction.Decipher:
                    {
                        DecipherInputAction menuStartingChoice = menu.ChooseDecipherInputType();
                        valuesToUse = GetDecipherProgramValuesToUse(menu, menuStartingChoice, lastCipherText);
                        IEnumerable <RoundWords> roundKeys = GetRoundKeys(valuesToUse);
                        menu.PromptForContinue();

                        byte[] inputBytes = valuesToUse.Item2.Select(x => CreateByteFromHexadecimal(x)).ToArray();
                        cipherProgram.Decipher(inputBytes, roundKeys);
                        break;
                    }

                    default:
                    {
                        throw new Exception(SurpriseExceptionMessage);
                    }
                    }
                }

                menu.PromptForContinue();
            }
        }
예제 #2
0
        private static Tuple <string[], string[]> GetDecipherProgramValuesToUse(ProgramMenu menu, DecipherInputAction menuStartingChoice, Tuple <string[], string[]> lastCipherText)
        {
            Tuple <string[], string[]> valuesToUse = null;

            switch (menuStartingChoice)
            {
            case DecipherInputAction.LastGeneratedCipherText:
            {
                if (lastCipherText != null)
                {
                    valuesToUse = lastCipherText;
                }
                else
                {
                    menu.NoLastCipherTextWarning();
                }

                break;
            }

            case DecipherInputAction.UserInput:
            {
                valuesToUse = menu.InputValues();
                break;
            }

            default:
            {
                throw new Exception(SurpriseExceptionMessage);
            }
            }

            return(valuesToUse);
        }