EldoS | Feel safer!

Software components for data protection, secure storage and transfer

PGP Encryption

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#1799
Posted: 12/07/2006 10:07:04
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

I am loading a public key from a memory stream (streamPublic) that is fed by a a .NET string (PublicKey). I looked for similar posts, and modified my code accordingly. LoadFromStream still does not work. LoadFromFile does work. The relevant code is as follows:

UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] buffer = uniEncoding.GetBytes(PublicKey);
streamPublic.Write(buffer, 0, buffer.Length);
streamPublic.Position = streamPublic.Length;
streamPublic.Seek(0, SeekOrigin.Begin);
pgpPublicKey.LoadFromStream(streamPublic);

The last line fails with a EElPGPDearmoringStreamException: "Invalid header line"

Any ideas?

Thanks,

David[/code]
#1800
Posted: 12/07/2006 10:32:10
by Eugene Mayevski (EldoS Corp.)

You are saving the key as unicode. Please avoid using Unicode when working with text representation of various security entities (PGP keys, PEM-formatted PKI data etc.).


Sincerely yours
Eugene Mayevski
#1804
Posted: 12/07/2006 20:25:40
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

Thanks. I changed from Unicode to ASCII, and it now works, if I load the keys from a file. However I need to load the keys from a string, and am having problems. I am using the following code, which fails on the last line with an "Unexpected end of data' error.

***********************
MemoryStream streamPublic = new MemoryStream();
MemoryStream streamPrivate = new MemoryStream();

try
{
--load public key into stream
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] pubBuffer = asciiEncoding.GetBytes(PublicKeyString);
streamPublic.Write(pubBuffer, 0, pubBuffer.Length);
streamPublic.Seek(0, SeekOrigin.Begin);

-- load private key into stream
byte[] privBuffer = asciiEncoding.GetBytes(PrivateKeyString);
streamPrivate.Write(privBuffer, 0, privBuffer.Length);
streamPrivate.Seek(0, SeekOrigin.Begin);

-- load streams into keyring (error generated)
myKeyRing.Load(streamPublic, streamPrivate, true);

*******************

Note that the string variables PublicKeyString and PrivateKeyString include the entire content of the two files listed below, and if I substitute the load statement above with what is below, all is fine:

myKeyRing.Load("c:\\pgp\\keys\\pub\\ahip_bvs.asc", "c:\\pgp\\keys\\priv\\aiexport.asc",true);

Any ideas?
#1805
Posted: 12/07/2006 23:24:24
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

Corrected problem. Key string was incorrect. One followup question: is there anything special to keep in mind when using ONLY streams, rather than files, to provide all the inputs and outputs (both keys and data)? I am seeing some inconsistent results when moving data from an unencrypted file to a stream, to an encrypted stream, to a string, to a stream, and finally to an unencrypted file again. The end doesn't match the beginning, and it should.

Thanks,

David
#1806
Posted: 12/08/2006 02:07:02
by Eugene Mayevski (EldoS Corp.)

First of all, remember about correct conversion between string and streams. Strings are unicode (UTF-16) in .NET, while data encryption operates with bytes.

Next thing is that stream position must be reset between operations. If you write the data to the stream and then pass the stream to pgpreader/pgpwriter without resetting stream position to 0, the reader/writer won't know what data to process.


Sincerely yours
Eugene Mayevski
#1824
Posted: 12/11/2006 08:32:28
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

I understand that I should use ASCIIEncoding if the encryption is text-based. What if it is binary?
#1826
Posted: 12/11/2006 08:44:26
by Eugene Mayevski (EldoS Corp.)

Quote
David Ridenour wrote:
What if it is binary?


If it is binary, you don't deal with strings.


Sincerely yours
Eugene Mayevski
#1827
Posted: 12/11/2006 09:06:31
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

Actually, I am, and that may be a problem. I am being passed the contents of an encrypted file as a string, but if the encryption is binary, I need handle it accordingly. How would I extract a binary stream, and also, if another message came in as text-based encryption, is it possible to tell the difference programatically?
#1828
Posted: 12/11/2006 09:30:11
by Eugene Mayevski (EldoS Corp.)

The most possible result of such passing is the corrupted data. So you should change the way the data is passed.

I have no idea, what the data can be and become, if somebody puts them to the UTF-16 string. So I can say nothing about how to correctly extract them from the string.


Sincerely yours
Eugene Mayevski
#1854
Posted: 12/13/2006 19:50:22
by David Ridenour (Basic support level)
Joined: 12/07/2006
Posts: 12

Corrected problem, which was due to using streams incorrectly. Still have issues, though.

I am seeing two problems with different partners using different command-line PGP utilities (including McAfee) to decrypt a file I encrypt using BlackBox: first, a "for your eyes only" message comes up. Is there a way of removing this? The second is that the content of the file appears on the console, but no additional decrypted file is created, and the partners insist that the additional file should be created automatically (not a command-line setting they are missing). These people handle PGP files all day long, so I don't think they are missing anything obvious. Any ideas?

Thanks,

David
Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.

Reply

Statistics

Topic viewed 9096 times

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




|

Back to top

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

Got it!