Skip to content

Instantly share code, notes, and snippets.

@turret-io
Last active September 3, 2025 09:42
Show Gist options
  • Select an option

  • Save turret-io/957e82d44fd6f4493533 to your computer and use it in GitHub Desktop.

Select an option

Save turret-io/957e82d44fd6f4493533 to your computer and use it in GitHub Desktop.

Revisions

  1. turret-io revised this gist Apr 3, 2017. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -14,22 +14,25 @@
    $data = "Encrypt me, please!";
    echo "Before encryption: $data\n";

    // Encrypt $data using aes-256-cbc cipher with the given encryption key and
    // Encrypt $data using aes-256-cbc cipher with the given encryption key and
    // our initialization vector. The 0 gives us the default options, but can
    // be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
    $encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
    echo "Encrypted: $encrypted\n";

    // If we lose the $iv variable, we can't decrypt this, so append it to the
    // encrypted data with a separator that we know won't exist in base64-encoded
    // data
    $encrypted = $encrypted . ':' . $iv;
    // If we lose the $iv variable, we can't decrypt this, so:
    // - $encrypted is already base64-encoded from openssl_encrypt
    // - Append a separator that we know won't exist in base64, ":"
    // - And then append a base64-encoded $iv
    $encrypted = $encrypted . ':' . base64_encode($iv);

    // To decrypt, separate the encrypted data from the initialization vector ($iv)
    // To decrypt, separate the encrypted data from the initialization vector ($iv).
    $parts = explode(':', $encrypted);
    // $parts[0] = encrypted data
    // $parts[1] = initialization vector
    // $parts[1] = base-64 encoded initialization vector

    $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, $parts[1]);
    // Don't forget to base64-decode the $iv before feeding it back to
    //openssl_decrypt
    $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, base64_decode($parts[1]));
    echo "Decrypted: $decrypted\n";
    ?>
  2. turret-io revised this gist Oct 1, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    // Generate a 256-bit encryption key
    // This should be stored somewhere instead of recreating it each time
    $encryption_key = openssl_random_pseudo_bytes(256);
    $encryption_key = openssl_random_pseudo_bytes(32);

    // Generate an initialization vector
    // This *MUST* be available for decryption as well
  3. turret-io revised this gist Oct 1, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,16 @@
    // Create some data to encrypt
    $data = "Encrypt me, please!";
    echo "Before encryption: $data\n";

    // Encrypt $data using aes-256-cbc cipher with the given encryption key and
    // our initialization vector. The 0 gives us the default options, but can
    // be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
    $encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
    echo "Encrypted: $encrypted\n";

    // If we lose the $iv variable, we can't decrypt this, so append it to the
    // encrypted data with a separator that we know won't exist in base64-encoded
    // data

    $encrypted = $encrypted . ':' . $iv;

    // To decrypt, separate the encrypted data from the initialization vector ($iv)
  4. turret-io revised this gist Oct 1, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,4 @@

    $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, $parts[1]);
    echo "Decrypted: $decrypted\n";



    ?>
  5. turret-io revised this gist Oct 1, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    // encrypted data with a separator that we know won't exist in base64-encoded
    // data

    $encrypted = $encrypted . ':' . $iv
    $encrypted = $encrypted . ':' . $iv;

    // To decrypt, separate the encrypted data from the initialization vector ($iv)
    $parts = explode(':', $encrypted);
  6. turret-io revised this gist Oct 1, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -12,12 +12,12 @@

    // Create some data to encrypt
    $data = "Encrypt me, please!";

    echo "Before encryption: $data\n";
    // Encrypt $data using aes-256-cbc cipher with the given encryption key and
    // our initialization vector. The 0 gives us the default options, but can
    // be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
    $encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
    echo "Encrypted: $encrypted\n;"
    echo "Encrypted: $encrypted\n";
    // If we lose the $iv variable, we can't decrypt this, so append it to the
    // encrypted data with a separator that we know won't exist in base64-encoded
    // data
  7. turret-io created this gist Oct 1, 2014.
    37 changes: 37 additions & 0 deletions aes_enc_dec.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php
    // DEFINE our cipher
    define('AES_256_CBC', 'aes-256-cbc');

    // Generate a 256-bit encryption key
    // This should be stored somewhere instead of recreating it each time
    $encryption_key = openssl_random_pseudo_bytes(256);

    // Generate an initialization vector
    // This *MUST* be available for decryption as well
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));

    // Create some data to encrypt
    $data = "Encrypt me, please!";

    // Encrypt $data using aes-256-cbc cipher with the given encryption key and
    // our initialization vector. The 0 gives us the default options, but can
    // be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
    $encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
    echo "Encrypted: $encrypted\n;"
    // If we lose the $iv variable, we can't decrypt this, so append it to the
    // encrypted data with a separator that we know won't exist in base64-encoded
    // data
    $encrypted = $encrypted . ':' . $iv
    // To decrypt, separate the encrypted data from the initialization vector ($iv)
    $parts = explode(':', $encrypted);
    // $parts[0] = encrypted data
    // $parts[1] = initialization vector
    $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, $parts[1]);
    echo "Decrypted: $decrypted\n";



    ?>