Esempio n. 1
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            // make sure the user has entered a plain text message
            if (txtPlainText.Text != "" && txtPlainText.Text != " ")
            {
                int    cipher     = cboCipher.SelectedIndex;
                string plainText  = txtPlainText.Text;
                string cipherText = null;

                // instantiate the class for the selected cipher, declare and assign value
                // to a key variable if neccesary, call the encrypt method
                switch (cipher)
                {
                case 0:
                    HashFunction hash = new HashFunction();

                    cipherText = hash.EncrpytHash(plainText);

                    break;

                case 1:
                    Caesar caesar = new Caesar();
                    try
                    {
                        int cKey = Convert.ToInt32(txtKey.Text);

                        cipherText = caesar.EncrpytCaesar(plainText, cKey);
                    }
                    catch (FormatException)
                    {
                        // users must enter a numeric key
                        MessageBox.Show("Please enter a valid encryption key");
                    }
                    break;

                case 2:
                    try {
                        RailFence railFence = new RailFence();
                        int       rKey      = Convert.ToInt32(txtKey.Text);

                        cipherText = railFence.encryptRailFence(plainText, rKey);
                    }
                    catch (FormatException)
                    {
                        // users must enter a numeric key
                        MessageBox.Show("Please enter a valid encryption key");
                    }
                    break;

                case 3:
                    Polybius polybius = new Polybius();

                    cipherText = polybius.encryptPoly(plainText);

                    break;

                case 4:
                    try
                    {
                        Gronsfeld gronsfeld = new Gronsfeld();
                        int       gKey      = Convert.ToInt32(txtKey.Text);

                        cipherText = gronsfeld.encryptGronsfeld(plainText, gKey);
                    }
                    catch (FormatException)
                    {
                        // users must enter a numeric key
                        MessageBox.Show("Please enter a valid encryption key");
                    }
                    break;

                case 5:
                    FourSquare fourSquare = new FourSquare();

                    cipherText = fourSquare.encryptFourSquare(plainText);

                    break;
                }
                // ensure that the message was encrypted
                if (cipherText != null)
                {
                    txtCipherText.Text = cipherText;
                }
            }
            else
            {
                MessageBox.Show("Please enter a message to encrypt");
            }
        }
Esempio n. 2
0
        private void btnDecrpyt_Click(object sender, EventArgs e)
        {
            // make sure the user has entered a cipher text message
            int    cipher     = cboCipher.SelectedIndex;
            int    key        = 0;
            string plainText  = null;
            string cipherText = txtCipherText.Text;

            // instantiate the class for the selected cipher, declare and assign value
            // to a key variable if neccesary, call the decrypt method
            switch (cipher)
            {
            case 0:
                MessageBox.Show("Hash functions cannot be decrypted");

                break;

            case 1:
                try {
                    Caesar dCaesar = new Caesar();
                    key = Convert.ToInt32(txtKey.Text);

                    plainText = dCaesar.EncrpytCaesar(cipherText, -key);
                }
                catch (FormatException)
                {
                    // users must enter a numeric key
                    MessageBox.Show("Please enter a valid encryption key");
                }
                break;

            case 2:
                try {
                    RailFence dRailFence = new RailFence();
                    key = Convert.ToInt32(txtKey.Text);

                    plainText = dRailFence.decryptRailFence(cipherText, key);
                }
                catch (FormatException)
                {
                    // users must enter a numeric key
                    MessageBox.Show("Please enter a valid encryption key");
                }
                break;

            case 3:
                Polybius dPolybius = new Polybius();

                plainText = dPolybius.decryptPoly(cipherText);

                break;

            case 4:
                try {
                    Gronsfeld dGronsfeld = new Gronsfeld();
                    key = Convert.ToInt32(txtKey.Text);

                    plainText = dGronsfeld.encryptGronsfeld(cipherText, -key);
                }
                catch (FormatException)
                {
                    // users must enter a numeric key
                    MessageBox.Show("Please enter a valid encryption key");
                }
                break;
            }

            // ensure that the message was decrypted
            if (cipherText != null)
            {
                txtPlainText.Text = plainText;
            }
        }