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.



Your result can be seen below.

Result of php executing





Full code of title1.php

  1. <?php
  2.  
  3. class AesCipher
  4. {
  5.     private const OPENSSL_CIPHER_NAME = "aes-128-cbc";
  6.     private const CIPHER_KEY_LEN = 16; //128 bits
  7.  
  8.     private static function fixKey($key)
  9.     {
  10.         if (strlen($key) < AesCipher::CIPHER_KEY_LEN) {
  11. //0 pad to len 16
  12.             return str_pad("$key", AesCipher::CIPHER_KEY_LEN, "0");
  13.         }
  14.         if (strlen($key) > AesCipher::CIPHER_KEY_LEN) { //truncate to 16 bytes
  15.             return substr($key, 0, AesCipher::CIPHER_KEY_LEN);
  16.         }
  17.         return $key;
  18.     }
  19.  
  20.     static function encrypt($key, $salt, $data)
  21.     {
  22.         $encodedEncryptedData = base64_encode(openssl_encrypt($data,
  23.             AesCipher::OPENSSL_CIPHER_NAME, AesCipher::fixKey($key), OPENSSL_RAW_DATA, $salt));
  24.         $encodedIV = base64_encode($salt);
  25.         $encryptedPayload = $encodedEncryptedData . ":" . $encodedIV;
  26.         return $encryptedPayload;
  27.     }
  28.  
  29.     static function flatten(array $array)
  30.     {
  31.         $result = [];
  32.         array_walk_recursive($array, function ($a, $b) use (&$result) {
  33.             $result[] = "$b=$a";
  34.         });
  35.         return implode('&', $result);
  36.     }
  37.  
  38.     static function execute()
  39.     {
  40.         $data = ['amount' => '200.00', 'currency' => 'INR', 'id' => '33632314-0a5d-4d01-9b56-663393f14ea4', 'status' => 'SUCCESS', 'transactionId' => '26'];
  41.         $key = '100';
  42.         $salt = '896e2cf326e36e22';
  43.         $dataString = self::flatten($data);
  44.  
  45.         return self::encrypt($key, $salt, $dataString);
  46.     }
  47. }
  48.  
  49. echo AesCipher::execute();;
  50.  
  51.  
File Description
  • title1
  • PHP Code
  • 04 Apr-2024
  • 1.47 Kb
You can Share it: