使用 public 密钥加密大文件

Encrypting large files with public key

我正在开发一个 shell 脚本,除其他外,它使用 public 密钥加密一些文件——打算用私人密钥解密——使用 smime命令。

smime 适用于小文件,但不适用于大文件 (>4GB)。

openssl smime -aes-256-cbc -encrypt -in INPUT_FILE_NAME -binary -outform DEM -out OUTPUT_FILE_NAME PUBLIC_PEM_FILE

此行未显示错误,输出文件已创建,但在调用完成后仍为空。

如何加密大小文件?

编辑 1:how to encrypt a large file in openssl using public key 上发现了一条具有相同问题的评论,但建议的解决方案无效。

必须通过将大文件拆分成小块来解决问题:

# 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

为了提供信息,下面是如何解密并将所有部分放在一起:

# 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

解决方案基于:

  1. http://linuxconfig.org/easy-way-to-encrypt-and-decrypt-large-files-using-openssl-and-linux
  2. https://unix.stackexchange.com/questions/1588/break-a-large-file-into-smaller-pieces#1589