Skip to content

Instantly share code, notes, and snippets.

@locked
Created March 6, 2016 16:49
Show Gist options
  • Save locked/b537bf726f94a1c51fbf to your computer and use it in GitHub Desktop.
Save locked/b537bf726f94a1c51fbf to your computer and use it in GitHub Desktop.
AES CBC PHP & CLI
// This code generate AES block compatible with how Go does things
$string = "string to encrypt";
$key = "my secret key";
$iv = openssl_random_pseudo_bytes(16);
echo base64_encode($iv.openssl_encrypt($string, "aes-256-cbc", $key, OPENSSL_RAW_DATA, $iv));
// The shell equivalent:
// echo 'string to encrypt' | openssl enc -aes-256-cbc -nosalt -K $KEY -iv acfa7a047800b2f221f2c4f7d626eafb | base64
// In which:
// $KEY is the hex string of the binary, padded, key
// $IV is the hex string of the binary IV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment