Reputation: 187
Im trying to convert some ruby code that encrypts data with AES 265 in CBC mode to php but its not working, the converted php code returns a null string. Here is what i have:
Ruby:
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
And the php code:
echo base64_encode($encrypted_data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hash('sha256', 'p4ssw0rd'), 'hey', MCRYPT_MODE_CBC));
Upvotes: 2
Views: 383
Reputation: 496
have a look at
https://github.com/nirnanaaa/xlix/blob/master/xlix/lib/Xlix/Bundle/Crypto/Aes/TwoLevel.php
I wrote this a while ago for my crypto functions based on a GIST I have found on the web
Upvotes: 2