Skip to content

Instantly share code, notes, and snippets.

@naveed-ahmad
Last active May 3, 2017 02:01
Show Gist options
  • Save naveed-ahmad/6061fc17d011d5ed24181dd3ac46a8da to your computer and use it in GitHub Desktop.
Save naveed-ahmad/6061fc17d011d5ed24181dd3ac46a8da to your computer and use it in GitHub Desktop.
PHP encryption <=> Ruby decryption using aes 256 cbc
require "openssl"
require "base64"
data = "yKji5PQto8hUuj6bPcgXWQ=="
PASSWORD = '!!!!!sufficiently_long_password!!!!!';
CIPHER_METHOD = 'AES-256-CBC';
cipher = OpenSSL::Cipher.new(CIPHER_METHOD)
data=URI.decode(data)
str = Base64.urlsafe_decode64(data)
cipher.decrypt # set cipher to be encryption mode
cipher.key = PASSWORD
cipher.update(str) + cipher.final
const PASSWORD = '!!!!!sufficiently_long_password!!!!!';
const CIPHER_METHOD = 'AES-256-CBC';
function encrypt($str) {
$val = openssl_encrypt($str, CIPHER_METHOD, PASSWORD);
return $val;
}
$plain_data = 'TEST STRING';
$encrypted = encrypt($plain_data);
echo $encrypted."\n";
@naveed-ahmad
Copy link
Author

encryption demo: https://repl.it/H9Er/1
decryption demo: https://repl.it/9MW/8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment