Last active
September 3, 2025 09:42
-
-
Save turret-io/957e82d44fd6f4493533 to your computer and use it in GitHub Desktop.
Revisions
-
turret-io revised this gist
Apr 3, 2017 . 1 changed file with 11 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal 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 // 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: // - $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). $parts = explode(':', $encrypted); // $parts[0] = encrypted data // $parts[1] = base-64 encoded initialization vector // 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"; ?> -
turret-io revised this gist
Oct 1, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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(32); // Generate an initialization vector // This *MUST* be available for decryption as well -
turret-io revised this gist
Oct 1, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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) -
turret-io revised this gist
Oct 1, 2014 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal 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"; ?> -
turret-io revised this gist
Oct 1, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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; // To decrypt, separate the encrypted data from the initialization vector ($iv) $parts = explode(':', $encrypted); -
turret-io revised this gist
Oct 1, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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"; // 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 -
turret-io created this gist
Oct 1, 2014 .There are no files selected for viewing
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 charactersOriginal 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"; ?>