I’m encrypting with Ruby and decrypting with JavaScript using AES-256 but I’m having trouble crossing platforms, i.e. the JS returns gibberish when decrypting the output from Ruby.
I’m using the JS AES implementation from here: http://www.movable-type.co.uk/scripts/aes.html
var decrypted = Aes.Ctr.decrypt(encrypted, key, 256);
and OpenSSL / Ruby:
def encrypt(string, key)
Base64.encode64(aes(key, string)).gsub /\s/, ''
end
def aes(key,string)
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.encrypt
cipher.key = Digest::SHA256.digest(key)
cipher_text = cipher.update(string)
cipher_text << cipher.final
return cipher_text
end
In ruby I get:
encrypt("This is a test", "password")
# => "zDMm47GniTQ2p5a5UqSDbg=="
But when decrypting in JS, I get:
Aes.Ctr.decrypt("zDMm47GniTQ2p5a5UqSDbg==", "password", 256);
# => "Ü}$> 3"
Any ideas? Thanks in advance.
In the end I used Gibberish AES which has both a Ruby and JavaScript implementation written by the same author:
https://github.com/mdp/gibberish-aes
https://github.com/mdp/gibberish