Esempio n. 1
0
        void mnuClipboardDecryptVerify_Click(Object sender, System.EventArgs e)
        {
            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            bool  bAskForPassphrase = true;
            ulong lDecryptionKey    = 0;

            try {
                SharpPrivacy.ReloadKeyRing();
                lDecryptionKey = SharpPrivacy.Instance.GetDecryptionKey(strMessage);
            } catch (Exception ex) {
                bAskForPassphrase = false;
                Console.WriteLine(ex.Message);
            }

            string strPassphrase = "";

            if (bAskForPassphrase)
            {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                if (lDecryptionKey > 0)
                {
                    //Find the fitting key
                    XmlElement xmlKey = FindSecretKey(lDecryptionKey);
                    if (xmlKey == null)
                    {
                        MessageBox.Show("An unexpected error occured: The secret key used to decrypt the message could not be found.", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        return;
                    }
                    qpPassphrase.ShowSingleKeyDialog(xmlKey);
                    strPassphrase = qpPassphrase.Passphrase;
                }
                else
                {
                    qpPassphrase.ShowMyDialog();
                    strPassphrase = qpPassphrase.Passphrase;
                }
            }

            try {
                PlaintextViewer pvPlaintext = new PlaintextViewer();
                SharpPrivacy.ReloadKeyRing();
                pvPlaintext.XmlMessage = SharpPrivacy.Instance.DecryptAndVerify(strMessage, strPassphrase);
                pvPlaintext.ShowPlaintext();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while decrypting the message: " + ex.Message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Esempio n. 2
0
        void mnuFileSystemDecryptVerify_Click(Object sender, System.EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = false;
            ofdOpen.Filter      = "OpenPGP Files (*.asc;*.enc)|*.asc;*.enc|All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            bool  bAskForPassphrase = true;
            ulong lDecryptionKey    = 0;

            try {
                SharpPrivacy.ReloadKeyRing();
                lDecryptionKey = SharpPrivacy.Instance.GetDecryptionKeyFromFile(ofdOpen.FileName);
            } catch (Exception) {
                bAskForPassphrase = false;
            }

            string strPassphrase = "";

            if (bAskForPassphrase)
            {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                if (lDecryptionKey > 0)
                {
                    //Find the fitting key
                    XmlElement xmlKey = FindSecretKey(lDecryptionKey);
                    qpPassphrase.ShowSingleKeyDialog(xmlKey);
                    strPassphrase = qpPassphrase.Passphrase;
                }
                else
                {
                    qpPassphrase.ShowMyDialog();
                    strPassphrase = qpPassphrase.Passphrase;
                }
            }

            try {
                PlaintextViewer pvPlaintext = new PlaintextViewer();
                SharpPrivacy.ReloadKeyRing();
                pvPlaintext.XmlMessage = SharpPrivacy.Instance.DecryptAndVerifyFile(ofdOpen.FileName, strPassphrase);
                pvPlaintext.ShowPlaintext();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while decrypting the message: " + ex.Message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Esempio n. 3
0
        void mnuFileSystemDecryptVerify_Click(Object sender, System.EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = false;
            ofdOpen.Filter = "OpenPGP Files (*.asc;*.enc)|*.asc;*.enc|All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            bool bAskForPassphrase = true;
            ulong lDecryptionKey = 0;
            try {
                SharpPrivacy.ReloadKeyRing();
                lDecryptionKey = SharpPrivacy.Instance.GetDecryptionKeyFromFile(ofdOpen.FileName);
            } catch (Exception) {
                bAskForPassphrase = false;
            }

            string strPassphrase = "";
            if (bAskForPassphrase) {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                if (lDecryptionKey > 0) {
                    //Find the fitting key
                    XmlElement xmlKey = FindSecretKey(lDecryptionKey);
                    qpPassphrase.ShowSingleKeyDialog(xmlKey);
                    strPassphrase = qpPassphrase.Passphrase;
                } else {
                    qpPassphrase.ShowMyDialog();
                    strPassphrase = qpPassphrase.Passphrase;
                }
            }

            try {
                PlaintextViewer pvPlaintext = new PlaintextViewer();
                SharpPrivacy.ReloadKeyRing();
                pvPlaintext.XmlMessage = SharpPrivacy.Instance.DecryptAndVerifyFile(ofdOpen.FileName, strPassphrase);
                pvPlaintext.ShowPlaintext();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while decrypting the message: " + ex.Message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Esempio n. 4
0
        private static void DecryptAndVerify(SecretKeyRing skrSecretKeyRing, PublicKeyRing pkrPublicKeyRing, byte[] bData)
        {
            string strMessage = System.Text.Encoding.UTF8.GetString(bData);
            ArmorTypes atType = new ArmorTypes();
            string strRest = "";
            string strRadix64 = Armor.RemoveArmor(strMessage, ref atType, ref strRest);
            if (strRadix64.Length > 0)
                bData = Radix64.Decode(strRadix64);

            SharpPrivacy.OpenPGP.Messages.Message mContent = null;

            if (atType == ArmorTypes.OpenPGPSignature) {
                string strSignature = "";
                string strSignedMessage = Armor.RemoveClearSignatureArmor(strMessage, ref atType, ref strSignature);

                strSignedMessage = Radix64.DashUnescape(strSignedMessage);
                strSignedMessage = Radix64.TrimMessage(strSignedMessage);
                SignedMessage smMessage = new SignedMessage();
                Packet[] pPackets = Packet.ParsePackets(strSignature);
                if (!(pPackets[0] is SignaturePacket)) {
                    MessageBox.Show("Not a valid cleartext signature!");
                    return;
                }
                smMessage.Signature = (SignaturePacket)pPackets[0];

                LiteralMessage lmMessage = new LiteralMessage(DataFormatTypes.Text);
                lmMessage.Text = strSignedMessage;
                smMessage.MessageSigned = lmMessage;

                mContent = smMessage;
            } else {

                // let us see what kind of message this is
                EncryptedMessage emMessage = new EncryptedMessage();
                try {
                    Packet[] pPackets = Packet.ParsePackets(bData);
                    emMessage.ParseMessage(pPackets);

                    if (emMessage.SymmetricallyEncrypted) {
                        // Query passphrase for symmetrically encrypted message
                        QueryPassphrase qpPassphrase = new QueryPassphrase();
                        qpPassphrase.ShowMyDialog();
                        string strPassphrase = qpPassphrase.Passphrase;

                        mContent = emMessage.Decrypt(strPassphrase);

                    } else {
                        ulong lKeyID = emMessage.GetFittingKeyID(skrSecretKeyRing);
                        QueryPassphrase qpPassphrase = new QueryPassphrase();
                        qpPassphrase.ShowMyDialog(skrSecretKeyRing.Find(lKeyID));
                        string strPassphrase = qpPassphrase.Passphrase;

                        mContent = emMessage.Decrypt(skrSecretKeyRing, strPassphrase);
                    }

                    while ((!(mContent is LiteralMessage)) && (!(mContent is SignedMessage))) {
                        if (mContent is CompressedMessage) {
                            mContent = ((CompressedMessage)mContent).Uncompress();
                        } else {
                            MessageBox.Show("This is not a valid OpenPGP message!");
                            return;
                        }
                    }
                } catch (Exception ee) {
                    MessageBox.Show("There was an error decrypting your message: " + ee.Message);
                    return;
                }
            }

            LiteralMessage lmContent = new LiteralMessage();
            string strDisplay = "";
            if (mContent is SignedMessage) {
                SignedMessage smContent = (SignedMessage)mContent;
                lmContent = smContent.MessageSigned;
                strDisplay += "*** OpenPGP Signed Message ***\r\n";
                strDisplay += "*** Signature Status: " + smContent.Verify(pkrPublicKeyRing) + " ***\r\n";
                strDisplay += "*** Signing Key: " + smContent.Signature.KeyID.ToString("x") + " ***\r\n";
                strDisplay += "*** Signing Date: " + smContent.Signature.TimeCreated.ToString() + "***\r\n\r\n";
            } else if (mContent is LiteralMessage) {
                lmContent = (LiteralMessage)mContent;
                strDisplay += "*** OpenPGP Encrypted Message ***\r\n\r\n";
            } else {
                MessageBox.Show("An error occured: Could not find an encrypted or signed message!", "Error...");
                return;
            }

            if (lmContent.DataFormat == DataFormatTypes.Text) {
                strDisplay += lmContent.Text;
                strDisplay += "\r\n\r\n*** End OpenPGP Message ***\r\n";
                PlaintextViewer pvViewer = new PlaintextViewer();
                pvViewer.MessageText = strDisplay;
                pvViewer.Show();
            } else {
                if (MessageBox.Show(strDisplay, "Signature Status...", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.OK) {
                    System.Windows.Forms.SaveFileDialog sfdSave = new SaveFileDialog();
                    sfdSave.OverwritePrompt = true;
                    sfdSave.Filter = "All Files (*.*)|*.*";
                    sfdSave.FileName = lmContent.Filename;
                    sfdSave.ShowDialog();
                    if (sfdSave.FileName.Length > 0) {
                        System.IO.FileStream fsOut = new FileStream(sfdSave.FileName, FileMode.CreateNew);
                        System.IO.BinaryWriter bwOut = new BinaryWriter(fsOut);
                        bwOut.Write(lmContent.Binary);
                        bwOut.Close();
                        fsOut.Close();
                    }
                }
            }
        }
Esempio n. 5
0
        void mnuClipboardDecryptVerify_Click(Object sender, System.EventArgs e)
        {
            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            bool bAskForPassphrase = true;
            ulong lDecryptionKey = 0;
            try {
                SharpPrivacy.ReloadKeyRing();
                lDecryptionKey = SharpPrivacy.Instance.GetDecryptionKey(strMessage);
            } catch (Exception ex) {
                bAskForPassphrase = false;
                Console.WriteLine(ex.Message);
            }

            string strPassphrase = "";
            if (bAskForPassphrase) {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                if (lDecryptionKey > 0) {
                    //Find the fitting key
                    XmlElement xmlKey = FindSecretKey(lDecryptionKey);
                    if (xmlKey == null) {
                        MessageBox.Show("An unexpected error occured: The secret key used to decrypt the message could not be found.", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        return;
                    }
                    qpPassphrase.ShowSingleKeyDialog(xmlKey);
                    strPassphrase = qpPassphrase.Passphrase;
                } else {
                    qpPassphrase.ShowMyDialog();
                    strPassphrase = qpPassphrase.Passphrase;
                }
            }

            try {
                PlaintextViewer pvPlaintext = new PlaintextViewer();
                SharpPrivacy.ReloadKeyRing();
                pvPlaintext.XmlMessage = SharpPrivacy.Instance.DecryptAndVerify(strMessage, strPassphrase);
                pvPlaintext.ShowPlaintext();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while decrypting the message: " + ex.Message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }