public String DecryptMessage(String encryptedMessage, char[] passPhrase)
        {
            // Remove the Base64 encoding
            byte[] rawMessage = Convert.FromBase64String(encryptedMessage);

            Stream inputStream = new MemoryStream(rawMessage);

            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpObjectFactory     pgpF = new PgpObjectFactory(inputStream);
            PgpEncryptedDataList enc  = null;
            PgpObject            o    = pgpF.NextPgpObject();

            //
            // the first object might be a PGP marker packet.
            //
            if (o is PgpEncryptedDataList)
            {
                enc = (PgpEncryptedDataList)o;
            }
            else
            {
                enc = (PgpEncryptedDataList)pgpF.NextPgpObject();
            }

            PgpPbeEncryptedData pbe = (PgpPbeEncryptedData)enc[0];

            Stream clear = pbe.GetDataStream(passPhrase);

            PgpObjectFactory pgpFact = new PgpObjectFactory(clear);

            PgpLiteralData ld = (PgpLiteralData)pgpFact.NextPgpObject();

            Stream unc = ld.GetInputStream();

            String message;

            using (StreamReader reader = new StreamReader(unc, Encoding.UTF8))
            {
                message = reader.ReadToEnd();
            }

            // Finally verify the integrity
            if (pbe.IsIntegrityProtected())
            {
                if (!pbe.Verify())
                {
                    throw new MessageVerificationException("Failed to verify the message. It might have been modified in transit.");
                }
            }

            return(message);
        }
        public void DecryptFile(Stream outStream, Stream inputStream, char[] passPhrase)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpObjectFactory     pgpF = new PgpObjectFactory(inputStream);
            PgpEncryptedDataList enc  = null;
            PgpObject            o    = pgpF.NextPgpObject();

            //
            // the first object might be a PGP marker packet.
            //
            if (o is PgpEncryptedDataList)
            {
                enc = (PgpEncryptedDataList)o;
            }
            else
            {
                enc = (PgpEncryptedDataList)pgpF.NextPgpObject();
            }

            PgpPbeEncryptedData pbe = (PgpPbeEncryptedData)enc[0];

            Stream clear = pbe.GetDataStream(passPhrase);

            PgpObjectFactory pgpFact = new PgpObjectFactory(clear);

            PgpLiteralData ld = (PgpLiteralData)pgpFact.NextPgpObject();

            Stream unc = ld.GetInputStream();

            byte[] buf = new byte[1 << 16];
            int    len;

            while ((len = unc.Read(buf, 0, buf.Length)) > 0)
            {
                outStream.Write(buf, 0, len);
            }

            // Finally verify the integrity
            if (pbe.IsIntegrityProtected())
            {
                if (!pbe.Verify())
                {
                    throw new MessageVerificationException("Failed to verify the message. It might have been modified in transit.");
                }
            }
        }
        private byte[] DecryptMessageBuffered(
            byte[] message)
        {
            PgpObjectFactory     pgpF = new PgpObjectFactory(message);
            PgpEncryptedDataList enc  = (PgpEncryptedDataList)pgpF.NextPgpObject();
            PgpPbeEncryptedData  pbe  = (PgpPbeEncryptedData)enc[0];

            Stream clear = pbe.GetDataStream(pass);

            PgpObjectFactory  pgpFact = new PgpObjectFactory(clear);
            PgpCompressedData cData   = (PgpCompressedData)pgpFact.NextPgpObject();

            pgpFact = new PgpObjectFactory(cData.GetDataStream());

            PgpLiteralData ld = (PgpLiteralData)pgpFact.NextPgpObject();

            MemoryStream bOut = new MemoryStream();

            if (!ld.FileName.Equals("test.txt") &&
                !ld.FileName.Equals("_CONSOLE"))
            {
                Fail("wrong filename in packet");
            }
            if (!ld.ModificationTime.Equals(TestDateTime))
            {
                Fail("wrong modification time in packet: " + ld.ModificationTime.Ticks + " " + TestDateTime.Ticks);
            }

            Stream unc = ld.GetInputStream();

            byte[] buf = new byte[1024];

            int len;

            while ((len = unc.Read(buf, 0, buf.Length)) > 0)
            {
                bOut.Write(buf, 0, len);
            }

            if (pbe.IsIntegrityProtected() && !pbe.Verify())
            {
                Fail("integrity check failed");
            }

            return(bOut.ToArray());
        }
        /**
         * decrypt the passed in message stream
         */
        private byte[] DecryptMessage(
            byte[] message)
        {
            PgpObjectFactory     pgpF = new PgpObjectFactory(message);
            PgpEncryptedDataList enc  = (PgpEncryptedDataList)pgpF.NextPgpObject();
            PgpPbeEncryptedData  pbe  = (PgpPbeEncryptedData)enc[0];
            Stream clear = pbe.GetDataStream(pass);

            PgpObjectFactory  pgpFact = new PgpObjectFactory(clear);
            PgpCompressedData cData   = (PgpCompressedData)pgpFact.NextPgpObject();

            pgpFact = new PgpObjectFactory(cData.GetDataStream());

            PgpLiteralData ld = (PgpLiteralData)pgpFact.NextPgpObject();

            if (!ld.FileName.Equals("test.txt") &&
                !ld.FileName.Equals("_CONSOLE"))
            {
                Fail("wrong filename in packet");
            }

            if (!ld.ModificationTime.Equals(TestDateTime))
            {
                Fail("wrong modification time in packet: " + ld.ModificationTime + " vs " + TestDateTime);
            }

            Stream unc = ld.GetInputStream();

            byte[] bytes = Streams.ReadAll(unc);

            if (pbe.IsIntegrityProtected() && !pbe.Verify())
            {
                Fail("integrity check failed");
            }

            return(bytes);
        }
Beispiel #5
0
        /**
         * decrypt the passed in message stream
         */
        private static void DecryptFile(
            Stream inputStream,
            char[]      passPhrase)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
            PgpObject        o    = pgpF.NextPgpObject();

            //
            // the first object might be a PGP marker packet.
            //
            PgpEncryptedDataList enc = o as PgpEncryptedDataList;

            if (enc == null)
            {
                enc = (PgpEncryptedDataList)pgpF.NextPgpObject();
            }

            PgpPbeEncryptedData pbe = (PgpPbeEncryptedData)enc[0];

            Stream clear = pbe.GetDataStream(passPhrase);

            PgpObjectFactory pgpFact = new PgpObjectFactory(clear);

            //
            // if we're trying to read a file generated by someone other than us
            // the data might not be compressed, so we check the return type from
            // the factory and behave accordingly.
            //
            o = pgpFact.NextPgpObject();
            if (o is PgpCompressedData)
            {
                PgpCompressedData cData = (PgpCompressedData)o;
                pgpFact = new PgpObjectFactory(cData.GetDataStream());
                o       = pgpFact.NextPgpObject();
            }

            PgpLiteralData ld   = (PgpLiteralData)o;
            Stream         unc  = ld.GetInputStream();
            Stream         fOut = File.Create(ld.FileName);

            Streams.PipeAll(unc, fOut);
            fOut.Close();

            if (pbe.IsIntegrityProtected())
            {
                if (!pbe.Verify())
                {
                    Console.Error.WriteLine("message failed integrity check");
                }
                else
                {
                    Console.Error.WriteLine("message integrity check passed");
                }
            }
            else
            {
                Console.Error.WriteLine("no message integrity check");
            }
        }