예제 #1
0
        public ISimpleCipheringAlgorithm NewAlgorithm(SimpleCiphers Cipher)
        {
            switch (Cipher)
            {
            case SimpleCiphers.Columns:
                return(new ColumnarTransposition());

            case SimpleCiphers.RailFence:
                return(new RailFence());

            case SimpleCiphers.Vigenere:
                return(new Vigenere());

            case SimpleCiphers.Grille:
                return(new TurningGrille());

            default:
                return(new Vigenere());
            }
        }
예제 #2
0
        private void Work()
        {
            IsNoActionRunning = false;

            try
            {
                #region Checks

                //Check whether file id is int

                if (!int.TryParse(ChosenFileId, out int FileId))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                //Check whether file id is correct

                if ((FileId >= FilesLoaded.Count()) || (FileId < 0))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                if (string.IsNullOrEmpty(ChosenAction))
                {
                    return;
                }

                string FilePath = string.Empty;

                //Load file path

                foreach (var item in FilesLoaded)
                {
                    if (item.Id == FileId)
                    {
                        FilePath = item.FileRealName;
                        break;
                    }
                }

                //if file id points to empty record

                if (string.IsNullOrWhiteSpace(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File id is wrong.";
                    return;
                }

                // if file was deleted after loading

                if (!File.Exists(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File doesnt exist.";
                    return;
                }

                #endregion

                var AlphabetInCiphers = string.Empty;

                //Get chosen Alphabet

                if (ChosenAlphabet == AlphabetNamesList[0])
                {
                    AlphabetInCiphers = ResourceStrings.EngCapital;
                }
                else
                if (ChosenAlphabet == AlphabetNamesList[1])
                {
                    AlphabetInCiphers = ResourceStrings.RuCapital;
                }

                var ChosenCipherType = new SimpleCiphers();

                //Find Chosen Algorithm

                for (int i = 0; i < CiphersList.Count(); ++i)
                {
                    if (CiphersList[i] == ChosenCipher)
                    {
                        ChosenCipherType = (SimpleCiphers)i;
                    }
                }

                var Algorithm = new SimpleCipheringAlgorithmFactory().NewAlgorithm(ChosenCipherType);

                Algorithm.Initialize(KeyValue, AlphabetInCiphers);

                var chosenActionType = new Operations();

                for (int i = 0; i < ActionsList.Count(); ++i)
                {
                    if (ActionsList[i] == ChosenAction)
                    {
                        chosenActionType = (Operations)i;
                    }
                }

                var TextToWorkWith = File.ReadAllText(FilePath).ToUpper();

                TextToWorkWith = GetFixedString(ref TextToWorkWith, AlphabetInCiphers, chosenActionType);

                //Encrypt
                if (chosenActionType == Operations.Encrypt)
                {
                    ReturnedText = Algorithm.Encrypt(TextToWorkWith);
                    File.WriteAllText(FilePath.Insert(FilePath.IndexOf("."), "Encrypted"), ReturnedText);
                    StateText = "Successfully created text file " + Path.GetFileName(FilePath);
                }
                else//Decipher
                {
                    ReturnedText = Algorithm.Decipher(TextToWorkWith);
                    File.WriteAllText(FilePath.Insert(FilePath.IndexOf("."), "Decrypted"), ReturnedText);
                    StateText = "Successfully created text file " + Path.GetFileName(FilePath);
                }

                IsCompleted = true;
            }
            catch (ArgumentException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }

            finally
            { IsNoActionRunning = true; }
        }