Search code examples
phpencryptionphp-openssl

How big of a key do I need?


I'm learning encryption. I made some keys, encoded a sample string ("1234 5678 9012 3456") and was able to decode it no problem. The first part is working fine.

Then I tried it with a sample of the real data that I want to encode, and got a "data to large for key size" error.

The string length is 14041 characters, and the final product may be larger still, so how do I make it work without having a 1024000 bit key or something crazy like that?

Edit: As I've already learned some stuff from when this was opened a few hours ago, the current method I've been playing with uses RSA, since that is the default for openssl_pkey_new.

Basically, I have a mobile app and I want to be able to update the in app data in the most secure way possible. My boss is a bit paranoid about "the cloud". My idea is to use RSA or something to validate the app with a web API page, get the most up to date data from the database that has been encoded, and the decode it in the app for storage in order to avoid having to push out a new version each time the apps data is updated (couple times a year).
My boss really doesn't like the thought of it leaking data, so any thing I can do to lock it down would be good.


Solution

  • Unless a public/private key pair are needed RSA (asymmetric) encryption is not needed. Asymmetric data size is limited to less than the key length and it is very slow.

    Generally a symmetric encryption algorithm such as AES is used for data.

    A string of length of 14041 bytes encrypted with RSA would require a key size of over 112K-bits (unrealistic/impossible), an AES key would generally be 128 or 256 bits.

    If RSA is required the strategy is to use hybrid encryption where the data is encrypted with AES and a generated random key and then the key is encrypted with RSA.