10

我正在生成要从 Ruby 堆栈发送到 PHP 堆栈的数据。我在 Ruby 端使用 OpenSSL::Cipher 库和 PHP 中的“mcrypt”库。当我在 Ruby 中使用“aes-256-cbc”(256 位块大小)加密时,我需要在 PHP 中使用 MCRYPT_RIJNDAEL_128(128 位块大小)来解密它。我怀疑 Ruby 代码被破坏了,因为 cipher.iv_len 是 16;我认为应该是 32:

>> cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
=> #<OpenSSL::Cipher::Cipher:0x3067c5c>
>> cipher.key_len
=> 16
>> cipher.iv_len
=> 16
>> cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
=> #<OpenSSL::Cipher::Cipher:0x306de18>
>> cipher.key_len
=> 32
>> cipher.iv_len
=> 16

所以这是我的测试。在 Ruby 方面,首先我生成密钥和 iv:

>> cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
>> cipher.encrypt
>> iv = cipher.random_iv
>> iv64 = [iv].pack("m").strip
=> "vCkaypm5tPmtP3TF7aWrug=="
>> key = cipher.random_key
>> key64 = [key].pack("m").strip
=> "RIvFgoi9xZaHS/0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0="

然后我使用这些密钥进行加密:

>> plain_data = "Hi, Don, this is a string."
>> cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
>> cipher.encrypt
>> cipher.key = Base64.decode64(key64)
>> cipher.iv = Base64.decode64(iv64)
>> encrypted_data = cipher.update(plain_data)
>> encrypted_data << cipher.final
>> crypt64 = [encrypted_data].pack("m").strip
=> "5gfC/kJcnAV2fJI0haxnLcdraIKWgtu54UoznVxf8K0="

这是PHP解密:

$ruby_crypt = "5gfC/kJcnAV2fJI0haxnLcdraIKWgtu54UoznVxf8K0=";
$encrypted_data = base64_decode($ruby_crypt);
$key = base64_decode("RIvFgoi9xZaHS/0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0=");
$iv = base64_decode("vCkaypm5tPmtP3TF7aWrug==");
$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted_data, MCRYPT_MODE_CBC, $iv);
$unencrypt = rtrim($result, "\x00..\x1F");
print "\nUnencrypted token:\n'$unencrypt'\n";

RESULT:
Unencrypted token:
'Hi, Don, this is a string.'

我更喜欢使用更长的块大小。显然我误解了 API。帮助?

4

4 回答 4

13

我写了一个例子,其他人可能会发现上述讨论的解释:

$猫出版商.rb

#!/usr/bin/env ruby

require 'openssl'
require 'base64'

key = '7fc4d85e2e4193b842bb0541de51a497'

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

cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.encrypt()
cipher.key = key
cipher.iv = iv
crypt = cipher.update('This is my text')
crypt << cipher.final()

puts [Base64.encode64(crypt).strip(), Base64.encode64(iv).strip()].join('|')

$猫消费者.php

$key256 = '7fc4d85e2e4193b842bb0541de51a497';

$fd = fopen("php://stdin", "r");
$tokens = '';
while (!feof($fd))
  $tokens .= fread($fd, 1024);
fclose($fd);

$tokens = explode('|', trim($tokens));
$crypt = $tokens[0];
$iv = $tokens[1];

$crypttext = base64_decode($crypt);
$iv = base64_decode($iv);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key256, $crypttext, MCRYPT_MODE_CBC, $iv);

print $decrypted ."\n";

要对其进行测试,请从命令行尝试:

$红宝石出版商.rb | php消费者.php

这是我的文字

于 2010-12-06T10:46:20.480 回答
7

我不了解 PHP,但通过阅读侧边栏上的相关问题,我看到Converting Ruby AES256 decrypt function to PHP。这包括对这个页面的引用,指出 128 inMCRYPT_RIJNDAEL_128是指加密的块大小,而不是密钥大小。您会注意到,您在 ruby​​ 和 PHP 之间传递的密钥大小在这两种情况下都是 256 位。换句话说,这似乎是预期的行为,并且您已经在使用较大的密钥。

#!/usr/bin/ruby
require 'base64'

puts((Base64.decode64("RIvFgoi9xZaHS/0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0=").length * 8).to_s)

高温高压

于 2009-12-07T21:17:56.487 回答
0

我遇到了麻烦,因为 PHP 使用的密码小于 8 个字符。在这种情况下,需要添加 0,以使其与 PHP 兼容:

mcrypt-encrypt 手册页“密钥

用于加密数据的密钥。如果它小于所需的密钥大小,则用 '\0' 填充。最好不要将 ASCII 字符串用作键。 http://php.net/manual/en/function.mcrypt-encrypt.php 建议使用 mhash 函数从字符串创建密钥。”

require 'openssl'
cipher = OpenSSL::Cipher.new('DES-ECB')
cipher.encrypt
key =  'passwrd'[0...7].ljust(8, 0.chr)  #Pad the key smaller than 8 chars
cipher.key = key
encrypted = cipher.update('33')
encrypted << cipher.final
dec = Base64.encode64(encrypted).strip()
于 2013-09-05T15:24:25.650 回答
0

让我给你看一些代码。

PHP代码:

$privateKey = "1234567890123456"; # the size is 16.
$data = "hello";
$iv = "0123456789012345";

$result = mcrypt_encrypt(
  MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv
)

$base64str = base64_encode($result);
$base64str = str_replace("+", "-",  $base64str);
$base64str = str_replace("/","_",  $base64str);

# => f-WffBXnf122NcVBUZ6Rlg==

红宝石代码:

require 'base64'
require 'openssl'

private_key = "1234567890123456"
data = "hello"
iv = "0123456789012345"

cipher = OpenSSL::Cipher::AES.new(128, :CBC) 
cipher.encrypt

cipher.padding = 0 # we must disable padding in ruby.
cipher.key = private_key
cipher.iv = iv
block_size = cipher.block_size

# Add padding by yourself.
data = data + "\0" * (block_size - data.bytesize % block_size)
result = cipher.update(data) + cipher.final

Base64.urlsafe_encode64(result)
# ==> f-WffBXnf122NcVBUZ6Rlg==

如您所见,我在 ruby​​ 中使用 AES-128,因为大小为 16。因此,如果您的大小为 32 private_key,则必须使用 AES-256 。private_key

公式:size_of_private_key* 8。

于 2017-07-11T07:40:45.287 回答