/// <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));
        }
示例#2
0
 void KeyDownHandler(object sender, KeyRoutedEventArgs e)
 {
     if (KeyValidation.IsModifier(e.Key))
     {
         return;
     }
     e.Handled = !KeyValidation.IsNumeric(e.Key) && !(AllowDecimalPlaces && ((int)e.Key == 190 || e.Key == VirtualKey.Decimal));
 }
示例#3
0
        public override void GetProperties(ObjectPropertyList list)
        {
            base.GetProperties(list);

            list.Add(KeyValidation != null && KeyValidation.Any(x => x.Key == typeof(FlyWheel) && x.Active) ? 1154448 : 1154432);
            list.Add(KeyValidation != null && KeyValidation.Any(x => x.Key == typeof(WireSpool) && x.Active) ? 1154449 : 1154434);
            list.Add(KeyValidation != null && KeyValidation.Any(x => x.Key == typeof(PowerCore) && x.Active) ? 1154450 : 1154435);
            list.Add(KeyValidation != null && KeyValidation.Any(x => x.Key == typeof(BearingAssembly) && x.Active) ? 1154451 : 1154436);
        }
示例#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
        public KeyValidation CheckKey(string key)
        {
            bool          ctr1   = false;
            bool          ctr2   = false;
            bool          found1 = false;
            bool          found2 = false;
            KeyValidation type   = 0;

            new Thread(() =>
            {
                if (_oneTerm.Contains(key))
                {
                    type   = KeyValidation.ValidOneTerm;
                    found1 = true;
                }
                ctr1 = true;
            }).Start();
            new Thread(() =>
            {
                if (_fullYear.Contains(key))
                {
                    type   = KeyValidation.ValidFullYear;
                    found2 = true;
                }
                ctr2 = true;
            }).Start();
            while (!ctr1 || !ctr2)
            {
            }
            if (found1 && found2)
            {
                return(KeyValidation.Invalid);
            }
            if (found1 || found2)
            {
                return(type);
            }
            return(KeyValidation.Invalid);
        }
        /// <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);
        }