0

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.

AndyD273
  • 7,177
  • 12
  • 54
  • 92
  • Might help : http://stackoverflow.com/questions/7143514/how-to-encrypt-a-large-file-in-openssl-using-public-key – CD001 Mar 17 '17 at 16:09
  • What key and cipher are you using? It almost sounds like RSA, but it could be others. – jww Mar 17 '17 at 16:17
  • @jww This is as much as I've figured out: `$keysize = 4096; $res=openssl_pkey_new(array('private_key_bits' => $keysize));` – AndyD273 Mar 17 '17 at 16:19
  • @CD001 That does look like what I need, but I'm not sure I understand it yet :) If I'm reading it right, I want to generate a RSA key, encode it with my openssl private key, encode the file with the RSA key, and then send the RSA encoded file and the encoded key to the destination, where the key will be decoded and then the file will be decoded with that key? Though in this case it won't be a file, but an arbitrarily long string. – AndyD273 Mar 17 '17 at 16:21
  • Ummm, I'll just have to nod sagely and say "probably" :) I generally use GnuPG for encryption which is a slightly different approach as you need the recipient's public key first... I can't remember the last time I used something to interface *directly* with OpenSSL beyond generating self-signed certs on Apache so I'm kinda (very) rusty... but that sounds about right. – CD001 Mar 17 '17 at 16:47
  • Nowhere in your question you say what kind of encryption you're using. I could assume you're using RSA, but it could be something else. – Artjom B. Mar 17 '17 at 18:12
  • @ArtjomB. Very good point, and one I honestly didn't understand when I first opened the question. I'm new to the world of encryption, and so that's why I'm doing this little project, to learn some stuff. I believe what I have now is in fact RSA, since that seems to be the default for openssl_pkey_new. – AndyD273 Mar 17 '17 at 18:43

1 Answers1

0

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • After further research I'm pretty sure the key I currently have is RSA, since that appears to be the default for openssl_pkey_new(). I'm still figuring this out,and so I'll do a little reading up on AES – AndyD273 Mar 17 '17 at 18:39
  • 2
    Add more to the question about what you are trying to accomplish. See updated answer. – zaph Mar 17 '17 at 18:51
  • Ok, I added my end goal, and the symmetric encryption does look like it's the way to go. I found [this page with an AES-256 example to replace mcrypt](https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcrypt-into-your-code-you-re-doing-it-wrong) but the example has this warning: `This library is unsafe because it does not MAC after encrypting`, and I don't even know what that means... I probably need to close this question and open a new one that is asking the right question. – AndyD273 Mar 17 '17 at 19:07