1

In the scenario of a hybrid encryption for which I use RSA (asymmetric) to encrypt the encryption key used with AES (symmetric) to encrypt my message, I need to store the encrypted key together with the cipher text and all in the same column of the DB in order to then be able to request this value pair and perform the decryption.

What are the best practices for composing this pair: Simply an encrypted key + cipher text with an identifiable separator? Or something else?

5
  • What is the size of the data? Why do you need RSA?
    – kelalaka
    Feb 23, 2021 at 17:16
  • @kelalaka Raw plaintext string is string(4000). I would like to use Hybride Encryption and avoid managing the AES key myself, I understood that it could be a complex process and I dont want to consider it again. With RSA encryption of the random AES key and storing of the key with the cypher text, it fits perfectly to my use case, by managing only the RSA certificate to my clients. My question is about how to store Encrypted Key and Cipher text with a concrete implementation example, if someone can guide me :)
    – Marc Alves
    Feb 23, 2021 at 17:21
  • Use ECC based solution? Shorter keys. see libsodium cryptobox.
    – kelalaka
    Feb 23, 2021 at 17:25
  • @kelalaka libsodium cryptobox implementation exists with C# .Net Core ?
    – Marc Alves
    Feb 23, 2021 at 17:33
  • github.com/adamcaudill/libsodium-net
    – kelalaka
    Feb 23, 2021 at 17:34

2 Answers 2

1

Email encryption has faced the same problem and come up with two solutions.

  1. You might want to look into PKCS#7 aka the Cryptographic Message Syntax (CMS). It allows for storage of not only the encrypted AES key but also certificate information (issuer and serial number) to identify which public key was used for encryption.
  2. Alternatively, PGP has a very similar system with its own message syntax. PGP also stores a key identifier to tell the decryptor which public key was used for encryption.

Both formats can be stored as BLOBs or CLOBs to work around database column size restrictions. Both formats also support message signatures, should you need that, too.

4
  • Thx for your quick answer. The problem is that in my case I have to store it (encrypted key + AES cipher text) as a string only. I dont have any other options. This why I ask if I build something simple like string('EncryptedKey'+'Separator'+'CipherText') is it ok or not ? Then I will simply get the string from the DB and split on the separator, decrypt the key and then decrypt the cipher... Thx
    – Marc Alves
    Feb 23, 2021 at 11:29
  • In cryptography you generally don't want to invent your own schemes, and the CLOB variants of CMS and PGP are strings. But are your table columns size-limited? Then you might have to resort to something more compact than CMS. But i recommend to include some key identifier in the storage, for example a SHA-256 hash of the public key, so you can tell which key to use for decryption.
    – wallenborn
    Feb 23, 2021 at 13:06
  • I can handle table columns size. I totally undestand that I dont have to invent my own algo, but to be honnest here, I dont really understand how to implement PGP or CMS, ... My code is ok for dealing with RSA certificate keys, generate a random AES key and IV each time I encrypt, Encrypt these keys with RSA and then encrypt the message itself with AES. But I dont understand how to store the entire information into the same string (as you said that outputs could be a string...). I'm working with C# .Net Core, if by chance someone have any example or link to provide it will be wonderful ! Thx
    – Marc Alves
    Feb 23, 2021 at 16:10
  • You might want to look into the C# version of the bouncycastle crypto library. You can get the source code at bouncycastle.org/csharp/index.html and it contains both a CMS and an OPENPGP package.
    – wallenborn
    Feb 24, 2021 at 13:56
0

If the encrypted key has a fixed length, you don't need a separator, just stick them together. If the encrypted key has a variable length, things get a bit more tricky. Using a separator is problematic, since both the encrypted key and cipher text will appear random and may very well contain the separator by chance. You could use something long and random like an UUID as a separator to make this unlikely. But I would probably just put the length of the encrypted key first, then the encrypted key, and then the cipher text, without any separators.

It might be easier to just use two different columns, though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .