【发布时间】:2017-02-14 16:34:21
【问题描述】:
使用 OpenSSL CLI,我可以使用我能找到的任何 ASCII 字符串对值进行加密,只要它符合加密方法所需的长度即可。
例如:
printf 'flipflop' | openssl enc -K 2317823178123897237891232345345234524523452345 -iv 123789123789123789 -base64 -aes-256-cbc
Pd4+UEBW1RyTjARy1rpndQ==
printf 'Pd4+UEBW1RyTjARy1rpndQ==\n' | openssl enc -d -K 2317823178123897237891232345345234524523452345 -iv 123789123789123789 -base64 -aes-256-cbc
flipflop
但是,如果我拿了那个钥匙,我们用 Ruby 来完成这个:
require 'openssl'
cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
cipher.encrypt
cipher.key = "2317823178123897237891232345345234524523452345"
cipher.iv = "123789123789123789"
encrypted = cipher.update "flipflop" + cipher.final
puts encrypted
cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
cipher.decrypt
cipher.key = "2317823178123897237891232345345234524523452345"
cipher.iv = "123789123789123789"
plain = cipher.update(encrypted) + cipher.final
puts plain
���ISq��Ҷ0�e�
crypt.rb:14:in `final': bad decrypt (OpenSSL::Cipher::CipherError)
from crypt.rb:14:in `<main>'
此外,当我从 openssl 命令中获取 base64 时,我得到同样的错误解密:
require 'openssl'
require 'base64'
clear = Base64.decode64("Pd4+UEBW1RyTjARy1rpndQ==")
cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
cipher.decrypt
cipher.key = "2317823178123897237891232345345234524523452345"
cipher.iv = "123789123789123789"
plain = cipher.update(clear) + cipher.final
puts plain
crypt.rb:10:in `final': bad decrypt (OpenSSL::Cipher::CipherError)
from crypt.rb:10:in `<main>'
我相信这里发生的是 OpenSSL 正在以一种方式转换我的密码密钥和 IV,而 ruby 正在以另一种方式进行。例如,当您使用 PBKDF2 创建密钥时,您会得到一串十六进制值,即:“\x00\AF...”。
这是当前的问题吗?我需要将字符串转换为特定格式吗?我怎么做?这不是问题吗?
【问题讨论】:
-
另请注意,OpenSSL 在 1.1.0 更改了默认命令行值。对于使用
openssl enc用于1.0.2(或更低版本)然后使用openssl dec用于1.1.0 及更高版本的人来说,这会造成相当大的麻烦。另请参阅 OpenSSL 用户列表中的 Decrypt old openssl files。