Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt file with public key using openssl EVP routines

I'm trying to encrypt a file using public key crypto with OpenSSL. I know enough to know that for a file of any reasonable size, this can't/shouldn't be done with public key. Instead:

1) Generate random key (e.g. 256 bit)

2) Encrypt key with public key

3) Symmetric encrypt file with random key from (1) e.g AES

4) Join (2) and (3) together

5) Send (4) to wherever/whoever

This process is replicated in OpenSSL's EVP routines as "envelope encryption". I've managed to implement a decryption/encryption program using this API in C, which works fine :)

I'm wondering if you can do this process directly through OpenSSL commands rather than through the low-level API. I've tried and can generate the random key, encrypt the file etc. I then get stuck when joining the two files together - at the moment I just cat the two together but this doesn't seem to work. Is this possible?

like image 819
user4005750 Avatar asked Nov 26 '25 15:11

user4005750


1 Answers

Well, you can define your own container format, but it is probably easier to just use CMS which handles that use case just fine.

like image 89
Maarten Bodewes Avatar answered Nov 28 '25 17:11

Maarten Bodewes