Пример #1
0
        /// <summary>
        /// Method handling decrypting and verifying
        /// </summary>
        /// <param name="bData">data to be decrypted</param>
        /// <param name="strPassphrase">passphrase</param>
        /// <param name="fileOut">the decrypted file location if one produced</param>
        /// <returns>verifying process result</returns>
        private string DecryptAndVerify(byte[] bData, string strPassphrase, string fileOut)
        {
            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.SharpPrivacyLib.OpenPGP.Messages.Message mContent = null;

            if (atType == ArmorTypes.OpenPGPSignedMessage) {
                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;

                pPackets = Packet.ParsePackets(strSignature);

                if (!(pPackets[0] is SignaturePacket)) {
                    throw new Exception("Not a valid cleartext signature!");
                }
                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();
                Packet[] pPackets;
                try {
                    pPackets = Packet.ParsePackets(bData);
                    try {
                        emMessage.ParseMessage(pPackets);

                        if (emMessage.SymmetricallyEncrypted) {
                            // Query passphrase for symmetrically encrypted message

                            mContent = emMessage.Decrypt(strPassphrase);

                        } else {
                            ulong lKeyID = emMessage.GetFittingKeyID(skrSecretKeyRing);
                            mContent = emMessage.Decrypt(skrSecretKeyRing, strPassphrase);
                        }
                    } catch (Exception) {
                        mContent = new CompressedMessage();
                        mContent.ParseMessage(pPackets);
                    }

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

            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 Dec: " + smContent.Signature.KeyID.ToString() + " ***\r\n";
                strDisplay += "*** Signing Key Hex: " + smContent.Signature.KeyID.ToString("x") + " ***\r\n";
                try {
                    try {
                        strDisplay += "*** Signing Key Expiration: " + pkrPublicKeyRing.Find(smContent.Signature.KeyID,true).KeyExpirationTime + " ***\r\n";
                    } catch(Exception e) {
                        if(e.Message.Equals("never"))
                            strDisplay += "*** Signing Key Expiration: "+e.Message+" ***\r\n";
                        else
                            throw new Exception("Signing_Key_Not_Available");

                    }
                    try {
                        strDisplay += "*** Signing Key Revoked: "+ this.pkrPublicKeyRing.isRevoked(smContent.Signature.KeyID) +" ***\r\n";
                    } catch(Exception e) {
                        string msg = e.Message;
                        strDisplay += "*** Signing Key Revoked: Revocation_Key_Not_Available ***\r\n";
                    }
                } catch (Exception e) {
                    string warn = e.Message;
                    strDisplay += "*** Signing Key Expiration: " + SignatureStatusTypes.Signing_Key_Not_Available + " ***\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 {
                throw new Exception("An error occured: Could not find an encrypted or signed message!");
            }

            if (lmContent.DataFormat == DataFormatTypes.Text) {
                strDisplay += lmContent.Text;
                strDisplay += "\r\n\r\n*** End OpenPGP Message ***\r\n";
                if(fileOut != null && fileOut != "") {
                    System.IO.FileStream fsOut = new FileStream(fileOut, FileMode.Create);
                    System.IO.BinaryWriter bwOut = new BinaryWriter(fsOut);
                    bwOut.Write(lmContent.Binary);
                    bwOut.Close();
                    fsOut.Close();
                }
            } else {
                System.IO.FileStream fsOut = new FileStream(fileOut, FileMode.Create);
                System.IO.BinaryWriter bwOut = new BinaryWriter(fsOut);
                bwOut.Write(lmContent.Binary);
                bwOut.Close();
                fsOut.Close();
            }
            return strDisplay;
        }
Пример #2
0
        private string DecryptAndVerifyData(byte[] bData, string strPassphrase)
        {
            // let us see what kind of message this is

            bool bSymEncrypted = false;
            bool bAsymEncrypted = false;
            bool bSigned = false;

            SharpPrivacyLib.OpenPGP.Messages.Message mContent = null;
            EncryptedMessage emMessage = new EncryptedMessage();
            Packet[] pPackets = Packet.ParsePackets(bData);

            XmlDocument xmlDoc = new XmlDocument();
            XmlElement xmlMessage = xmlDoc.CreateElement("OpenPGPMessage");

            try {
                emMessage.ParseMessage(pPackets);

                if (emMessage.SymmetricallyEncrypted) {
                    mContent = emMessage.Decrypt(strPassphrase);

                    bSymEncrypted = true;
                } else {
                    ulong lKeyID = emMessage.GetFittingKeyID(skrKeyRing);

                    mContent = emMessage.Decrypt(skrKeyRing, strPassphrase);
                    bAsymEncrypted = true;
                }
            } catch (ArgumentException) {
                //obviously it wasn't an encrypted message. perhaps the
                //message has only been signed, but not encrypted!
                try {
                    SignedMessage smMessage = new SignedMessage();
                    smMessage.ParseMessage(pPackets);
                    mContent = smMessage;
                } catch (Exception e) {
                    throw new Exception("Not a valid OpenPGP Message: " + e.Message);
                }
            }

            while ((!(mContent is LiteralMessage)) && (!(mContent is SignedMessage))) {
                if (mContent is CompressedMessage) {
                    mContent = ((CompressedMessage)mContent).Uncompress();
                } else {
                    throw new Exception("Not a valid OpenPGP Message!");
                }
            }

            LiteralMessage lmContent = new LiteralMessage();

            if (mContent is SignedMessage) {
                bSigned = true;
                SignedMessage smContent = (SignedMessage)mContent;
                lmContent = smContent.MessageSigned;

                xmlMessage.SetAttribute("signaturestatus", smContent.Verify(pkrKeyRing).ToString());
                xmlMessage.SetAttribute("signingkey", "0x" + smContent.Signature.KeyID.ToString("x"));
                xmlMessage.SetAttribute("signingdate", smContent.Signature.TimeCreated.Ticks.ToString());

                mContent = lmContent;

            }

            xmlMessage.SetAttribute("symmetricallyencrypted", bSymEncrypted.ToString());
            xmlMessage.SetAttribute("asymmetricallyencrypted", bAsymEncrypted.ToString());
            xmlMessage.SetAttribute("signed", bSigned.ToString());

            XmlElement xmlLiteral = xmlDoc.CreateElement("LiteralMessage");
            if (mContent is LiteralMessage) {
                lmContent = (LiteralMessage)mContent;

                xmlLiteral.SetAttribute("dataformat", lmContent.DataFormat.ToString());
                xmlLiteral.SetAttribute("timecreated", lmContent.TimeCreated.Ticks.ToString());
                xmlLiteral.SetAttribute("filename", lmContent.Filename);

            } else {
                throw new Exception("Error decrypting the message!");
            }

            if (lmContent.DataFormat == DataFormatTypes.Binary) {
                xmlLiteral.InnerText = Convert.ToBase64String(lmContent.Binary);
            } else {
                xmlLiteral.InnerText = lmContent.Text;
            }

            xmlMessage.AppendChild(xmlLiteral);

            return xmlMessage.OuterXml;
        }
Пример #3
0
        public ulong GetDecryptionKey(string strMessage)
        {
            ArmorTypes atType = new ArmorTypes();
            string strRest = "";
            string strRadix64 = Armor.RemoveArmor(strMessage, ref atType, ref strRest);
            if (strRadix64.Length == 0)
                throw new Exception("This is not a valid OpenPGP message!");

            EncryptedMessage emMessage = new EncryptedMessage();
            Packet[] pPackets = Packet.ParsePackets(strRadix64);

            emMessage.ParseMessage(pPackets);

            if (!emMessage.SymmetricallyEncrypted) {
                ulong lKeyID = emMessage.GetFittingKeyID(skrKeyRing);
                return lKeyID;
            } else
                return 0ul;
        }
Пример #4
0
        public ulong GetDecryptionKeyFromFile(string strPath)
        {
            System.IO.FileStream fsIn = new FileStream(strPath, FileMode.Open);
            System.IO.BinaryReader brIn = new BinaryReader(fsIn);

            byte[] bData = new byte[fsIn.Length];
            brIn.Read(bData, 0, bData.Length);

            try {
                string strMessage = System.Text.Encoding.UTF8.GetString(bData);
                return GetDecryptionKey(strMessage);
            } catch (Exception) {
                EncryptedMessage emMessage = new EncryptedMessage();
                Packet[] pPackets = Packet.ParsePackets(bData);

                emMessage.ParseMessage(pPackets);

                if (!emMessage.SymmetricallyEncrypted)
                    return emMessage.GetFittingKeyID(skrKeyRing);
                else
                    return 0ul;
            }
        }
Пример #5
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();
                    }
                }
            }
        }