2012-12-08 52 views
2

我試圖轉換一些ruby代碼,用CBC模式中的AES 265加密數據到PHP,但它不工作,轉換後的php代碼返回一個空字符串。這裏是我有:Ruby to PHP AES

紅寶石:

require 'openssl' 

module AESCrypt 
    def self.encrypt(message, password) 
    Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-256-CBC")) 
    end 

    def self.decrypt(message, password) 
    base64_decoded = Base64.decode64(message.to_s.strip) 
    self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-256-CBC") 
    end 

    def self.key_digest(password) 
    OpenSSL::Digest::SHA256.new(password).digest 
    end 

def self.decrypt_data(encrypted_data, key, iv, cipher_type) 
    aes = OpenSSL::Cipher::Cipher.new(cipher_type) 
    aes.decrypt 
    aes.key = key 
    aes.iv = iv if iv != nil 
    aes.update(encrypted_data) + aes.final 
    end 

def self.encrypt_data(data, key, iv, cipher_type) 
    aes = OpenSSL::Cipher::Cipher.new(cipher_type) 
    aes.encrypt 
    aes.key = key 
    aes.iv = iv if iv != nil 
    aes.update(data) + aes.final  
    end 
end 

而且PHP代碼:

echo base64_encode($encrypted_data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hash('sha256', 'p4ssw0rd'), 'hey', MCRYPT_MODE_CBC)); 

回答