コード例 #1
0
        private void EncryptButton_Click(object sender, RoutedEventArgs e)
        {
            string plainText  = InputBox.Text;
            string rawKey     = KeyBox.Text;
            string cipherText = "";

            switch ((Cryptoanalyzer.EncryptionMethods)EncryptionMethodBox.SelectedIndex)
            {
            case Cryptoanalyzer.EncryptionMethods.Shift:
                cipherText = Cryptoanalyzer.EncryptShiftCipher(plainText, int.Parse(rawKey));
                break;

            case Cryptoanalyzer.EncryptionMethods.OneTimePad:
                cipherText = Cryptoanalyzer.EncryptOneTimePad(plainText, rawKey);
                break;

            case Cryptoanalyzer.EncryptionMethods.Affine:
                string[] keys = rawKey.Split(',', ';', ':', ' ');
                cipherText = Cryptoanalyzer.EncryptAffineCipher(plainText,
                                                                int.Parse(keys[0]), int.Parse(keys[1]));
                break;

            case Cryptoanalyzer.EncryptionMethods.Polynomial:
                var polynomials = PolynomialCipher.EncodeMessage(InputBox.Text, int.Parse(rawKey));
                cipherText = string.Join("\r\n", polynomials);
                break;

            case Cryptoanalyzer.EncryptionMethods.Multiplicative:
                cipherText = Cryptoanalyzer.EncryptMultiplicativeCipher(plainText, int.Parse(rawKey));
                break;

            case Cryptoanalyzer.EncryptionMethods.PolynomialBlock:
                cipherText = Cryptoanalyzer.EncryptPolynomialBlockCipher(InputBox.Text, int.Parse(rawKey));
                break;

            case Cryptoanalyzer.EncryptionMethods.Bifid:
                cipherText = Cryptoanalyzer.EncryptBifidCipher(plainText, rawKey);
                break;

            case Cryptoanalyzer.EncryptionMethods.Atbash:
                cipherText = Cryptoanalyzer.DecryptAtbashCipher(plainText);
                break;

            case Cryptoanalyzer.EncryptionMethods.MorseCode:
                string[] morseCodeChars = rawKey.Split(',', ';', ':', ' ');
                cipherText = morseCodeChars.Length <= 3
                        ? Cryptoanalyzer.EncryptMorseCode(plainText)
                        : Cryptoanalyzer.EncryptMorseCode(plainText, morseCodeChars[0], morseCodeChars[1], morseCodeChars[2]);
                break;

            case Cryptoanalyzer.EncryptionMethods.Ascii:
                cipherText = Cryptoanalyzer.EncryptAsciiEncoding(plainText, int.Parse(rawKey));
                break;

            case Cryptoanalyzer.EncryptionMethods.Vigenere:
                cipherText = Cryptoanalyzer.EncryptVigenereCipher(plainText, rawKey);
                break;
            }
            OutputBox.Text = cipherText;
        }
コード例 #2
0
        private void UpdateCipherText(bool decrypt)
        {
            if (!IsLoaded)
            {
                return;
            }

            PolynomialOutputBox.Text = "";

            if (decrypt)
            {
                // Parse the polynomials and decode them
                List <List <double> > coeffs = new List <List <double> >();
                string[] inputPolynomials    = InputBox.Text.Split('\r', '\n', StringSplitOptions.RemoveEmptyEntries);
                foreach (string inputPoly in inputPolynomials)
                {
                    List <double> polyCoeffs = new List <double>();
                    foreach (string inputCoeff in inputPoly.Split(' ', StringSplitOptions.RemoveEmptyEntries))
                    {
                        polyCoeffs.Add(Double.Parse(inputCoeff));
                    }

                    polyCoeffs.Reverse();

                    // Send the coefficients to be decoded
                    PolynomialOutputBox.Text += PolynomialCipher.DecodePolynomial(polyCoeffs, (int)KeyBox.Value) + "\r\n";
                }
            }
            else
            {
                var polynomials = PolynomialCipher.EncodeMessage(InputBox.Text, (int)KeyBox.Value);
                foreach (string output in polynomials)
                {
                    PolynomialOutputBox.Text += output + "\r\n";
                }
            }

            return;

            // Don't do this:
            // Generate one gigantic polynomial for the entire message
            Debug.WriteLine("Encryption started: " + DateTime.Now);
            PolynomialOutputBox.Text +=
                PolynomialCipher.CoeffsToPolynomial(PolynomialCipher.EncryptString(InputBox.Text, 1));
            Debug.WriteLine("Encryption Finished: " + DateTime.Now);
        }
