How to encrypt a large file in openssl using public key

后端 未结 8 1579
梦谈多话
梦谈多话 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 05:49

    To safely encrypt large files (>600MB) with openssl smime you'll have to split each file into small chunks:

    # Splits large file into 500MB pieces
    split -b 500M -d -a 4 INPUT_FILE_NAME input.part.
    
    # Encrypts each piece
    find -maxdepth 1 -type f -name 'input.part.*' | sort | xargs -I % openssl smime -encrypt -binary -aes-256-cbc -in % -out %.enc -outform DER PUBLIC_PEM_FILE
    

    For the sake of information, here is how to decrypt and put all pieces together:

    # Decrypts each piece
    find -maxdepth 1 -type f -name 'input.part.*.enc' | sort | xargs -I % openssl smime -decrypt -in % -binary -inform DEM -inkey PRIVATE_PEM_FILE -out %.dec
    
    # Puts all together again
    find -maxdepth 1 -type f -name 'input.part.*.dec' | sort | xargs cat > RESTORED_FILE_NAME
    

提交回复
热议问题