【问题标题】:Ruby create Cipher key from preexisting key stringRuby 从预先存在的密钥字符串创建密码密钥
【发布时间】: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

标签: ruby openssl


【解决方案1】:

这里有几个问题。

由于这一行,您的 Ruby 往返代码无法正常工作:

encrypted = cipher.update "flipflop" + cipher.final

应该是:

encrypted = cipher.update("flipflop") + cipher.final

这是一个错误的加密,导致bad decrypt 错误。否则,该代码应该可以工作,尽管它使用的键和 ivs 与命令行版本不同。但是,它只适用于旧版本的 Ruby 和 OpenSSL 绑定。当前版本的 Ruby 的 OpenSSL 库会检查所提供的 keyiv 的长度,如果错误则引发异常。对于 aes-256-cbc,它们应分别为 32 和 16 字节。

openssl enc 命令的-K-iv 选项接受十六进制编码字符串,然后将其解码为原始字节。它还会用零字节填充这些值,直到它们的长度正确。

以下是在 Ruby 中解密命令行加密字符串的方法:

# Base64 cipher text from the question:
message = "Pd4+UEBW1RyTjARy1rpndQ=="
message = Base64.decode64(message)

key = "2317823178123897237891232345345234524523452345"
#Convert from hex to raw bytes:
key = [key].pack('H*')
#Pad with zero bytes to correct length:
key << ("\x00" * (32 - key.length))

iv ="123789123789123789"
#Convert from hex to raw bytes:
iv = [iv].pack('H*')
#Pad with zero bytes to correct length:
iv << ("\x00" * (16 - iv.length))

cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.decrypt
cipher.key = key
cipher.iv = iv
plain = cipher.update(message) + cipher.final
puts plain # => 'flipflop'

【讨论】:

  • 漂亮,我马上试试这个。感谢马特提供的信息。
猜你喜欢
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 2015-07-31
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多