示例#1
0
        /// <summary>
        /// Performs one of the possible actions: encrypts or decrypts data.
        /// </summary>
        /// <param name="text">Data to be encrypted / decrypted.</param>
        /// <param name="key">The key for the encryption / decryption algorithm.</param>
        /// <param name="typeOfChiper">The name of the encryption / decryption algorithm.</param>
        /// <param name="action">Action to be taken (encrypt / decrypt).</param>
        /// <returns>The result of encryption / decryption.</returns>
        private static string PerformAction(string text, string key,
                                            TypesOfCiphers typeOfChiper, Model.Ciphers.Action action)
        {
            switch (typeOfChiper)
            {
            case TypesOfCiphers.RailFence:
                RailFence railFence = new RailFence();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    return(railFence.Encrypt(text, key));
                }
                else
                {
                    return(railFence.Decrypt(text, key));
                }

            case TypesOfCiphers.RotatingSquare:
                RotatingGrill rotatingSquare = new RotatingGrill();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    return(rotatingSquare.Encrypt(text));
                }
                else
                {
                    return(rotatingSquare.Decrypt(text));
                }

            case TypesOfCiphers.Vigenere:
                Vigenere vigener = new Vigenere();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    try
                    {
                        return(vigener.Encrypt(text, key));
                    }
                    catch (DivideByZeroException)
                    {
                        return(text);
                    }
                }
                else
                {
                    try
                    {
                        return(vigener.Decrypt(text, key));
                    }
                    catch (DivideByZeroException)
                    {
                        return(text);
                    }
                }

            default:
                return(null);
            }
        }
 /// <summary>
 /// Deletes all characters except the English alphabet.
 /// </summary>
 /// <param name="text">The text to be modified according to the pattern.</param>
 /// <returns>Modified text to match the pattern.</returns>
 public static string ModifyText(string text, TypesOfCiphers typeOfCipher)
 {
     if (typeOfCipher == TypesOfCiphers.RailFence ||
         typeOfCipher == TypesOfCiphers.RotatingSquare)
     {
         text = Regex.Replace(text, EngTextPattern, string.Empty);
     }
     else
     {
         text = Regex.Replace(text, RusTextPattern, string.Empty);
     }
     text = text.ToUpper();
     return(text);
 }
示例#3
0
 /// <summary>
 /// Deletes the corresponding characters according to the selected pattern.
 /// </summary>
 /// <param name="key">The key to change according to the pattern.</param>
 /// <returns>Modified key according to pattern.</returns>
 public static string ModifyKey(string key, TypesOfCiphers typeOfCipher)
 {
     if (typeOfCipher == TypesOfCiphers.RailFence ||
         typeOfCipher == TypesOfCiphers.RotatingSquare)
     {
         key = Regex.Replace(key, KeyPattern, string.Empty);
     }
     else
     {
         key = Regex.Replace(key, VigenerKeyPattern, string.Empty);
         key = key.ToUpper();
     }
     return(key);
 }
示例#4
0
        private void BtnDecrypt_Click(object sender, EventArgs e)
        {
            TypesOfCiphers typesOfCipher = TypesOfCiphers.RailFence;

            if (rbRailFence.Checked)
            {
                typesOfCipher = TypesOfCiphers.RailFence;
            }
            else if (rbRotatingSquare.Checked)
            {
                typesOfCipher = TypesOfCiphers.RotatingSquare;
            }
            else if (rbVigenerCipher.Checked)
            {
                typesOfCipher = TypesOfCiphers.Vigenere;
            }

            tbKey.Text      = KeyValidation.ModifyKey(tbKey.Text, typesOfCipher).ToString();
            rtbSrcText.Text = TextValidation.ModifyText(rtbSrcText.Text, typesOfCipher);

            if (cbUseDataInRcb.Checked)
            {
                rtbResText.Text = MainController.Decrypt(rtbSrcText.Text, tbKey.Text,
                                                         typesOfCipher);
            }
            else
            {
                try
                {
                    MainController.Decrypt(tbPathToSrcFile.Text, tbPathToResFile.Text,
                                           tbKey.Text, typesOfCipher);
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Incorrect file path entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (IOException)
                {
                    MessageBox.Show("Incorrect file path entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Encrypts data from a file using the selected algorithm and writes the result to another file.
        /// </summary>
        /// <param name="pathToSrcFile">The path to the file with the text to be encrypted.</param>
        /// <param name="pathToDestFile">The path to the file where you want to save the encryption result.</param>
        /// <param name="key">The key for the encryption algorithm.</param>
        /// <param name="typeOfChiper">The name of the encryption algorithm.</param>
        public static void Encrypt(string pathToSrcFile, string pathToDestFile,
                                   string key, TypesOfCiphers typeOfChiper)
        {
            string plaintext;

            try
            {
                plaintext = File.ReadAllText(pathToSrcFile);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException();
            }
            catch (IOException)
            {
                throw new IOException();
            }

            plaintext = TextValidation.ModifyText(plaintext, typeOfChiper);

            string ciphertext = PerformAction(plaintext, key, typeOfChiper, Model.Ciphers.Action.Encrypt);

            File.WriteAllText(pathToDestFile, ciphertext);
        }
示例#6
0
 /// <summary>
 /// Decrypts the text with the selected algorithm.
 /// </summary>
 /// <param name="ciphertext">The text to decrypt.</param>
 /// <param name="key">The key for the decryption algorithm.</param>
 /// <param name="typeOfChiper">The name of the decryption algorithm.</param>
 /// <returns>Decrypted text.</returns>
 public static string Decrypt(string ciphertext, string key, TypesOfCiphers typeOfChiper)
 {
     return(PerformAction(ciphertext, key, typeOfChiper, Model.Ciphers.Action.Decrypt));
 }