Exemplo n.º 1
0
        /// <summary>
        /// The method decodes ciphertext with a cipher "Rail-Fence" with the specified key.
        /// </summary>
        /// <param name="ciphertext">Text to decrypt.</param>
        /// <param name="key">Fence height.</param>
        /// <returns>Decrypted text.</returns>
        public string Decrypt(string ciphertext, string key)
        {
            int numericKey;

            int.TryParse(KeyValidation.ModifyKey(key, TypesOfCiphers.RailFence), out numericKey);

            if (numericKey == 0)
            {
                numericKey = 1;
            }

            int currentPos = 0;

            char[] plaintext = new char[ciphertext.Length];

            for (int rowNumber = 0; rowNumber < numericKey; rowNumber++)
            {
                for (int i = rowNumber, currentCharInStr = 0; i < ciphertext.Length;
                     i += GetIndexNextChar(currentCharInStr, rowNumber, numericKey), currentCharInStr++)
                {
                    plaintext[i] = ciphertext[currentPos++];
                }
            }
            return(new string(plaintext));
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The method encodes plaintext with a cipher "Rail-Fence" with the specified key.
        /// </summary>
        /// <param name="plaintext">Text to be encrypted.</param>
        /// <param name="key">Fence height.</param>
        /// <returns>The ciphertext.</returns>
        public string Encrypt(string plaintext, string key)
        {
            int numericKey;

            int.TryParse(KeyValidation.ModifyKey(key, TypesOfCiphers.RailFence), out numericKey);

            if (numericKey == 0)
            {
                numericKey = 1;
            }

            string ciphertext = null;

            for (int rowNumber = 0; rowNumber < numericKey; rowNumber++)
            {
                for (int i = rowNumber, currentCharInStr = 0; i < plaintext.Length;
                     i += GetIndexNextChar(currentCharInStr, rowNumber, numericKey), currentCharInStr++)
                {
                    ciphertext += plaintext[i];
                }
            }

            return(ciphertext);
        }