EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Invalid Data - TElPGPReader: DecryptAndVerify (.NET)

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#7369
Posted: 08/20/2008 06:37:33
by Brian Ryther (Basic support level)
Joined: 08/19/2008
Posts: 5

Good Morning,

I am currently receiving an exception while trying to decrypt as String using the DecryptAndVerify method of the TElPGPReader class. The exception simply states "Invalid Data" and from the stack trace I am receiving, appears to be occuring when the component is attempting to write to the output stream. I have not seen any threads regarding this issue. I really appreciate any help with this issue.

Thanks in advance.
Brian


Some additional info:

Encrypted String:hQEOA5MEbgK0tqddEAP/eNn66TTmogxsJWTBkTcWtC0ne2sxP3DhIqIfoM8b0RWEXb7mJFGWWFs5Cvim+uYGChrnJRFnkB4H1O9nL2zGcqkFb0o4b14HvtN2hIFnYoiAzOWz0G+sJS0seC8ShPjd5T9iykCHoCMnOY8SNIad097CK0F4E2N/soJJSpcs16ID/1meOHN+vVcMGRic0EBj7M18SedNnIT1u1VPKNBCEMLqmL+Vg6MD7EAsVwiIYtOcbT3LCKUelKKAxhN2BPyR7UhDN6jEG/fra7hrW6hegQk0NUf1HT3a/a7vNpafoEqtxBF7ftuigucKmfxqZqWg/szCuIFK4hrTNFm6J0boBWawpgAAACKwKOhF5veMJ36l/SarL/PXEO5l6iduhtRwb5sKz0M6Orxm=Xzj2

Exception Message:
An unhandled exception of type 'System.Exception' occurred in mscorlib.dll
Additional information: Invalid data

Stack Trace:
at SBPGP.TElPGPProcessingUnit.CreateProcessingStream()
at SBPGP.TElPGPProcessingUnit.Write(Byte[] Buffer, Int32 Offset, Int32 Count)
at SBPGP.TElPGPReader.Synchronize()
at SBPGP.TElPGPReader.DecryptAndVerify(Stream ASourceStream, Int64 Count)
at xxx.Functional.Common.CommonUtility.DecryptString(String stringToDecrypt)

Code:
Code
      public string DecryptString(String stringToDecrypt)
      {

         try
         {
            SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString("......."));

            SBPGPKeys.TElPGPKeyring keyring = new SBPGPKeys.TElPGPKeyring();
            keyring.ArmorBoundary = "";
            keyring.SaveSecretKeySignatures = false;
            keyring.WriteTrust = true;


            Stream inStream = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(stringToDecrypt));

            SBPGP.TElPGPReader pgpReader = new SBPGP.TElPGPReader();
            pgpReader.OnCreateOutputStream += new SBPGP.TSBPGPCreateOutputStreamEvent(pgpReader_OnCreateOutputStream);


            //load the keys
            keyring.Load(GetRegistryValue("SecureBlackBox", "PublicKeyFile"), GetRegistryValue("SecureBlackBox", "SecretKeyFile"), true);

            pgpReader.DecryptingKeys = keyring;
            pgpReader.VerifyingKeys = keyring;

            pgpReader.DecryptAndVerify(inStream, inStream.Length);

            outStream.Position = 0;
            byte[] bytes = new byte[outStream.Length];
            int nBytesReceived = outStream.Read(bytes, 0, (int)outStream.Length);
            return System.Text.Encoding.ASCII.GetString(bytes, 0, nBytesReceived);
         }
         catch (Exception e)
         {
            throw e;
         }
      }

      private void pgpReader_OnCreateOutputStream(object Sender, string Filename, System.DateTime TimeStamp, ref System.IO.Stream Stream, ref bool FreeOnExit)
      {
         Stream = new MemoryStream();            
         outStream = Stream;
         FreeOnExit = true;
      }
#7370
Posted: 08/20/2008 07:06:49
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Your string isn't valid OpenPGP encrypted data.
It is Base64-encoded ('armored'), but without armor headers (like ----- BEGIN PGP MESSAGE-----).
#7371
Posted: 08/20/2008 07:55:07
by Brian Ryther (Basic support level)
Joined: 08/19/2008
Posts: 5

Thanks for your reply.

So when decrypting the string I need to include the header and footer information? I must admit I am not entirely clear on the use of the Armor property when encrypting (and consequently decrypting) the string. I received the "armor" solution from another thread, in an attempt to use the APIs to Decrypt/Encrypt without the use of file IO.

Can you please direct me to some documentation explaining the use of the Armor property? Is this the best manner for encrypting/decrypting strings w/o the use of file IO?

Thanks again.
#7372
Posted: 08/20/2008 08:12:29
by Brian Ryther (Basic support level)
Joined: 08/19/2008
Posts: 5

I tried passing the entire armored message and am now receiving an exception incidating "Invalid header line". I have attached the entire string being passed.

Thanks again!

