PGP PgpPublicKeyEncryptedData Throws Premature end of stream in PartialInputStream












0















Ive seen a couple of threads on this for Java about closing the stream. I just don't understand what they are getting at though. I will convert everything to usings again once i get the issue figured out.



Is it possible that its because I'm storing the Private Key as a string then converting it?



        public static MemoryStream StringToStream(string toConvert)
{
// convert string to stream
var byteArray = Encoding.Default.GetBytes(toConvert);
//byte byteArray = Encoding.ASCII.GetBytes(contents);
var stream = new MemoryStream(byteArray);

return stream;
}


I have intentions of enhancing this a bit but when i couldnt get it working I went back to basically the version posted here Original Post



public static byte DecryptBytes(byte inputData)
{
if(!PrivateKeyPopulated)
{ throw new Exception("PrivateKey Must be populated!!!");}

if (String.IsNullOrWhiteSpace(_passcode))
{ throw new Exception("Passcode Must be populated!!!"); }

byte error = Encoding.ASCII.GetBytes("ERROR");

Stream inputStream = new MemoryStream(inputData);
inputStream = PgpUtilities.GetDecoderStream(inputStream);
MemoryStream decoded = new MemoryStream();

try
{
PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
PgpEncryptedDataList enc;
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();

//
// find the secret key
//
PgpPrivateKey sKey = null;
PgpPublicKeyEncryptedData pbe = null;
PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
PgpUtilities.GetDecoderStream(PSS_PGPEncrypt.StringToStream(_privateKey)));
foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
{
sKey = FindSecretKey(pgpSec, pked.KeyId, _passcode.ToCharArray());
if (sKey != null)
{
pbe = pked;
break;
}
}
if (sKey == null)
throw new ArgumentException("secret key for message not found.");

Stream clear = pbe.GetDataStream(sKey);<------ KABOOOM
PgpObjectFactory plainFact = new PgpObjectFactory(clear);
PgpObject message = plainFact.NextPgpObject();

if (message is PgpCompressedData)
{
PgpCompressedData cData = (PgpCompressedData)message;
PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream());
message = pgpFact.NextPgpObject();
}
if (message is PgpLiteralData)
{
PgpLiteralData ld = (PgpLiteralData)message;
Stream unc = ld.GetInputStream();
Streams.PipeAll(unc, decoded);
}
else if (message is PgpOnePassSignatureList)
throw new PgpException("encrypted message contains a signed message - not literal data.");
else
throw new PgpException("message is not a simple encrypted file - type unknown.");

if (pbe.IsIntegrityProtected())
{
if (!pbe.Verify())
throw new Exception("PGP Error - Message failed integrity check.");
//else
//"Message integrity check passed.", "PGP Error"
}
else
{
//MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

return decoded.ToArray();
}


enter image description here










