예제 #1
0
        // Decrypt
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            byte[] RSAdecbytes;
            byte[] AESdecbytes;

            byte[] KIV;
            byte[] Cipher;

            byte[] CipherBytes;
            string PlainText;

            // Make sure there is text to decrypt
            if (txtMainText.Text == String.Empty)
            {
                return;
            }

            // Set cursor as hourglass
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                // Show the Get Password Dialog
                password1.Location      = new Point(this.Location.X + 10, this.Location.Y + 50);
                password1.StartPosition = FormStartPosition.Manual;

                password1.ShowDialog();

                string password = password1.Password;
                if (password == String.Empty)
                {
                    return;
                }

                // Read Private Keys
                string privkey = crypto.RSAReadPrivateKey(keyStorePath + cmbChooseKey.Text + ".prvkey", password);

                // Recover RSA and AES ciphers bytes from Base64
                try
                {
                    CipherBytes = Convert.FromBase64String(crypto.RemoveWhitespace(txtMainText.Text));
                }
                catch
                {
                    MessageBox.Show("Cannot Decrypt non-cipher text!", "Cipher Text Error");

                    // Set cursor as default arrow
                    Cursor.Current = Cursors.Default;
                    return;
                }

                // Split Key and IV bytes from Cipher bytes
                KIV    = crypto.GetKeyIVfromKeyIVCipher(CipherBytes);
                Cipher = crypto.GetCipherfromKeyIVCipher(CipherBytes);

                if (KIV == null || Cipher == null)
                {
                    MessageBox.Show("RSA/AES Cipher Split Error!", "RSA/AES Split Error");

                    // Set cursor as default arrow
                    Cursor.Current = Cursors.Default;
                    return;
                }

                // Decrypt the Key and IV
                RSAdecbytes = crypto.RSADecrypt(privkey, KIV);

                if (RSAdecbytes == null)
                {
                    MessageBox.Show("RSA Decryption failed!", "RSA Decryption Error");

                    // Set cursor as default arrow
                    Cursor.Current = Cursors.Default;
                    return;
                }

                // Decrypt AES
                AESdecbytes = crypto.AESDecrypt(Cipher, crypto.GetKeyfromKeyIV(RSAdecbytes), crypto.GetIVfromKeyIV(RSAdecbytes));

                if (AESdecbytes == null)
                {
                    MessageBox.Show("AES Decryption failed!", "AES Decryption Error");

                    // Set cursor as default arrow
                    Cursor.Current = Cursors.Default;
                    return;
                }

                // Extract Plain Text
                PlainText = Encoding.UTF8.GetString(AESdecbytes);

                // Check to see if we have a file to download
                if (PlainText[0] == '<')
                {
                    int x = PlainText.IndexOf('>');

                    string fileName = PlainText.Substring(1, x - 1);
                    string contents = PlainText.Substring(x + 1);

                    try
                    {
                        File.WriteAllBytes(fileStorePath + fileName, Convert.FromBase64String(contents));
                    }
                    catch
                    {
                        MessageBox.Show("Failed to download file", "File Error");

                        // Set cursor as default arrow
                        Cursor.Current = Cursors.Default;
                        return;
                    }

                    txtMainText.Text = "Downloaded file: " + fileStorePath + fileName;

                    // Set cursor as default arrow
                    Cursor.Current = Cursors.Default;
                    return;
                }

                // Send to main window
                txtMainText.Text = PlainText;
            }
            catch
            {
                MessageBox.Show("General Decryption failure!", "Decryption Error");

                // Set cursor as default arrow
                Cursor.Current = Cursors.Default;
                return;
            }

            // Set cursor as default arrow
            Cursor.Current = Cursors.Default;
        }