EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Load Key from Stream

Posted: 07/12/2006 17:43:28
by Bob Hope (Basic support level)
Joined: 07/12/2006
Posts: 13

I haven't been able to get the LoadFromStream to work. I can get LoadFromFile on the TElPGPPublicKey object to work, but that isn't an option.
I have a public key text in a .NET string. I load into a MemoryStream and try to load it into a TElPGPPublicKey. I get the error:
SBPGPExceptions.EElPGPInvalidKeyException: Invalid PGP key data.
Here is the code:

UnicodeEncoding uniEncoding = new UnicodeEncoding();

MemoryStream ms = new MemoryStream();

byte[] buffer = uniEncoding.GetBytes( partnerPublicKey );

ms.Write( buffer, 0, buffer.Length );

SBPGPKeys.TElPGPPublicKey pubKey = new SBPGPKeys.TElPGPPublicKey();

pubKey.LoadFromStream( ms );

keyRing.AddPublicKey( pubKey );

Any help would be greatly appreciated. Like I said, it all works fine if I use LoadFromFile but I have to use LoadFromStream...
Posted: 07/13/2006 02:43:33
by Eugene Mayevski (Team)

After you call ms.Write, ms.Position == ms.Length. Obviously, LoadFromStream can't read 0 bytes successfully.

Sincerely yours
Eugene Mayevski
Posted: 07/13/2006 10:33:04
by Bob Hope (Basic support level)
Joined: 07/12/2006
Posts: 13

Thank you for your suggestion. I tried your code. The fix did not change the behavior.
I have been using this code with a stream in the TElPGPKeyring.Load method sometime without the Position.Length call... This code has been working with TElPGPKeyring.Load:

Assembly.GetExecutingAssembly().GetManifestResourceStream( "pubkey.asc" ), null, true );

but fails on the
pubKey.LoadFromStream( Assembly.GetExecutingAssembly().GetManifestResourceStream( "pubkey.asc" ) );

Any type of stream I try to load from gives me an error. From the Manifest, or MemoryStream, I still get "SBPGPExceptions.EElPGPInvalidKeyException: Invalid PGP key data."

I am able to take the stream, save it to a file, and then use the LoadFromFile method to load the key.

Can someone please help? I have to use the LoadFromStream method. Has anyone been able to use the LoadFromStream method? I can't find a working example in the samples or documentation.

Posted: 07/13/2006 11:14:25
by Eugene Mayevski (Team)

Now I am confused. With the key you try, does *any* method of loading work? If LoadFromFile works, then the problem is with your streams, cause LoadFromFile is a wrapper on top of LoadFromStream.

Sincerely yours
Eugene Mayevski
Posted: 07/13/2006 11:19:02
by Bob Hope (Basic support level)
Joined: 07/12/2006
Posts: 13

Yes, LoadFromFile works. But I've been successfully using Stream code on the Load method of the KeyRing object for a while.
When I tried using that same code on the SBPGPKeys.TElPGPPublicKey.LoadFromStream(), I get the error. That's why I'm having trouble. Why would the Stream work with one method of the SBPGPKeys.TElPGPKeyring.Load(), but not with SBPGPKeys.TElPGPPublicKey.LoadFromStream() when they both accept type Stream?
I'm sure it probably is with my Stream. I would just like someone to show me some example documentation or where they've been able to use the LoadFromStream themselves so I can move on with my work. Please.
Posted: 07/13/2006 11:42:15
by Bob Hope (Basic support level)
Joined: 07/12/2006
Posts: 13

Thank you for your help on this. You were in the right direction with the position of the Stream. Instead of being at the END of the stream ( by default, the Stream is positioned at the end ), you have to position at the beginning by using:

Stream.Seek( offset, SeekOrigin.Begin );



Topic viewed 5653 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS business operates as a division of /n software, inc. For more information, please read the announcement.

Got it!