EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Decrypt Simple String

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#8255
Posted: 11/24/2008 15:05:24
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

After searching the form, I found plenty of examples on encrypting a simple string. What I don't see is how to I decrypt the same string. I am using the following code to encrypt and decrypt. I am getting an exception: EElPGPInvalidDataException in my code. Here is the code:

Code
private string PGPEncrypt(string unencryptedString)
        {
            string retVal;
            const int BUFFER_SIZE = 1024;

            Stream inStream = new MemoryStream(Encoding.ASCII.GetBytes(unencryptedString));
            Stream outStream = new MemoryStream();

            TElPGPWriter writer = new TElPGPWriter();
            writer.Armor = false;
            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[BUFFER_SIZE];
            int nBytesReceived = outStream.Read(bytes, 0, BUFFER_SIZE);
            retVal = Encoding.ASCII.GetString(bytes, 0, nBytesReceived);
            return retVal;
        }

        private string PGPDecrypt(string encryptedString)
        {
            string retVal;
            const int BUFFER_SIZE = 1024;

            Stream inStream = new MemoryStream(Encoding.ASCII.GetBytes(encryptedString));
            Stream outStream = new MemoryStream();

         TElPGPReader reader = new TElPGPReader();
            
            reader.DecryptingKeys = secretKey;
            reader.Passphrase = "secretpassphrase";
         reader.VerifyingKeys = publicKey;
            reader.OutputStream = outStream;
            reader.DecryptAndVerify(inStream, 0);

            outStream.Position = 0;
            byte[] bytes = new byte[BUFFER_SIZE];
            int nBytesReceived = outStream.Read(bytes, 0, BUFFER_SIZE);
            retVal = Encoding.ASCII.GetString(bytes, 0, nBytesReceived);
            return retVal;
        }




#8256
Posted: 11/24/2008 15:14:24
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Have you tried to decrypt this string with our sample application (PGPFilesDemo)?
#8257
Posted: 11/24/2008 15:22:09
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

Nope. I was using the ASP .NET example since that is where I need the function. I will look over the PGPFilesDemo.
#8258
Posted: 11/24/2008 15:31:25
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Also, while at first glance your code is looking ok, please check if the encryption result is not going over BUFFER_SIZE.
#8259
Posted: 11/24/2008 15:43:26
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

I must be missing something. I turned the Armor on during the encryption and have removed the KeyPassPhrase. I am still getting the error "Unexpected End Of Data" in the DecryptAndVerify.

My code is simple...

string sEncreturn = PGPEncryptString("michael");
string sDereturn = PGPDecryptString(sEncreturn);

The encrypted string looks good with the "Begin and End" markers. I reviewed the PGPFiles and cannot see what I am missing. I appear to be doing everything.
#8260
Posted: 11/24/2008 15:55:44
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

I have it working, but I don't understand. I have a form with three text boxes and an Encrypt and Decrypt button. I type something in the encrypt textbox and it's encrypted value shows up in the decrypt textbox. Then I decrypt the decrypt textbox and the result shows me my original value. However, the Encrypt button always works if I decrypt it at the same time. The Decrypt button never works and gives an error.

here is the code.

Code
Encrypt Button
string sReturnString = PGPEncrypt(txtEncrypt.Text);
txtDecrypt.Text = sReturnString;
txtResult.Text = PGPDecrypt(txtDecrypt.Text);

Decrypt Button

string sData = txtDecrypt.Text;
string sReturnString = PGPDecrypt(sData);
#8261
Posted: 11/24/2008 15:56:40
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

BTW, I had to put back the KeyPassPhrase value before the above example worked. I don't understand why the code under the decrypt button does not work.
#8262
Posted: 11/24/2008 16:04:27
by Michael Landy (Basic support level)
Joined: 11/24/2008
Posts: 6

I have figured it out! The "Decrypt" textbox on the page was not setup with multiple lines, so the data written to the control was modified when it was rendered on the screen. Where as, in the Encrypt code, it had not been rendered so the data was not modified.

Once I changed the textbox "textmode" to "multiple lines", then everything worked!

Thanks for your help.
#8263
Posted: 11/24/2008 16:13:22
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Okay, glad that your problem is solved.
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

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