cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

encrypt_token.php (1397B)


      1<?php
      2
      3const CIPHER = 'AES-256-CBC';
      4const SECRET_KEY = 'MySuperSecretKeyForParamsToken12';
      5
      6$tokenObject = [
      7    "connection" => [
      8        "type" => "rdp",
      9        "settings" => [
     10            "hostname" => "10.0.0.12",
     11            "username" => "Administrator",
     12            "password" => "pAsSwOrD",
     13            "enable-drive" => true,
     14            "create-drive-path" => true,
     15            "security" => "any",
     16            "ignore-cert" => true,
     17            "enable-wallpaper" => false
     18        ]
     19    ]
     20];
     21
     22function encryptToken($value): string
     23{
     24    $iv = random_bytes(16);
     25
     26    $value = openssl_encrypt(
     27        json_encode($value),
     28        CIPHER,
     29        SECRET_KEY,
     30        0,
     31        $iv
     32    );
     33
     34    if ($value === false) {
     35        throw new Exception('Could not encrypt the data.');
     36    }
     37
     38    $data = [
     39        'iv' => base64_encode($iv),
     40        'value' => $value,
     41    ];
     42
     43    $json = json_encode($data);
     44
     45    if (!is_string($json)) {
     46        throw new Exception('Could not encrypt the data.');
     47    }
     48
     49    return base64_encode($json);
     50}
     51
     52
     53$token = encryptToken($tokenObject);
     54
     55echo "Parameters:" . PHP_EOL;
     56echo json_encode($tokenObject, JSON_PRETTY_PRINT) . PHP_EOL;
     57
     58echo PHP_EOL . PHP_EOL;
     59
     60echo "Encrypted token:" . PHP_EOL;
     61echo $token . PHP_EOL;
     62
     63echo PHP_EOL . PHP_EOL;
     64
     65echo "Use this token in the URL:" . PHP_EOL;
     66echo "ws://localhost:8080/?token=" . urlencode($token) . PHP_EOL;