使用相同参数加密文本时,OpenSSL 命令行和 Ruby OpenSSL 库有所不同

发布于 2024-12-05 03:28:24 字数 1553 浏览 1 评论 0原文

注意:使用 OpenSSL 对文本进行对称加密。

我制作了一个 Ruby 脚本来测试 OpenSSL,我发现我得到了不同的结果。密钥、iv 和密码是相同的,所以我希望结果是相同的。但他们不是。这是我的脚本:

require 'openssl'
require 'base64'

key = "00000000000000000000000000000000"
iv = "00000000000000000000000000000000"

### OPENSSL Command Line ###
puts "*** OpenSSL Command Line ***"
print "Encrypted via Command Line:  "
string = `printf %s \"Hello\" | openssl enc -aes-128-cbc -K #{key} -iv #{iv} -base64`

puts string
puts string.inspect

print "Decrypted Data is:  "
puts `printf %s \"BC2+AQJ6ZQx0al3GXba+EQ==\n\" | openssl enc -d -aes-128-cbc -K #{key} -  iv #{iv} -base64`
puts "\n"

### Ruby OpenSSL Library ###
puts "*** OpenSSL Ruby Library ***"

cipher = OpenSSL::Cipher.new('aes-128-cbc').encrypt
cipher.padding = 1
cipher.key = key
cipher.iv = iv

encrypted_data = cipher.update("Hello")
encrypted_data << cipher.final
encrypted_data = Base64.encode64(encrypted_data)
puts "Encrypted via Ruby is: #{encrypted_data}"
puts encrypted_data.inspect

decipher = OpenSSL::Cipher.new('aes-128-cbc').decrypt
decipher.key = key
decipher.iv = iv

data = decipher.update(Base64.decode64(encrypted_data))
data << decipher.final
puts "Decrypted Data:  #{data}"

结果是:

*** OpenSSL Command Line ***
Encrypted via Command Line:  BC2+AQJ6ZQx0al3GXba+EQ==
"BC2+AQJ6ZQx0al3GXba+EQ==\n"
Decrypted Data is:  Hello

*** OpenSSL Ruby Library ***
Encrypted via Ruby is: ZkeNEgsUXi1J7ps6kCQxdQ==
"ZkeNEgsUXi1J7ps6kCQxdQ==\n"
Decrypted Data:  Hello

只是一个奇怪的结果。知道是什么导致数据不同吗?

Note: Using OpenSSL for symmetric encryption of text.

I made a Ruby script to test OpenSSL and I found I'm getting different results. The key, iv, and ciphers are identical, so I would expect the results to be identical. But they are not. Here's my script:

require 'openssl'
require 'base64'

key = "00000000000000000000000000000000"
iv = "00000000000000000000000000000000"

### OPENSSL Command Line ###
puts "*** OpenSSL Command Line ***"
print "Encrypted via Command Line:  "
string = `printf %s \"Hello\" | openssl enc -aes-128-cbc -K #{key} -iv #{iv} -base64`

puts string
puts string.inspect

print "Decrypted Data is:  "
puts `printf %s \"BC2+AQJ6ZQx0al3GXba+EQ==\n\" | openssl enc -d -aes-128-cbc -K #{key} -  iv #{iv} -base64`
puts "\n"

### Ruby OpenSSL Library ###
puts "*** OpenSSL Ruby Library ***"

cipher = OpenSSL::Cipher.new('aes-128-cbc').encrypt
cipher.padding = 1
cipher.key = key
cipher.iv = iv

encrypted_data = cipher.update("Hello")
encrypted_data << cipher.final
encrypted_data = Base64.encode64(encrypted_data)
puts "Encrypted via Ruby is: #{encrypted_data}"
puts encrypted_data.inspect

decipher = OpenSSL::Cipher.new('aes-128-cbc').decrypt
decipher.key = key
decipher.iv = iv

data = decipher.update(Base64.decode64(encrypted_data))
data << decipher.final
puts "Decrypted Data:  #{data}"

The results are:

*** OpenSSL Command Line ***
Encrypted via Command Line:  BC2+AQJ6ZQx0al3GXba+EQ==
"BC2+AQJ6ZQx0al3GXba+EQ==\n"
Decrypted Data is:  Hello

*** OpenSSL Ruby Library ***
Encrypted via Ruby is: ZkeNEgsUXi1J7ps6kCQxdQ==
"ZkeNEgsUXi1J7ps6kCQxdQ==\n"
Decrypted Data:  Hello

Just a curious result. Any idea what's causing the data to be different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-12-12 03:28:24

只是猜测,在不了解 Ruby 的 OpenSSL 接口的情况下:

您以十六进制编码将密钥和初始化向量提供给命令行 OpenSSL。例如,您的密钥和初始化向量是0x000...

我想您的 Ruby 库将密钥和初始化向量作为二进制数据,例如,您实际上传递的是由 0x30303030... (假设 ASCII 或与之兼容的任何内容)组成的密钥和初始化向量,而不是 <代码>0x00000...。

Just a guess, without knowing Ruby's OpenSSL interface:

You give the keys and initialization vector to the command line OpenSSL in hexadecimal encoding. E.g. your key and initialization vector are 0x000....

I suppose your Ruby library takes the key and initialization vector as binary data, e.g you are actually passing a key and initialization vector consisting of 0x30303030... (assuming ASCII or anything compatible to it) instead of 0x00000....

故事灯 2024-12-12 03:28:24

将它们打包为二进制(十六进制)序列将修复它。
在我的机器上测试(Mac ox 10.11.1 ruby​​-2.2.3)。

cipher.key = ["#{key}"].pack('H*')
cipher.iv = ["#{iv}"].pack('H*')

ruby 包

decipher.key = ["#{key}"].pack('H*')
decipher.iv = ["#{iv}"].pack('H*')

Pack them to a binary(Hex) sequence will fix it.
Test on my machine(Mac ox 10.11.1 ruby-2.2.3).

cipher.key = ["#{key}"].pack('H*')
cipher.iv = ["#{iv}"].pack('H*')

ruby Packs

decipher.key = ["#{key}"].pack('H*')
decipher.iv = ["#{iv}"].pack('H*')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文