Domanda

Sto generazione dei dati per l'invio di un Rubino stack di uno stack PHP.Sto usando OpenSSL::Cipher libreria Ruby lato e la "mcrypt' libreria in PHP.Quando ho crittografare l'utilizzo di 'aes-256-cbc' (256-bit, dimensione del blocco) in Ruby è necessario utilizzare MCRYPT_RIJNDAEL_128 (128-bit dimensione del blocco) in PHP per la decodifica.Ho il sospetto che il codice Ruby che è rotto, perché il livello di codifica.iv_len 16;Credo che dovrebbe essere 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

Quindi, ecco il mio test.Su Ruby lato, prima di generare la chiave e 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="

Poi io uso quelli tasti per eseguire la crittografia:

>> 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="

Ecco il PHP decrittazione:

$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.'

Io preferisco usare il blocco più dimensioni.Chiaramente mi equivoco le Api.Aiuto?

È stato utile?

Soluzione

Non so PHP, ma la lettura attraverso domande relative nella barra laterale, vedo Conversione funzione di decifrare rubino AES256 a PHP . Questo include un riferimento a questa pagina , indicando che il 128 in MCRYPT_RIJNDAEL_128 riferisce alla dimensione del blocco della crittografia, non la dimensione della chiave. Si noterà che la dimensione della chiave che hai superato tra Ruby e PHP è di 256 bit in entrambi i casi. In altre parole, questo sembra essere il comportamento previsto, e si utilizza già la chiave più grande.

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

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

HTH

Altri suggerimenti

Ho scritto un esempio che qualcun altro potrebbe trovare esplicativa della discussione di cui sopra:

$ cat editore.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('|')

$ cat consumer.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";

Il test, da linea di comando provare:

$ rubino editore.rb | php consumer.php

Questo è il mio testo

ho avuto problemi perché il PHP stava usando una password più piccolo di 8 caratteri. In questo caso si ha la necessità di aggiungere il 0, per renderlo compatibile con PHP:

pagina di manuale mcrypt-cifrare "Tasto

La chiave con cui verranno crittografati i dati. Se è più piccolo della dimensione della chiave richiesto, è imbottito con '\ 0'. E 'meglio non usare le stringhe ASCII per le chiavi. http://php.net/manual/en/function.mcrypt-encrypt.php Si consiglia di utilizzare le funzioni Mhash per creare una chiave da una stringa ".

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()

Mi permetta di mostrare un po 'di codice.

codice 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==

codice Ruby:

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==

Come potete vedere sto utilizzando AES-128 in Ruby perché la dimensione di private_key è 16. Quindi, è necessario utilizzare AES-256 se la dimensione del vostro private_key è 32.

Formula:. size_of_private_key * 8

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top