[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    Re: [dev-crypto] Calling BCPGInputStream.nextPacketTag() returns 40?
From:       "David A. Sampayo" <david.a.sampayo () gmail ! com>
Date:       2011-11-02 20:25:06
Message-ID: CACK8DETRMjLdM5BqRVifM1pRhW4p1Ucnx=GY9EHkF6dSwOHifQ () mail ! gmail ! com
[Download RAW message or body]

Hi folks,

So for all you who happen to stumble upon this in the future, this was my
problem (very simple):

I wanted to encrypt an array of bytes, but I didn't want to compress it at
all, so I removed the section from the example code which went through
a PGPCompressedDataGenerator. Also, in this section, there happened to be a
part which passed the data through a PGPLiteralDataGenerator. As a result,
I skipped decorating my stream with the PGPLiteralDataGenerator. This was
the cause of my issue. I went from this code:

// retrieved publicKey above

1.  ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
2.  ArmoredOutputStream armoredOutStream = new
ArmoredOutputStream(byteOutStream);
3.
4.  PGPEncryptedDataGenerator encDataGen = new
PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, true, 5. new
SecureRandom(), "BC");
6.  encDataGen.addMethod(publicKey);
7.
8.  OutputStream encOutStream = encDataGen.open(armoredOutStream, "Hello
world".getBytes().length);
9.
10. encOutStream.write( "Hello world".getBytes() );
11.
12. encOutStream.close();
13. armoredOutStream.close();

To changing line 10 to be this:

10. encOutStream.write( makeLiteralData("Hello world".getBytes()) );

Where makeLiteralData is defined as follows:

// Code taken in part from ByteArrayHandler.java->compress
private byte[] getLiteralBytes(byte[] rawData) {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPLiteralDataGenerator literalDataGen = new PGPLiteralDataGenerator();
 try {
OutputStream literalOutStream = literalDataGen.open(bOut,
   PGPLiteralData.BINARY,
   "doesnt_matter",
   rawData.length,
   new java.util.Date());
literalOutStream.write(rawData);
literalOutStream.close();
 return bOut.toByteArray();
 } catch (IOException e) {
e.printStackTrace();
return null;
}
}

Apologies for more noise in the list, I just hope someone may someday find
it helpful.

David S

On Wed, Nov 2, 2011 at 11:16 AM, David A. Sampayo <david.a.sampayo@gmail.com
> wrote:

> I'm currently investigating that error, but future readers may find this
> useful. Will post if I find a solution.
>

[Attachment #3 (text/html)]

Hi folks,<div><br></div><div>So for all you who happen to stumble upon this in the \
future, this was my problem (very simple):</div><div><br></div><div>I wanted to \
encrypt an array of bytes, but I didn&#39;t want to compress it at all, so I removed \
the section from the example code which went through a PGPCompressedDataGenerator. \
Also, in this section, there happened to be a part which passed the data through a \
PGPLiteralDataGenerator. As a result, I skipped decorating my stream with the \
PGPLiteralDataGenerator. This was the cause of my issue. I went from this code:</div> \
<div><br></div><div>// retrieved publicKey above</div><div><br></div><div><div>1.  \
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();</div><div>2.  \
ArmoredOutputStream armoredOutStream = new ArmoredOutputStream(byteOutStream);</div> \
<div>3. <span class="Apple-tab-span" style="white-space:pre">		</span></div><div>4.  \
PGPEncryptedDataGenerator encDataGen = new \
PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, true, 5. new SecureRandom(), \
&quot;BC&quot;);</div> <div>6.  \
encDataGen.addMethod(publicKey);</div><div>7.</div><div>8.  OutputStream encOutStream \
= encDataGen.open(armoredOutStream, &quot;Hello \
world&quot;.getBytes().length);</div><div>9. </div><div>10. encOutStream.write( \
&quot;Hello world&quot;.getBytes() );</div> <div>11.</div><div>12. \
encOutStream.close();</div><div>13. \
armoredOutStream.close();</div><div><br></div><div>To changing line 10 to be \
this:</div><div><br></div><div>10. encOutStream.write( makeLiteralData(&quot;Hello \
world&quot;.getBytes()) );</div> <div><br></div><div>Where makeLiteralData is defined \
as follows:</div><div><br></div><div>// Code taken in part from \
ByteArrayHandler.java-&gt;compress</div><div><div>private byte[] \
getLiteralBytes(byte[] rawData) {</div> <div><span class="Apple-tab-span" \
style="white-space:pre">	</span>ByteArrayOutputStream bOut = new \
ByteArrayOutputStream();</div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>PGPLiteralDataGenerator literalDataGen = new \
PGPLiteralDataGenerator();</div> <div><span class="Apple-tab-span" \
style="white-space:pre">	</span></div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>try {</div><div><span class="Apple-tab-span" \
style="white-space:pre">		</span>OutputStream literalOutStream = \
literalDataGen.open(bOut, </div> <div><span class="Apple-tab-span" \
style="white-space:pre">											</span>   PGPLiteralData.BINARY, </div><div><span \
class="Apple-tab-span" style="white-space:pre">											</span>   \
&quot;doesnt_matter&quot;, </div><div> <span class="Apple-tab-span" \
style="white-space:pre">											</span>   rawData.length, </div><div><span \
class="Apple-tab-span" style="white-space:pre">											</span>   new \
java.util.Date());</div><div><span class="Apple-tab-span" \
style="white-space:pre">		</span>literalOutStream.write(rawData);</div> <div><span \
class="Apple-tab-span" \
style="white-space:pre">		</span>literalOutStream.close();</div><div><span \
class="Apple-tab-span" style="white-space:pre">		</span></div><div><span \
class="Apple-tab-span" style="white-space:pre">		</span>return \
bOut.toByteArray();</div> <div><span class="Apple-tab-span" \
style="white-space:pre">		</span></div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>} catch (IOException e) {</div><div><span \
class="Apple-tab-span" style="white-space:pre">		</span>e.printStackTrace();</div> \
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return \
null;</div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>}</div><div>}</div></div><div><br></div><div>Apologies \
for more noise in the list, I just hope someone may someday find it helpful.</div> \
<div><br></div><div>David S</div><br><div class="gmail_quote">On Wed, Nov 2, 2011 at \
11:16 AM, David A. Sampayo <span dir="ltr">&lt;<a \
href="mailto:david.a.sampayo@gmail.com">david.a.sampayo@gmail.com</a>&gt;</span> \
wrote:<br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px \
#ccc solid;padding-left:1ex;"><div>I&#39;m currently investigating that error, but \
future readers may find this useful. Will post if I find a solution.</div> <div \
class="yj6qo c4rCgd"><div id=":4v" class="EtNW5c" \
tabindex="0"></div></div></blockquote></div><br></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic