Ruby on Rails Decryption

前端 未结 2 1553
南笙
南笙 2021-01-26 04:36

The following function works perfect in PHP. How can it be translated in Ruby on Rails.

Please note that both privateKey and iv are 32 characters long.

m         


        
相关标签:
2条回答
  • 2021-01-26 05:08

    Here some code which works for me :

    
    def decrypt_data(data, pwd, iv)
        encrypted_data = Base64.decode64(data)
        aes = OpenSSL::Cipher::Cipher.new("AES-256-CBC")
        aes.decrypt
        aes.key = Digest::MD5.hexdigest(pwd)
        aes.iv = iv
        result = aes.update(encrypted_data) + aes.final 
    end
    

    In my example the password is encrypted with MD5.

    I hope this help

    0 讨论(0)
  • 2021-01-26 05:09

    You are base64 decoding it in the php example. Are you doing that in the ruby one as well?

    require "base64"
    Base64.decode64(encrypted)
    

    Other than that, the code looks right to me.

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