我想使用AES-256加密一个文件。如何快速简便地完成这个操作,以及如何解密它 - 或者让其他人解密它?
stefano:~$ openssl aes-256-cbc -in attack-plan.txt -a
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
U2FsdGVkX192dXI7yHGs/4Ed+xEC3ejXFINKO6Hufnc=
aes-128-cbc ← this is okay
aes-128-ecb
aes-192-cbc
aes-192-ecb
aes-256-cbc ← this is recommended
aes-256-ecb
请注意:
OpenSSL会要求您输入密码。这不是加密密钥,不限制为32个字节!如果您打算与他人传输文件,您共享的密码应该非常强大。您可以使用此网站来了解您的密码有多好:
警告:我已经检查过这些网站不会将您的密码发送到服务器,但这可能随时发生变化。在输入强密码之前,请使用开发工具/检查器检查它们是否发送任何内容。
gpg
命令:gpg --cipher-algo AES256 --symmetric filename.tar.gz
速记:
gpg --cipher-algo AES256 -c filename.tar.gz
这将要求输入密码。
解密:
gpg --output filename.tar.gz --decrypt filename.tar.gz.gpg
gpg -o filename.tar.gz -d filename.tar.gz.gpg
~/.gnupg/gpg.conf
中添加cipher-algo AES256
,使AES256成为默认选项。(根据manpage,它是CAST5)AES
(即AES-128)。 AES-128稍微快一些,并且不会降低安全性。 - Gilles 'SO- stop being evil'20.04
版本中将您的密码存储在某个地方,因此当您解密文件时,它甚至不会要求您输入密码。这是出乎意料的。而且我甚至无法通过--no-keyring
选项停止gpg
这样做。 - MWB--no-symkey-cache
标志来防止这种情况发生,或者将no-symkey-cache
一行添加到你的~/.gnupg/gpg.conf
文件中。如果在更新配置文件之前密码已经被缓存,你可以通过重新加载代理来清除它:echo RELOADAGENT | gpg-connect-agent
。 - wilkystyletest@test-VirtualBox:~$
test@test-VirtualBox:~$ echo precious-content | openssl aes-128-cbc -a -salt -k mypassword
U2FsdGVkX1+K6tvItr9eEI4yC4nZPK8b6o4fc0DR/Vzh7HqpE96se8Fu/BhM314z
test@test-VirtualBox:~$
test@test-VirtualBox:~$ echo U2FsdGVkX1+K6tvItr9eEI4yC4nZPK8b6o4fc0DR/Vzh7HqpE96se8Fu/BhM314z | openssl aes-128-cbc -a -d -salt -k mypassword
precious-content
test@test-VirtualBox:~$
test@test-VirtualBox:~$
openssl
。在stefano-palazzo的回答基础上进行扩展:openssl enc -aes-256-cbc -a -e -in input.tar.gz -out oupput.enc -pbkdf2 -iter 1000000 -md sha512
openssl enc -aes-256-cbc -a -d -in output.tar.xz.enc -out output.tar.xz -pbkdf2 -iter 1000000 -md sha512
enc
代表加密-aes-256-cbc
是使用 AES
密码的 一个好方法-a
在加密之后或解密之前对数据进行 base64 编码-d
解密-e
加密-in
输入文件-out
输出文件-pbkdf2
将密钥扩展为难以破解的 密钥派生函数-iter
扩展密钥的迭代次数,更多次意味着更高的安全性,适当的次数可以在 这里找到-md sha512
用 SHA512 替换 PBKDF2 的哈希函数,比默认的 SHA256 更安全-salt
在命令中没有提及,因为它是默认设置的,并且是增加安全性的非常好的方法,为什么如此请参考 这里的说明aes256() {
decodeMe=""
isPipe="$([ ! -t 0 ] && echo "true" || echo "false")"
if [ "$1" = '-d' ] || [ "$1" = '--decode' ]; then
decodeMe="-d"
shift
fi
if [ "$isPipe" = "true" ]; then
read input
printf '%s\n' "$input" | openssl aes-256-cbc -a $decodeMe
exitCode="$?"
else
openssl aes-256-cbc -a $decodeMe -in "$*"
exitCode="$?"
fi
unset isPipe decodeMe input
return "$exitCode"
}
使用方法:
echo "my string" | aes256
# enter aes-256-cbc encryption password
# Returns: U2FsdGVkX1++e/BhBGlNOzNvarqq7zI13S/hbiKVzXQ=
echo "U2FsdGVkX1++e/BhBGlNOzNvarqq7zI13S/hbiKVzXQ=" | aes256 -d
# enter aes-256-cbc decryption password
# Returns: my string
aes256 file.plain > file.crypt
# enter aes-256-cbc encryption password
aes256 -d file.crypt
# enter aes-256-cbc decryption password
# Spits out original unencrypted file.
openssl aes-256-cbc
比openssl enc -aes-256-cbc
更简洁且同样有效。通过运行man enc
可以查看此命令的手册页面。对于不应被篡改的数据,永远不要使用ecb
模式,而是使用cbc
模式。由于默认设置已经包含了盐值(salt),因此-salt
是多余的。如果省略-out filename
参数,则输出将被写入标准输出,这在只需要分析数据而不需要将其写入磁盘时非常有用。下一个命令用于统计明文的行数:openssl aes-256-cbc -d -in filename | wc -l
。(另一个用途,读取文件:openssl aes-256-cbc -d -in filename | less
) - Lekensteyn/usr/bin/openssl enc -aes-256-cbc -a -salt -in $1 -out ${1}.enc
,反向操作与您预期的相同。 - belacquaopenssl enc
实际上并不是很安全。我之前推荐过某些使用openssl enc
的方法,但现在建议改用gpg
,因为它更好地使用了 KDF。请参考这个答案。 - Lekensteyncrypto.cat
似乎已经变成了一个糟糕的无关网站。也许应该删除或替换这个链接。 - Akito