function isAdmin($query, $key)
{
    $data = decryptAES128CBC($query, $key, $key);
    if (preg_match('/^[\\x{21}-\\x{7E}]*$/', $data)) {
        return strpos($data, ';admin=true;') !== false;
    }
    throw new Exception($data);
}
function validPadding($ciphertext, $key, $iv)
{
    try {
        decryptAES128CBC($ciphertext, $key, $iv, true);
        return true;
    } catch (Exception $e) {
        return false;
    }
}
function isAdmin($query, $key, $iv)
{
    $data = decryptAES128CBC($query, $key, $iv);
    return strpos($data, ';admin=true;') !== false;
}
function decryptAES128CBC($data, $key, $iv = "", $strictPadding = false)
{
    $blocks = str_split($data, 16);
    foreach ($blocks as &$block) {
        $nextIV = $block;
        $block = $iv ^ _decryptAES128ECB($block, $key);
        $iv = $nextIV;
    }
    $plaintext = implode($blocks);
    try {
        return removePKCS7Padding($plaintext);
    } catch (Exception $e) {
        if ($strictPadding) {
            throw $e;
        }
        return $plaintext;
    }
}
// don't output if we're included into another script.
if (!debug_backtrace()) {
    $encrypted = base64_decode(file_get_contents('10-data.txt'));
    $key = 'YELLOW SUBMARINE';
    $decrypted = decryptAES128CBC($encrypted, $key);
    $homebrewEncrypted = encryptAES128CBC($decrypted, $key);
    $homebrewDecrypted = decryptAES128CBC($homebrewEncrypted, $key);
    print "Sanity check:\n";
    $sanity = $decrypted === $homebrewDecrypted;
    print $sanity ? "Success!\n\n" : "Failure :(\n\n";
    print "Decrypted data:\n";
    print "{$decrypted}\n";
}
 function receive($data)
 {
     if (!$this->shared) {
         $this->kexResponse($data);
         return;
     }
     $key = sha1($this->shared, true);
     $iv = substr($data, 0, 16);
     $message = decryptAES128CBC(substr($data, 16), $key, $iv);
     print "{$this->name} received: {$message}\n";
 }
 function sniffData($data)
 {
     $obj = json_decode($data);
     if (is_object($obj)) {
         if ($obj->msg === 'neg' || $obj->msg === 'ack') {
             print "M: manipulating g\n";
             $this->Pminus1 = gmp_strval(gmp_sub(gmp_init($obj->p, 16), gmp_init(1)), 16);
             $obj->g = $this->Pminus1;
             $data = json_encode($obj);
         } else {
             print "M: sniffed: {$data}\n";
         }
     } else {
         $key = sha1('1', true);
         $iv = substr($data, 0, 16);
         $message = decryptAES128CBC(substr($data, 16), $key, $iv);
         // kind of dirty I guess, but gets the job done.
         $obj = json_decode($message);
         if (!is_object($obj)) {
             $key = sha1($this->Pminus1, true);
             $iv = substr($data, 0, 16);
             $message = decryptAES128CBC(substr($data, 16), $key, $iv);
         }
         print "M: sniffed: {$message}\n";
     }
     return $data;
 }