Last active
May 3, 2017 02:01
-
-
Save naveed-ahmad/6061fc17d011d5ed24181dd3ac46a8da to your computer and use it in GitHub Desktop.
PHP encryption <=> Ruby decryption using aes 256 cbc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
encryption demo: https://repl.it/H9Er/1
decryption demo: https://repl.it/9MW/8