Sending encrypted URL params to PHP: How to calculate size of encrypted data?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Sending encrypted URL params to PHP: How to calculate size of encrypted data?

Stokestack
Re-sending.  Forgot to finish the subject...

Hi all.

I have an iPhone app that retrieves database info by issuing HTTP GETs to PHP pages on a server.  All I want to do is encrypt the parameters sent in the URL, to prevent people from spoofing our app and abusing our database (most likely with spam).  I've seen people ask this question in forums, and they usually get barraged with questions about why they want to do it, rather than answers.  Let me try to head a few off:

1. It's neither practical nor necessary to maintain sessions on the server.  We're not using cookies, certificates, or HTTPS.  I don't even need the returned data to be encrypted (it's just DB queries coming back as XML).
2. I can't use GnuPG because of its license.
3. I want to use a public-key mechanism because the key will be sent in the clear from DB to app; I don't want to try to hide a private key in the app itself.

As I understand it, the typical procedure is as follows:

1. Generate a random key and initialization vector to encrypt the block of text.
2. Encrypt that random key with the RSA public key.
3. Encrypt the data payload with the random key and IV, using Blowfish or other encryption.
4. Send the encrypted data payload, encrypted random key, and IV to the server for decryption.

I think I'm nearly there: I'm generating a random key and IV; I have the public key coming back from the database and being loaded with PEM_read_bio_RSA_PUBKEY().  Now I guess I need to use the EVP_encrypt functions to encrypt the payload, but how do you calculate the size of the output buffer that's required for the encrypted data?

I assume a normal next step is to add the encrypted key, IV, and encrypted payload as parameters in the HTTP GET and unravel all this using appropriate functions (and the private RSA key) in PHP on the server.  Correct?

Thanks!

Gavin
Reply | Threaded
Open this post in threaded view
|

Re: Sending encrypted URL params to PHP: How to calculate size of encrypted data?

thulasi-2
As you are planning to use symmetric encryption for payload, encrypted data length will be same as that of payload assuming your payload is properly padded.

On 17 May 2011 14:54, G S <[hidden email]> wrote:
Re-sending.  Forgot to finish the subject...

Hi all.

I have an iPhone app that retrieves database info by issuing HTTP GETs to PHP pages on a server.  All I want to do is encrypt the parameters sent in the URL, to prevent people from spoofing our app and abusing our database (most likely with spam).  I've seen people ask this question in forums, and they usually get barraged with questions about why they want to do it, rather than answers.  Let me try to head a few off:

1. It's neither practical nor necessary to maintain sessions on the server.  We're not using cookies, certificates, or HTTPS.  I don't even need the returned data to be encrypted (it's just DB queries coming back as XML).
2. I can't use GnuPG because of its license.
3. I want to use a public-key mechanism because the key will be sent in the clear from DB to app; I don't want to try to hide a private key in the app itself.

As I understand it, the typical procedure is as follows:

1. Generate a random key and initialization vector to encrypt the block of text.
2. Encrypt that random key with the RSA public key.
3. Encrypt the data payload with the random key and IV, using Blowfish or other encryption.
4. Send the encrypted data payload, encrypted random key, and IV to the server for decryption.

I think I'm nearly there: I'm generating a random key and IV; I have the public key coming back from the database and being loaded with PEM_read_bio_RSA_PUBKEY().  Now I guess I need to use the EVP_encrypt functions to encrypt the payload, but how do you calculate the size of the output buffer that's required for the encrypted data?

I assume a normal next step is to add the encrypted key, IV, and encrypted payload as parameters in the HTTP GET and unravel all this using appropriate functions (and the private RSA key) in PHP on the server.  Correct?

Thanks!

Gavin

Reply | Threaded
Open this post in threaded view
|

RE: Sending encrypted URL params to PHP: How to calculate size of encrypted data?

Dave Thompson-5
> From: [hidden email] On Behalf Of Thulasi
> Sent: Tuesday, 17 May, 2011 06:26

> As you are planning to use symmetric encryption for payload,
> encrypted data length will be same as that of payload assuming
> your payload is properly padded.

And if you let EVP do PKCS5 padding, as it does by default
for CBC (and ECB but that's rarely a good mode to use),
ciphertext is up to one block longer than plaintext. For
Blowfish or 3DES (or 1DES!) block is 8 bytes, for AES 16.

If you use a stream mode no padding is needed and ciphertext
is same length as plaintext. OpenSSL provides only OFB and CFB,
which seem to have gone out of fashion although I don't know of
any real problems. CTR is fairly popular, and you can construct
it with a little work, but not as easy as just using EVP_*.

Or you can use a stream *cipher*, but RC4 is fraying some
and no newer one has been widely accepted, so I wouldn't.
(If you do, no IV is needed, at least not with random key.)

The OP (apparently) controls both ends, and hence can choose
any reasonable mode (and cipher) even if not popular.

In either/any case, the encrypted key (and IV) and encrypted data
are all binary. To put them in a URL, you need to encode them;
base64 is provided in OpenSSL and will make everything
just over a third bigger. Percent-encoding is also standard,
and may be provided in WWW client and/or server libraries,
but will usually expand more, worst case 3 times the size.
Or you can invent your own encoding, lots of people do,
but you can't do much better on average than base64.
       
> On 17 May 2011 14:54, G S <[hidden email]> wrote:
<snip>
> 1. Generate a random key and initialization vector
> to encrypt the block of text.

With a random key you don't really need IV to be random,
but people generally do so because it fits the usual structure
better and might provide some 'belt and suspenders' redundancy.

> 2. Encrypt that random key with the RSA public key.
> 3. Encrypt the data payload with the random key and IV,
> using Blowfish or other encryption.
> 4. Send the encrypted data payload, encrypted random key,
> and IV to the server for decryption.
<snip>
> I assume a normal next step is to add the encrypted key, IV, and
> encrypted payload as parameters in the HTTP GET and unravel all this using

> appropriate functions (and the private RSA key) in PHP on the server.
Correct?

Yes. At least on the server; in PHP or otherwise I don't know.



______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Sending encrypted URL params to PHP: How to calculate size of encrypted data?

Stokestack
Thanks a lot for that thorough answer, Dave.

Today I discovered EVP_Seal and related functions (actually I discovered their counterparts in PHP and then searched for the C side).  It looks like this is exactly what I'm looking for; maybe people just assume that everyone knows about these "envelope" functions.  I suspected something like that existed, but nobody mentions it in any of the numerous forum posts and answers about scenarios like this.

It looks like EVP will take care of most of the low-level concerns, right?  I'm just looking for the easiest means of wrapping this up; the amount of security I need is really minimal.