How to encrypt a large file in openssl using public key

后端 未结 8 1574
梦谈多话
梦谈多话 2020-12-04 05:27

How can I encrypt a large file with a public key so that no one other than who has the private key be able to decrypt it?

I can make RSA public and private keys but

8条回答
  •  庸人自扰
    2020-12-04 06:03

    Solution for safe and high secured encode anyone file in OpenSSL and command-line:

    You should have ready some X.509 certificate for encrypt files in PEM format.

    Encrypt file:

    openssl smime -encrypt -binary -aes-256-cbc -in plainfile.zip -out encrypted.zip.enc -outform DER yourSslCertificate.pem
    

    What is what:

    • smime - ssl command for S/MIME utility (smime(1))
    • -encrypt - chosen method for file process
    • -binary - use safe file process. Normally the input message is converted to "canonical" format as required by the S/MIME specification, this switch disable it. It is necessary for all binary files (like a images, sounds, ZIP archives).
    • -aes-256-cbc - chosen cipher AES in 256 bit for encryption (strong). If not specified 40 bit RC2 is used (very weak). (Supported ciphers)
    • -in plainfile.zip - input file name
    • -out encrypted.zip.enc - output file name
    • -outform DER - encode output file as binary. If is not specified, file is encoded by base64 and file size will be increased by 30%.
    • yourSslCertificate.pem - file name of your certificate's. That should be in PEM format.

    That command can very effectively a strongly encrypt big files regardless of its format.
    Known issue: Something wrong happens when you try encrypt huge file (>600MB). No error thrown, but encrypted file will be corrupted. Always verify each file! (or use PGP - that has bigger support for files encryption with public key)

    Decrypt file:

    openssl smime -decrypt -binary -in encrypted.zip.enc -inform DER -out decrypted.zip -inkey private.key -passin pass:your_password
    

    What is what:

    • -inform DER - same as -outform above
    • -inkey private.key - file name of your private key. That should be in PEM format and can be encrypted by password.
    • -passin pass:your_password - your password for private key encrypt. (passphrase arguments)

提交回复
热议问题