【问题标题】:Is there any difference between Ruby 'AES-128-CBC' and PHP 'rijndael-128' encryption?Ruby 'AES-128-CBC' 和 PHP 'rijndael-128' 加密有什么区别吗?
【发布时间】:2014-12-10 16:38:45
【问题描述】:

Ruby 代码是

require 'base64'
require 'openssl'

def ruby_dec iv, key, encrypted
  decipher = OpenSSL::Cipher::AES.new(128, :CBC)
  #decipher.padding = 0 
  decipher.decrypt
  decipher.key = key
  decipher.iv = iv
  ciphertext = Base64.decode64 encrypted
  decipher.update(ciphertext) + decipher.final
end


def ruby_enc iv, key, plaintext 
  enc = OpenSSL::Cipher.new 'aes-128-cbc'
  enc.encrypt
  enc.key = key
  enc.iv = iv

  Base64.encode64(enc.update(plaintext) + enc.final)
end

iv = Base64.decode64("TOB+9YNdXSbkSYIU7D/IpQ==")
key =  Base64.decode64("7DxoShENB0D+8xrwOwSbi1TPQBiIaFq2yveoUkutCpM=")
plaintext = "testtesttest"

encrypted = ruby_enc iv, key, plaintext
puts "encrypted is #{encrypted}"
ruby_dec iv, key, encrypted
puts "plaintext is #{plaintext}"

然后

$ ruby enc_dec.rb #the above code
encrypted is LXJmnM7t+HGKi2iI51ethA==
plaintext is testtesttest

现在 PHP 代码是

function php_dec($iv, $key, $encrypted) { 
  $cipher_algorithm    = 'rijndael-128';
  $cipher_mode = 'cbc';

  $key = base64_decode($key);
  $iv = base64_decode($iv);
  $ciphertext = base64_decode($enctypted);
  return $ciphertext;
}


function php_enc($iv, $key, $plaintext){
  $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

  $mcrypt_key = base64_decode($key);
  $iv = base64_decode($iv);
  mcrypt_generic_init($td, $mcrypt_key, $iv);

  $ciphertext = mcrypt_generic($td, $plaintext);

  retun base64_encode($ciphertext); 
}

$iv = base64_decode("TOB+9YNdXSbkSYIU7D/IpQ==");
$key =  base64_decode("7DxoShENB0D+8xrwOwSbi1TPQBiIaFq2yveoUkutCpM=");
$plaintext = "testtesttest";

$encrypted = php_enc($iv, $key, $plaintext);
echo "encrypted is ".$encrypted."\n";
php_dec($iv, $key, $encrypted);
echo "plaintext is ".$plaintext."\n";

$ruby_encrypted = base64_encode("LXJmnM7t+HGKi2iI51ethA==");
php_dec($iv, $key, $ruby_encrypted);
echo "plaintext is ".$plaintext."\n";

然后我得到

$ php enc_dec.php
encrypted is SUR33tXu32JjR9JAKIGL7w==
plaintext is testtesttest
plaintext is testtesttest

密文与红宝石不同。 现在我尝试用 PHP 生成的密文解密 ruby​​。

$ pry
  [1] pry(main)> load 'enc_dec.rb'
  [2] pry(main)> iv = Base64.decode64("TOB+9YNdXSbkSYIU7D/IpQ==")    
  => "L\xE0~\xF5\x83]]&\xE4I\x82\x14\xEC?\xC8\xA5"
  [3] pry(main)> key =  Base64.decode64("7DxoShENB0D+8xrwOwSbi1TPQBiIaFq2yveoUkutCpM=")    
  => "\xEC<hJ\x11\r\a@\xFE\xF3\x1A\xF0;\x04\x9B\x8BT\xCF@\x18\x88hZ\xB6\xCA\xF7\xA8RK\xAD\n\x93"
  [4] pry(main)> ruby_dec iv, key, Base64.decode64("SUR33tXu32JjR9JAKIGL7w==")
  OpenSSL::Cipher::CipherError: data not multiple of block length
  from enc_dec.rb:12:in `final'
  [5] pry(main)> 

Ruby 'AES-128-CBC' 和 PHP 'rijndael-128' 加密有什么区别吗? 以及如何用 ruby​​ 解密?

【问题讨论】:

  • 你能删除你之前关于同一件事的问题吗?请注意,如果您只想提供更多信息,则应编辑您的问题。
  • 谢谢你的评论,我删除了之前的。从下次开始我会编辑我的问题。

标签: php ruby encryption cryptography


【解决方案1】:

AES 和 Rijndael 在 128 的含义上有所不同,在 AES 中 128 是密钥大小,在 Rijndael 中是块大小。

我相信您使用的密钥大于 128 位。

请看这篇文章: http://www.leaseweblabs.com/2014/02/aes-php-mcrypt-key-padding/

【讨论】:

  • 感谢您的评论,PHP 加密密钥大小为 32Byte。
  • 如果 PHP 加密是 32 字节,我应该用 AES-256-CBC 解密吧?
  • 我知道密钥大小和他们名字中包含的数字。并尝试使用AES-256-CBC,我可以成功解密!非常感谢!
猜你喜欢
  • 2013-08-24
  • 1970-01-01
  • 2019-08-17
  • 2020-04-15
  • 2016-05-26
  • 2017-03-27
  • 2016-02-21
  • 2021-06-25
  • 1970-01-01
相关资源
最近更新 更多