Encrypt Large File with public key Via OpenSSL

You can use “rsautl” to encrypt and decrypt small message files but when it comes to large files it’ll shows the error as bellow
Error:
RSA operation error
6408:error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for
key size:./crypto/rsa/rsa_pk1.c:151:

So it’s better to use SMIME to perform large file Encryption.
Generate private and public key pairs:
openssl req -x509 -nodes -days 1000 -newkey rsa:1024 -keyout pri.pem -out pub.pem

Encrypt Large File:
openssl smime -encrypt -aes256 -in w.txt -binary -outform DEM -out w.ssl pub.pem

Decrypt Large File:
openssl smime -decrypt -in w.ssl -binary -inform DEM -inkey pri.pem -out w.txt

Here,
w.txt is the any Large file
w.ssl is the Encrypted File
pri.pem, pub.pem is the generated private-key and public-key respectively

Thanks

Leave a Comment