Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4337196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:54:39+00:00 2026-05-21T10:54:39+00:00

I am tryng to decrypt a file I just encrypted using bouncycastle, but im

  • 0

I am tryng to decrypt a file I just encrypted using bouncycastle, but im getting this exception:

Premature end of stream in PartialInputStream

I am using the example code from bouncycastle and haven’t changed anything.

I’m getting this when I use this code for encryption:

private static byte[] EncryptFile(byte[] clearData, string fileName, PgpPublicKey encKey, bool withIntegrityCheck)
{
    MemoryStream encOut = new MemoryStream();
    try
    {
        MemoryStream bOut = new MemoryStream();

        PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator( CompressionAlgorithmTag.Zip );

        //PgpUtilities.WriteFileToLiteralData(
        //    comData.Open(bOut),
        //    PgpLiteralData.Binary,
        //    new FileInfo(fileName));
        Stream cos = comData.Open(bOut);
        PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();

        Stream pOut = lData.Open(
            cos,
            PgpLiteralData.Binary,
            fileName,
            clearData.Length,
            DateTime.UtcNow
            );

        lData.Close();
        comData.Close();

        PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator( SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom() );

        cPk.AddMethod(encKey);

        byte[] bytes = bOut.ToArray();

        Stream os = encOut;

        Stream cOut = cPk.Open(os, bytes.Length);
        cOut.Write(bytes, 0, bytes.Length);
        cOut.Close();

        encOut.Close();
    }
    catch (PgpException e)
    {
        Console.Error.WriteLine(e);

        Exception underlyingException = e.InnerException;
        if (underlyingException != null)
        {
            Console.Error.WriteLine(underlyingException.Message);
            Console.Error.WriteLine(underlyingException.StackTrace);
        }
    }
    return encOut.ToArray();
}

I think it has something to do with the PgpLiteralDataGenerator.
But I need to use it because I want to encrypt data from a byte array, not from a file. Is there some other way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:54:39+00:00Added an answer on May 21, 2026 at 10:54 am

    Anybody who is struggeling heres a working code:

    using System;
    using System.Xml;
    using System.IO;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;
    using System.Text;
    
    using Org.BouncyCastle.Bcpg.OpenPgp;
    using Org.BouncyCastle.Security;
    using Org.BouncyCastle.Utilities.IO;
    using Org.BouncyCastle.Utilities.Encoders;
    using Org.BouncyCastle.Bcpg;
    
    class Program
    {
        private static PgpPublicKey ReadPublicKey(Stream inputStream)
        {
    
            inputStream = PgpUtilities.GetDecoderStream(inputStream);
    
            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
    
            //
            // we just loop through the collection till we find a key suitable for encryption, in the real
            // world you would probably want to be a bit smarter about this.
            //
    
            //
            // iterate through the key rings.
            //
    
            foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey k in kRing.GetPublicKeys())
                {
                    if (k.IsEncryptionKey)
                    {
                        return k;
                    }
                }
            }
    
            throw new ArgumentException("Can't find encryption key in key ring.");
        }
    
        private static byte[] EncryptFile(byte[] clearData, string fileName, PgpPublicKey encKey, bool withIntegrityCheck)
        {
    
            MemoryStream bOut = new MemoryStream();
    
            PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
                CompressionAlgorithmTag.Zip);
    
            Stream cos = comData.Open(bOut); // open it with the final destination
            PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();
    
            // we want to Generate compressed data. This might be a user option later,
            // in which case we would pass in bOut.
            Stream pOut = lData.Open(
                cos,                    // the compressed output stream
                PgpLiteralData.Binary,
                fileName,               // "filename" to store
                clearData.Length,       // length of clear data
                DateTime.UtcNow         // current time
            );
    
            pOut.Write(clearData, 0, clearData.Length);
    
            lData.Close();
            comData.Close();
    
            PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, new SecureRandom());
    
            cPk.AddMethod(encKey);
    
            byte[] bytes = bOut.ToArray();
    
            MemoryStream encOut = new MemoryStream();
            Stream os = encOut;
    
            Stream cOut = cPk.Open(os, bytes.Length);
            cOut.Write(bytes, 0, bytes.Length);  // obtain the actual bytes from the compressed stream
            cOut.Close();
    
            encOut.Close();
    
            return encOut.ToArray();
        }
    
        static void Main(string[] args)
        {
            try
            {
                byte[] dataBytes = File.ReadAllBytes("test.xml");
                Stream keyIn = File.OpenRead("cert.asc");
                Stream outStream = File.Create("data.bpg");
                byte[] encrypted = EncryptFile(dataBytes, "data", ReadPublicKey(keyIn), false);
                outStream.Write(encrypted, 0, encrypted.Length);
                keyIn.Close();
                outStream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
    
            Console.ReadLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decrypt an encrypted XML file and put it into a stream
I'm trying to encrypt and decrypt a file stream over a socket using RijndaelManaged,
Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but
I'm trying to decrypt an X509 cert on an android device using Bouncycastle. However,
I'm trying to encrypt a text file using Perl and then decrypt it using
I am trying to decrypt using gpg.exe --passphrase-file my.passphrase --decrypt --output MTR241_20111124.htm MTR241_20111124.htm.gpg (without
I am trying to encrypt/decrypt an XML file. I found this sample for encrypting
I am trying to use the following snippet to decrypt file, which was encrypted
I have a problem while trying to decrypt encrypted assertion using SAML 2.0. The
using BouncyCastle and with help from a stackoverflow question I got this: using System.Net.Sockets;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.