如何在 Ruby 中解密由 Node 已弃用的 createCipher 加密的数据?

mea*_*gar -2 ruby encryption cryptography node.js

我有一些遗留数据,在 Node 中加密,我需要在 Ruby 中解密。

问题是,数据是使用现已弃用的方法加密的,createCipher. 此方法使用密码来执行加密。它已被替换为createCipheriv,它需要一个 32 字节的密钥和一个 16 字节的初始化向量。

在 Node 中,我可以使用同样不推荐使用的 解密字符串createDecipher,它也接受密码。但是,我不知道 Ruby 中有任何等效的方法,这是有道理的,因为现在已知这些方法不安全并且已弃用。

Ruby的OpenSSL的AES密码正确,需要一个32字节的密钥和16字节的IV类的较新的createCipheriv,但我没有任何的这些,只有原来的密码,我用createCipher

如何在 Ruby 中获得类似createCipher/createDecipher的行为?

具体来说,鉴于以下 JavaScript ...

const crypto = require('crypto');

const cipher = crypto.createCipher("aes-256-cbc", 'secret password');
cipherText = cipher.update('dummy string', "utf8", "hex") + cipher.final("hex");

console.log(cipherText); // f3051259f83c7ca2ac012a396c4c0848
Run Code Online (Sandbox Code Playgroud)

...如何使用密码'secret password'解密字符串'f3051259f83c7ca2ac012a396c4c0848'并返回'dummy string'Ruby中的输入值?

mea*_*gar 6

底层算法 aes256 需要 32 位密钥和 16 位初始化向量,而不管createCipher. 在幕后,它必须产生一个 32 位的密钥和 16 位的初始化向量,从输入的密码中导出。

createCipher通过对输入密码重复 MD5 散列来生成字节数组,从中提取 32 字节的密钥和 16 字节的初始化向量。它已被弃用,因为这不是产生随机 IV 的安全方式。

这发生在 Node 的 C 源代码中,在/deps/openssl/openssl/crypto/evp/evp_key.c 中

这个函数有很多参数,但是当用于为 生成一个 key/iv 时createCipher('aes-256-cbc', 'password'),这个方法在这里被调用,带有以下参数:

  • const EVP_CIPHER *type // "aes-256-cbc"
  • const EVP_MD *md // EVP_md5()
  • const unsigned char *salt // 空,未使用
  • const unsigned char *data // “密码”
  • int datal // 8、“密码”的长度,
  • int count // 1、未使用
  • unsigned char *key // 输出密钥缓冲区,
  • unsigned char *iv // 输出 iv 缓冲区

此方法必须产生总共 48 个字节(密钥为 32,IV 为 16),它通过首先运行来实现hash = md5(password),产生 16 个字节。

现在,对于每个后续的 16 个字节集,它将前 16 个字节与 连接起来password,并再次对其进行散列:hash = md5(hash + password)

有效字节...

  • 通过生成 0 到 16 md5(password)
  • 17 到 32 生成通过 md5(md5(password) + password)
  • 33 到 48 通过md5(md5(md5(password) + password) + password).

我们可以在 Ruby 中实现相同的方法来派生正确的密钥和 IV 来通过密码解密给定的字符串:

require 'digest'
require 'openssl'

def decrypt(cipher_text, password)
  bytes = [ Digest::MD5.digest(password) ]
  bytes << Digest::MD5.digest(bytes.last + password)
  bytes << Digest::MD5.digest(bytes.last + password)

  bytes = bytes.join

  cipher = OpenSSL::Cipher.new('aes-256-cbc')
  cipher.decrypt

  cipher.key = bytes[0...32]
  cipher.iv = bytes[32...48]

  # OpenSSL deals in raw bytes, not the hex-encoded representation
  # 'f3051259f83c7ca2ac012a396c4c0848' => "\xF3\x05\x12Y\xF8<|\xA2\xAC\x01*9lL\bH"
  cipher_text = cipher_text.unpack('a2' * (cipher_text.length / 2)).map(&:hex).pack('c*')

  cipher.update(cipher_text) + cipher.final
end

decrypt('f3051259f83c7ca2ac012a396c4c0848', 'secret password') # => 'dummy string'
Run Code Online (Sandbox Code Playgroud)

为了完整起见,这里是等效的encrypt方法,它将允许在 Node 的 deprecate 中解密在 Ruby 中加密的数据createDecipher

 def encrypt(plain_text, password)
  cipher = OpenSSL::Cipher.new('aes-256-cbc')
  cipher.encrypt

  bytes = [ Digest::MD5.digest(password) ]
  bytes << Digest::MD5.digest(bytes.last + password)
  bytes << Digest::MD5.digest(bytes.last + password)

  bytes = bytes.join

  cipher.key = bytes[0...32]
  cipher.iv = bytes[32...48]

  cipher_text = cipher.update(plain_text) + cipher.final

  # Produce a hex representation
  # "\xF3\x05\x12Y\xF8<|\xA2\xAC\x01*9lL\bH" => 'f3051259f83c7ca2ac012a396c4c0848'
  cipher_text.unpack('C*').map { |byte| byte.to_s(16) }.map { |str| str.rjust(2, "0") }.join
end

encrypt('dummy string', 'secret password') # => "f3051259f83c7ca2ac012a396c4c0848"
Run Code Online (Sandbox Code Playgroud)