EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Simple String Encryption

Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.
#5739
Posted: 04/04/2008 16:13:00
by Joe King (Standard support level)
Joined: 04/04/2008
Posts: 16

I have searched and searched and cannot find any information on using these libraries to encrypt text in a string using C#. I tried modifying the sample code that encrypts a file, but my output does not look like it is using the lower ASCII character set (my output has box characters in it which does not look right at all). Can anyone tell me what the easiest way to encrypt a string is using these libraries?
#5742
Posted: 04/05/2008 01:35:25
by Ken Ivanov (EldoS Corp.)

There are a lot of ways to encrypt a string. Please provide the exact requirements you need to be conformant to. Do you need to use public key or symmetric encryption? What kind of encryption key should be used?
#5744
Posted: 04/07/2008 02:37:06
by Joe King (Standard support level)
Joined: 04/04/2008
Posts: 16

Quote
Innokentiy Ivanov wrote:
There are a lot of ways to encrypt a string. Please provide the exact requirements you need to be conformant to. Do you need to use public key or symmetric encryption? What kind of encryption key should be used?


I am using a public key from a file generated by PGP Desktop. I do not know the difference between symmetric and asymmetric encryption.
#5746
Posted: 04/07/2008 03:43:39
by Ken Ivanov (EldoS Corp.)

Please consider using TElPGPWriter class (the PGPFiles sample shows how exactly it should be used). As TElPGPWriter works with streams and not with strings, please use the following steps to encrypt the string:
1. Put your string to a memory stream (remember to seek the pointer to the beginning of the stream).
2. Encrypt the stream using TElPGPWriter.Encrypt() method to the destination memory stream.
3. Extract the encrypted data from the memory stream and put it to a string object.
#5776
Posted: 04/08/2008 12:53:36
by Joe King (Standard support level)
Joined: 04/04/2008
Posts: 16

Quote
Innokentiy Ivanov wrote:
Please consider using TElPGPWriter class (the PGPFiles sample shows how exactly it should be used).


That's exactly what I did and it does not appear to be producing correct results. What am I missing here(?):

Code
private string PGPEncrypt(string unencryptedString)
{
   string retVal;
   SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString("0645...589D"));
   
   TElPGPKeyring keyring = new TElPGPKeyring();
   TElPGPKeyring publicKey = new TElPGPKeyring();
   
   string path = Request.PhysicalApplicationPath;
   keyring.Load(path + "pubring.pkr", null, true);
   publicKey.AddPublicKey(keyring.get_PublicKeys(0));
   
   Stream inStream = new MemoryStream(Encoding.ASCII.GetBytes(unencryptedString));
   Stream outStream = new MemoryStream();
   
   TElPGPWriter writer = new TElPGPWriter();
   writer.EncryptingKeys = publicKey;
   writer.UseNewFeatures = false;
   writer.UseOldPackets = true;
   writer.EncryptionType = SBPGP.TSBPGPEncryptionType.etPublicKey;
   writer.Timestamp = DateTime.Now;
   writer.Encrypt(inStream, outStream, 0);
           
   outStream.Position = 0;
   TextReader sr = new StreamReader(outStream);
   retVal = sr.ReadToEnd();
   sr.Close();

   return retVal;
   }
#5777
Posted: 04/08/2008 12:59:19
by Eugene Mayevski (EldoS Corp.)

Quote
Joe King wrote:
it does not appear to be producing correct results


1) Get rid of StreamReader class. Use Stream and it's descendants.
2) "it doesn't work" is not something we can help with. Please describe what you expect to get and what you are getting instead. But first of all, remove StreamReader.


Sincerely yours
Eugene Mayevski
#5779
Posted: 04/08/2008 17:01:39
by Joe King (Standard support level)
Joined: 04/04/2008
Posts: 16

Quote
Eugene Mayevski wrote:

1) Get rid of StreamReader class. Use Stream and it's descendants.
2) "it doesn't work" is not something we can help with. Please describe what you expect to get and what you are getting instead. But first of all, remove StreamReader.


1) Well the logical choice was StreamReader after which I then tried the following (with results that are still suspect):

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


So rather than me continuing to make the wrong choice as to how to read the stream, could you suggest some code that you know will work that uses a descendent of the Stream class?

2) From my initial post:
Quote
...my output does not look like it is using the lower ASCII character set (my output has box characters in it which does not look right at all).
#5781
Posted: 04/09/2008 01:20:07
by Ken Ivanov (EldoS Corp.)

ASCII encoding is a problem. As PGP messages are binary, ASCII encoding corrupts them.

Please either avoid using ASCII encoding or set TElPGPWriter.Armor property to true to make TElPGPWriter create base64-enveloped PGP messages.
#5903
Posted: 04/16/2008 21:12:44
by Joe King (Standard support level)
Joined: 04/04/2008
Posts: 16

Quote
Innokentiy Ivanov wrote:
ASCII encoding is a problem. As PGP messages are binary, ASCII encoding corrupts them.

Please either avoid using ASCII encoding or set TElPGPWriter.Armor property to true to make TElPGPWriter create base64-enveloped PGP messages.


Thank you: Armor did the trick. I can now successfully encrypt.

I do have another question along the same topic/code, though. I need to modify the above function so that I am encrypting using the secret key and the vendor will decrypt using the public key that I have sent to them. If this is the case, I assume I need to load both my public and secret keys in my TElPGPKeyring object and then set the EncryptionType property of my TElPGPWriter object to SBPGP.TSBPGPEncryptionType.etBoth, correct? Thus I am left with the following:

Code
        private string PGPEncrypt(string unencryptedString)
        {
            string retVal;
            SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString("0645...589D"));

            TElPGPKeyring keyring = new TElPGPKeyring();

            string path = Request.PhysicalApplicationPath + FILE_PATH_KEY;
            keyring.Load(path + "pubring.pkr", path + "secring.skr", true);

            Stream inStream = new MemoryStream(Encoding.ASCII.GetBytes(unencryptedString));
            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[BUFFER_SIZE];
            int nBytesReceived = outStream.Read(bytes, 0, BUFFER_SIZE);
            retVal = Encoding.ASCII.GetString(bytes, 0, nBytesReceived);
            return retVal;
        }

The only problem I have with this is when I try to decrypt the data myself it requires a secret key which (obviously) the client will not have.

UPDATE: Okay, doing some poking around it looks like I am having the same issue as is described in [URL=http://www.eldos.com/forum/read.php?FID=7&TID=436&MID=2275&phrase_id=262312#message2275]this thread[/URL]. It appears I am trying to digitally sign a message in which case the above code is useless. I am doing some more reading, but any additional guidance would be great.
#5916
Posted: 04/17/2008 06:56:48
by Ken Ivanov (EldoS Corp.)

Quote
I need to modify the above function so that I am encrypting using the secret key and the vendor will decrypt using the public key that I have sent to them.

It is not possible due to the nature of public key cryptography. Encryption is always done using a public key, and decryption is always done using a corresponding secret key.

Please consider using passphrase-based encryption, probably it is the functionality you need.
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

Topic viewed 17017 times

Number of guests: 2, 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!