title1 - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of title1.php
- <?php
- class AesCipher
- {
- private const OPENSSL_CIPHER_NAME = "aes-128-cbc";
- private const CIPHER_KEY_LEN = 16; //128 bits
- private static function fixKey($key)
- {
- if (strlen($key) < AesCipher::CIPHER_KEY_LEN) {
- //0 pad to len 16
- return str_pad("$key", AesCipher::CIPHER_KEY_LEN, "0");
- }
- if (strlen($key) > AesCipher::CIPHER_KEY_LEN) { //truncate to 16 bytes
- return substr($key, 0, AesCipher::CIPHER_KEY_LEN);
- }
- return $key;
- }
- static function encrypt($key, $salt, $data)
- {
- $encodedEncryptedData = base64_encode(openssl_encrypt($data,
- AesCipher::OPENSSL_CIPHER_NAME, AesCipher::fixKey($key), OPENSSL_RAW_DATA, $salt));
- $encodedIV = base64_encode($salt);
- $encryptedPayload = $encodedEncryptedData . ":" . $encodedIV;
- return $encryptedPayload;
- }
- static function flatten(array $array)
- {
- $result = [];
- array_walk_recursive($array, function ($a, $b) use (&$result) {
- $result[] = "$b=$a";
- });
- return implode('&', $result);
- }
- static function execute()
- {
- $data = ['amount' => '200.00', 'currency' => 'INR', 'id' => '33632314-0a5d-4d01-9b56-663393f14ea4', 'status' => 'SUCCESS', 'transactionId' => '26'];
- $key = '100';
- $salt = '896e2cf326e36e22';
- $dataString = self::flatten($data);
- return self::encrypt($key, $salt, $dataString);
- }
- }
- echo AesCipher::execute();;