Take the 2-minute tour ×

I have seen many examples of how to use AES through OpenSSL with symmetric encryption. But I am using asymmetric encryption. My webapp is storing some sensitive data encrypted with a public key, which is later only decrypted with the private key if the admin can provide the private key passphrase.

My implementation is working well, however, I am noticing very small inputs produce rather large encrypted results. For example, a 16 character input becomes 288 bytes encrypted (it decrypts to the original 16 characters). Since these encrypted results are stored in a database table, I would like to achieve encrypted values that are much closer to the input size. Predictable storage lengths is desired, too.

I am using RSA public/private keys, but what cipher does PHP's OpenSSL implementation use by default? I haven't found a single example of how to specify a cipher when using public/private keys, and the openssl_public_encrypt function doesn't allow one of the cipher constants to be specified. Am I supposed to be specifying the cipher somehow when the pub/pri keys are generated (using openssl_pkey_new)?

share|improve this question

2 Answers 2

AES is a symmetric encryption algorithm, it doesn't support public/private key pairs. Whoever can encrypt can also decrypt the ciphertext.

One can combine AES with an asymmetric algorithm like RSA to obtain a hybrid scheme: One uses RSA to encrypt a (random) AES key, and then uses AES to encrypt the actual data. You will still have the RSA overhead of a minimum ciphertext size of about the size of the RSA modulus (since this is RSA's output), though.

I suppose this is actually what your OpenSSL function does, maybe depending on the key type.

There are other public-key schemes, specially those based on elliptic curves, which allow a smaller size overhead for similar security as RSA. (I don't know if these are supported by OpenSSL or its PHP bindings, though.)

share|improve this answer

I would recommend creating RSA private and public keys. See This great article

share|improve this answer
I realise not quite what you're asking for but in the article it shows you how to choose cypher and key-lengths which in turn affect min length of your encrypted content. –  robwithhair Aug 26 at 13:50

Your Answer


