Encrypting in PHP (mcrypt), Decrypting in Ruby (OpenSSL::Cipher)

前端 未结 1 1586
既然无缘
既然无缘 2021-01-15 17:48

I\'m working on a cross language project wrapping a ruby/Sinatra API in PHP to be consumed by another team. None of the information exposed by the API is sensitive, but we w

相关标签:
1条回答
  • 2021-01-15 18:10

    I was lead to believe Mcrypt's RIJNDAEL and Cipher's AES were compatible, but is this assumption incorrect?

    You need to slightly tweak data being encoded to make it AES compatible. Data must be right padded, with character and amount depending of its current width:

    $encode = $currentEpoch.'**Passcode**';
    $len = strlen($encode);
    $pad = 16 - ($len % 16);
    $encode .= str_repeat(chr($pad), $pad);
    

    Also remember to have $key exactly 16 characters long. If it is shorter, ruby throws CipherError, while php pads key with null bytes. If it is longer, ruby uses only first 16 character but php pads it again, and uses last 16 characters.

    0 讨论(0)
提交回复
热议问题