share|improve this question





























    0















    Ive seen a couple of threads on this for Java about closing the stream. I just don't understand what they are getting at though. I will convert everything to usings again once i get the issue figured out.



    Is it possible that its because I'm storing the Private Key as a string then converting it?



            public static MemoryStream StringToStream(string toConvert)
    {
    // convert string to stream
    var byteArray = Encoding.Default.GetBytes(toConvert);
    //byte byteArray = Encoding.ASCII.GetBytes(contents);
    var stream = new MemoryStream(byteArray);

    return stream;
    }


    I have intentions of enhancing this a bit but when i couldnt get it working I went back to basically the version posted here Original Post



    public static byte DecryptBytes(byte inputData)
    {
    if(!PrivateKeyPopulated)
    { throw new Exception("PrivateKey Must be populated!!!");}

    if (String.IsNullOrWhiteSpace(_passcode))
    { throw new Exception("Passcode Must be populated!!!"); }

    byte error = Encoding.ASCII.GetBytes("ERROR");

    Stream inputStream = new MemoryStream(inputData);
    inputStream = PgpUtilities.GetDecoderStream(inputStream);
    MemoryStream decoded = new MemoryStream();

    try
    {
    PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
    PgpEncryptedDataList enc;
    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();

    //
    // find the secret key
    //
    PgpPrivateKey sKey = null;
    PgpPublicKeyEncryptedData pbe = null;
    PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
    PgpUtilities.GetDecoderStream(PSS_PGPEncrypt.StringToStream(_privateKey)));
    foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
    {
    sKey = FindSecretKey(pgpSec, pked.KeyId, _passcode.ToCharArray());
    if (sKey != null)
    {
    pbe = pked;
    break;
    }
    }
    if (sKey == null)
    throw new ArgumentException("secret key for message not found.");

    Stream clear = pbe.GetDataStream(sKey);<------ KABOOOM
    PgpObjectFactory plainFact = new PgpObjectFactory(clear);
    PgpObject message = plainFact.NextPgpObject();

    if (message is PgpCompressedData)
    {
    PgpCompressedData cData = (PgpCompressedData)message;
    PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream());
    message = pgpFact.NextPgpObject();
    }
    if (message is PgpLiteralData)
    {
    PgpLiteralData ld = (PgpLiteralData)message;
    Stream unc = ld.GetInputStream();
    Streams.PipeAll(unc, decoded);
    }
    else if (message is PgpOnePassSignatureList)
    throw new PgpException("encrypted message contains a signed message - not literal data.");
    else
    throw new PgpException("message is not a simple encrypted file - type unknown.");

    if (pbe.IsIntegrityProtected())
    {
    if (!pbe.Verify())
    throw new Exception("PGP Error - Message failed integrity check.");
    //else
    //"Message integrity check passed.", "PGP Error"
    }
    else
    {
    //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    return decoded.ToArray();
    }


    enter image description here










    share|improve this question



























      0












      0








      0








      Ive seen a couple of threads on this for Java about closing the stream. I just don't understand what they are getting at though. I will convert everything to usings again once i get the issue figured out.



      Is it possible that its because I'm storing the Private Key as a string then converting it?



              public static MemoryStream StringToStream(string toConvert)
      {
      // convert string to stream
      var byteArray = Encoding.Default.GetBytes(toConvert);
      //byte byteArray = Encoding.ASCII.GetBytes(contents);
      var stream = new MemoryStream(byteArray);

      return stream;
      }


      I have intentions of enhancing this a bit but when i couldnt get it working I went back to basically the version posted here Original Post



      public static byte DecryptBytes(byte inputData)
      {
      if(!PrivateKeyPopulated)
      { throw new Exception("PrivateKey Must be populated!!!");}

      if (String.IsNullOrWhiteSpace(_passcode))
      { throw new Exception("Passcode Must be populated!!!"); }

      byte error = Encoding.ASCII.GetBytes("ERROR");

      Stream inputStream = new MemoryStream(inputData);
      inputStream = PgpUtilities.GetDecoderStream(inputStream);
      MemoryStream decoded = new MemoryStream();

      try
      {
      PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
      PgpEncryptedDataList enc;
      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();

      //
      // find the secret key
      //
      PgpPrivateKey sKey = null;
      PgpPublicKeyEncryptedData pbe = null;
      PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
      PgpUtilities.GetDecoderStream(PSS_PGPEncrypt.StringToStream(_privateKey)));
      foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
      {
      sKey = FindSecretKey(pgpSec, pked.KeyId, _passcode.ToCharArray());
      if (sKey != null)
      {
      pbe = pked;
      break;
      }
      }
      if (sKey == null)
      throw new ArgumentException("secret key for message not found.");

      Stream clear = pbe.GetDataStream(sKey);<------ KABOOOM
      PgpObjectFactory plainFact = new PgpObjectFactory(clear);
      PgpObject message = plainFact.NextPgpObject();

      if (message is PgpCompressedData)
      {
      PgpCompressedData cData = (PgpCompressedData)message;
      PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream());
      message = pgpFact.NextPgpObject();
      }
      if (message is PgpLiteralData)
      {
      PgpLiteralData ld = (PgpLiteralData)message;
      Stream unc = ld.GetInputStream();
      Streams.PipeAll(unc, decoded);
      }
      else if (message is PgpOnePassSignatureList)
      throw new PgpException("encrypted message contains a signed message - not literal data.");
      else
      throw new PgpException("message is not a simple encrypted file - type unknown.");

      if (pbe.IsIntegrityProtected())
      {
      if (!pbe.Verify())
      throw new Exception("PGP Error - Message failed integrity check.");
      //else
      //"Message integrity check passed.", "PGP Error"
      }
      else
      {
      //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }

      return decoded.ToArray();
      }


      enter image description here










      share|improve this question
















      Ive seen a couple of threads on this for Java about closing the stream. I just don't understand what they are getting at though. I will convert everything to usings again once i get the issue figured out.



      Is it possible that its because I'm storing the Private Key as a string then converting it?



              public static MemoryStream StringToStream(string toConvert)
      {
      // convert string to stream
      var byteArray = Encoding.Default.GetBytes(toConvert);
      //byte byteArray = Encoding.ASCII.GetBytes(contents);
      var stream = new MemoryStream(byteArray);

      return stream;
      }


      I have intentions of enhancing this a bit but when i couldnt get it working I went back to basically the version posted here Original Post



      public static byte DecryptBytes(byte inputData)
      {
      if(!PrivateKeyPopulated)
      { throw new Exception("PrivateKey Must be populated!!!");}

      if (String.IsNullOrWhiteSpace(_passcode))
      { throw new Exception("Passcode Must be populated!!!"); }

      byte error = Encoding.ASCII.GetBytes("ERROR");

      Stream inputStream = new MemoryStream(inputData);
      inputStream = PgpUtilities.GetDecoderStream(inputStream);
      MemoryStream decoded = new MemoryStream();

      try
      {
      PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
      PgpEncryptedDataList enc;
      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();

      //
      // find the secret key
      //
      PgpPrivateKey sKey = null;
      PgpPublicKeyEncryptedData pbe = null;
      PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
      PgpUtilities.GetDecoderStream(PSS_PGPEncrypt.StringToStream(_privateKey)));
      foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
      {
      sKey = FindSecretKey(pgpSec, pked.KeyId, _passcode.ToCharArray());
      if (sKey != null)
      {
      pbe = pked;
      break;
      }
      }
      if (sKey == null)
      throw new ArgumentException("secret key for message not found.");

      Stream clear = pbe.GetDataStream(sKey);<------ KABOOOM
      PgpObjectFactory plainFact = new PgpObjectFactory(clear);
      PgpObject message = plainFact.NextPgpObject();

      if (message is PgpCompressedData)
      {
      PgpCompressedData cData = (PgpCompressedData)message;
      PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream());
      message = pgpFact.NextPgpObject();
      }
      if (message is PgpLiteralData)
      {
      PgpLiteralData ld = (PgpLiteralData)message;
      Stream unc = ld.GetInputStream();
      Streams.PipeAll(unc, decoded);
      }
      else if (message is PgpOnePassSignatureList)
      throw new PgpException("encrypted message contains a signed message - not literal data.");
      else
      throw new PgpException("message is not a simple encrypted file - type unknown.");

      if (pbe.IsIntegrityProtected())
      {
      if (!pbe.Verify())
      throw new Exception("PGP Error - Message failed integrity check.");
      //else
      //"Message integrity check passed.", "PGP Error"
      }
      else
      {
      //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }

      return decoded.ToArray();
      }


      enter image description here







      c# dll bouncycastle pgp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 14:38







      HellKnight Hicks

















      asked Nov 21 '18 at 18:49









      HellKnight HicksHellKnight Hicks

      5010




      5010
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418753%2fpgp-pgppublickeyencrypteddata-throws-premature-end-of-stream-in-partialinputstre%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418753%2fpgp-pgppublickeyencrypteddata-throws-premature-end-of-stream-in-partialinputstre%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?