如何在 Ruby 中创建具有以下约束的加密文件?
- 数据是由不受信任的用户提供的字节数组。
- 密码是由不受信任的用户提供的字节数组。
- 密码接受所有常用密码字符,包括 password special characters (例如“、”等)。
- 文件格式应为常用格式,例如 OpenPGP。
- 加密文件被发送回用户。
注意:问题在误解后得到澄清。
最佳答案
如果您安装了 GPG,那么有一个快速、简单、可靠的方法:
open("| gpg [options]","w"){|f| f.syswrite(data) }
示例:
require 'shellwords'
data = "Hello World"
password = "letmein"
gpg = "/usr/local/bin/gpg \
--symmetric \
--cipher-algo aes256 \
--digest-algo sha256 \
--cert-digest-algo sha256 \
--batch --yes \
--passphrase #{password.shellescape} \
--output /tmp/out.gpg
"
open("| #{gpg}","w"){|f| f.syswrite(data) }
一般来说,使用系统内置的 GPG 比尝试管理自己的加密货币更安全。
关于ruby - 如何使用 Ruby 使用对称 AES256 加密文件并可以使用 gpg 解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593591/