コード例 #3
0
        private async void DecryptButton_Click(object sender, RoutedEventArgs e)
        {
            string cipherText = InputBox.Text;
            string rawKey     = KeyBox.Text;
            string plainText  = "";

            List <List <double> > coeffs;

            string[] inputPolynomials;

            switch ((Cryptoanalyzer.EncryptionMethods)EncryptionMethodBox.SelectedIndex)
            {
                #region Shift
            case Cryptoanalyzer.EncryptionMethods.Shift:
                plainText = Cryptoanalyzer.DecryptShiftCipher(cipherText, int.Parse(rawKey));
                break;
                #endregion

                #region OTP
            case Cryptoanalyzer.EncryptionMethods.OneTimePad:
                plainText = Cryptoanalyzer.DecryptOneTimePad(cipherText, rawKey);
                break;
                #endregion

                #region Affine
            case Cryptoanalyzer.EncryptionMethods.Affine:
                string[] keys = rawKey.Split(',', ';', ':', ' ');
                try
                {
                    plainText = Cryptoanalyzer.DecryptAffineCipher(cipherText,
                                                                   int.Parse(keys[0]), int.Parse(keys[1]));
                }
                catch (ArgumentException ex)
                {
                    await new ContentDialog()
                    {
                        Content           = "Failed to decrypt message, check key: " + ex.Message,
                        Title             = "Error",
                        PrimaryButtonText = "OK"
                    }.ShowAsync();
                }
                break;
                #endregion

                #region Polynomial
            case Cryptoanalyzer.EncryptionMethods.Polynomial:
                // Parse the polynomials and decode them
                coeffs           = new List <List <double> >();
                inputPolynomials = InputBox.Text.Split('\r', '\n', StringSplitOptions.RemoveEmptyEntries);
                foreach (string inputPoly in inputPolynomials)
                {
                    List <double> polyCoeffs = new List <double>();
                    foreach (string inputCoeff in inputPoly.Split(' ', StringSplitOptions.RemoveEmptyEntries))
                    {
                        polyCoeffs.Add(double.Parse(inputCoeff));
                    }

                    polyCoeffs.Reverse();

                    // Send the coefficients to be decoded
                    plainText += PolynomialCipher.DecodePolynomial(polyCoeffs, int.Parse(rawKey)) + "\r\n";
                }
                break;
                #endregion

                #region Multiplicative
            case Cryptoanalyzer.EncryptionMethods.Multiplicative:
                plainText = Cryptoanalyzer.DecryptMultiplicativeCipher(cipherText, int.Parse(rawKey));
                break;
                #endregion

                #region Atbash
            case Cryptoanalyzer.EncryptionMethods.Atbash:
                plainText = Cryptoanalyzer.DecryptAtbashCipher(cipherText);
                break;
                #endregion

                #region Morse Code
            case Cryptoanalyzer.EncryptionMethods.MorseCode:
                string[] morseCodeChars = rawKey.Split(',', ';', ':', ' ');
                plainText = morseCodeChars.Length != 2
                        ? Cryptoanalyzer.DecryptMorseCode(cipherText)
                        : Cryptoanalyzer.DecryptMorseCode(cipherText, morseCodeChars[0], morseCodeChars[1], morseCodeChars[2]);
                break;
                #endregion

                #region Polynomial Block
            case Cryptoanalyzer.EncryptionMethods.PolynomialBlock:
                // Parse the polynomials and decode them
                string[] blockInputPolynomials    = InputBox.Text.Split('\r', '\n', StringSplitOptions.RemoveEmptyEntries);
                List <List <double> > blockCoeffs = blockInputPolynomials.Select(PolynomialCipher.ParsePolynomial).ToList();
                plainText = Cryptoanalyzer.DecryptPolynomialBlockCipher(blockCoeffs, int.Parse(rawKey));
                break;
                #endregion

                #region ASCII Encoding
            case Cryptoanalyzer.EncryptionMethods.Ascii:
                plainText = Cryptoanalyzer.DecryptAsciiEncoding(cipherText, int.Parse(rawKey));
                break;
                #endregion

                #region Vigenere
            case Cryptoanalyzer.EncryptionMethods.Vigenere:
                plainText = Cryptoanalyzer.DecryptVigenereCipher(cipherText, rawKey);
                break;
                #endregion
            }
            OutputBox.Text = plainText;
        }