-----BEGIN PGP ENCRYPTED MESSAGE-----\n\nhQE...VK6j\nAaGEjgEDFzDibEwlTGp5Z98W9eafnzIBwS5HzijhSq8oa6qmuYC71p7PK/DEZZBR\nERYqTT+UkGRuiTI8GLAV+xHEOTfEmKv3h6vbVutqS++OBlqWH8VO/xbInMb30sID\n/Rufu3x+uKVwvBm34GRMXLLgD07vmBZsClgmjGsjNNiJGkRf3drLci22TTKVYT8r\n9/7SqLEmvJ9T264q9wzfguOkZX4xTexWG0VcHW1pUpdQBXBict0d9yW8YnE1Ym19\nxXxj3Lf2YIbWei5KmO06tfLsdOBsof83zHMmWcRlaygtpgAAACI/qFWpCs3ChTui\nKFaL4tQFECvp2yd6q1FhQR6re2phscxf\n=+dmF\n-----END PGP ENCRYPTED MESSAGE-----
#7374
Posted: 08/20/2008 08:39:51
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Armor is used to transfer messages in nearly-readable format via transports, which don't like binary data (like e-mail, etc). Also, looking at armored message you could see what is it.
It is the same binary data, but base64-encoded, and with headers.
Could you paste your message without carriage returns?
It seems, that it is not well-formed - some lines are 64-characters long, and some 65.
#7375
Posted: 08/20/2008 08:41:44
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Mm, I meant without \n.
Also, do you have success on decrypting this message with any other software?
And which one is generating this message?
#7376
Posted: 08/20/2008 08:52:41
by Brian Ryther (Basic support level)
Joined: 08/19/2008
Posts: 5

Mykola,

The message is being encrypted using the TElPGPWriter class (code below). This is the only software being used for this process.

As for the \n characters .. do I need to remove them from the data being sent to the Decrypt method as well?



Code
      public string EncryptString(String stringToEncrypt)
      {
         WriteToLog("EncryptString", "Entering Method");
         WriteToLog("EncryptString", "String to Encrypt: " + stringToEncrypt);

         // setting a license key
         SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString("....."));
         try
         {
            //Load the keys
            SBPGPKeys.TElPGPKeyring keyring = new SBPGPKeys.TElPGPKeyring();
            keyring.ArmorBoundary = "";
            keyring.SaveSecretKeySignatures = false;
            keyring.WriteTrust = true;

            keyring.Load(GetRegistryValue("SecureBlackBox", "PublicKeyFile"), GetRegistryValue("SecureBlackBox", "SecretKeyFile"), true);

      
            Stream inStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(stringToEncrypt));
            Stream outStream = new MemoryStream();

            TElPGPWriter writer = new TElPGPWriter();
            writer.Armor = true;
            writer.EncryptingKeys = keyring;
            writer.UseNewFeatures = false;
            writer.UseOldPackets = true;
            writer.EncryptionType = SBPGP.TSBPGPEncryptionType.etBoth;
            writer.Timestamp = DateTime.Now;
            writer.Encrypt(inStream, outStream, 0);

            outStream.Position = 0;
            byte[] bytes = new byte[outStream.Length];
            int nBytesReceived = outStream.Read(bytes, 0, (int)outStream.Length);
            return System.Text.Encoding.ASCII.GetString(bytes, 0, nBytesReceived);

         }
         catch(Exception ex)
         {
            logger.Write(CLASSNAME, "EncryptString", "Exception: " + ex.ToString());
            return null;
         }
      }
#7377
Posted: 08/20/2008 09:11:45
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Now I see, you are using SBB for encryption.
Please take a look at code of our demos, they will show you how to implement encryption/decryption in a right way (Primitives/EncryptKey, DecryptKey).
In your code you are not setting ArmoreHeaders & ArmorBoundary properties.
#7390
Posted: 08/21/2008 09:35:34
by Brian Ryther (Basic support level)
Joined: 08/19/2008
Posts: 5

Thanks for your help. Following your suggestions, I have now gotten the encryption to work. I implemented the following ArmorHeaders and ArmorBoundary, this was taken directly from the sample code in the Blackbox installation:

Code
            writer.ArmorHeaders.Clear();
            writer.ArmorHeaders.Add("Version: EldoS OpenPGPBlackbox");
            writer.ArmorBoundary = "PGP MESSAGE";


I do have a followup question though. Enabling these properties did add some additional header text to the encrypted message, but did not alter the message itself. What is special about implementing these specific header values that allows the decryption to work? Additionally, is there a way to generate a string representation of the encrypted string without using the header and footer information?

Thanks again for your help!
#7399
Posted: 08/21/2008 10:38:49
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

The meaning of these headers is described in RFC4880. They just should be present for armored PGP message, to show that this is an armored PGP message.
If you want simple string representation, you should encrypt with armor = false, but after encryption Base64-encode your encryption result, using method SBUtils.Base64Encode. And to decrypt such message, you should first SBUtils.Base64Decode it, and only after that pass to ElPGPReader.
Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages

Reply

Statistics

Topic viewed 8087 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!