/** * Packages the response to a JSON-encoded object, optionally encrypting the * data part with a caller-supplied password. * * @return string The JSON-encoded response */ private function getResponse() { // Initialize the response $response = array('encapsulation' => $this->encapsulation, 'body' => array('status' => $this->status, 'data' => null)); $data = json_encode($this->data); if (empty($this->password)) { $this->encapsulation = self::ENCAPSULATION_RAW; } switch ($this->encapsulation) { case self::ENCAPSULATION_RAW: break; case self::ENCAPSULATION_AESCTR128: $data = Factory::getEncryption()->AESEncryptCtr($data, $this->password, 128); break; case self::ENCAPSULATION_AESCTR256: $data = Factory::getEncryption()->AESEncryptCtr($data, $this->password, 256); break; case self::ENCAPSULATION_AESCBC128: $data = base64_encode(Factory::getEncryption()->AESEncryptCBC($data, $this->password, 128)); break; case self::ENCAPSULATION_AESCBC256: $data = base64_encode(Factory::getEncryption()->AESEncryptCBC($data, $this->password, 256)); break; } $response['body']['data'] = $data; return '###' . json_encode($response) . '###'; }
/** * Encodes the data. The data is JSON encoded by this method before encapsulation takes place. Encrypted * encapsulations will then encrypt the data and base64-encode it before returning it. * * The data being encoded correspond to the body > data structure described in the API documentation * * @param string $serverKey The server key we need to encode data * @param mixed $data The data to encode, typically a string, array or object * * @return string The encapsulated data * * @see https://www.akeebabackup.com/documentation/json-api/ar01s02s02.html * * @throws \RuntimeException When the server capabilities don't match the requested encapsulation * @throws \InvalidArgumentException When $data cannot be converted to JSON */ public function encode($serverKey, $data) { return Factory::getEncryption()->AESEncryptCtr($data, $serverKey, 128); }
/** * Decrypts the encrypted settings and returns the plaintext INI string * * @param string $encrypted The encrypted data * * @return string The decrypted data */ public function decryptSettings($encrypted, $key = null) { if (substr($encrypted, 0, 12) == '###AES128###') { $mode = 'AES128'; } elseif (substr($encrypted, 0, 12) == '###CTR128###') { $mode = 'CTR128'; } else { return $encrypted; } if (empty($key)) { $key = $this->getKey(); } $encrypted = substr($encrypted, 12); switch ($mode) { default: case 'AES128': $encrypted = base64_decode($encrypted); $decrypted = Factory::getEncryption()->AESDecryptCBC($encrypted, $key, 128); break; case 'CTR128': $decrypted = Factory::getEncryption()->AESDecryptCtr($encrypted, $key, 128); break; } return $decrypted; }
/** * Encodes the data. The data is JSON encoded by this method before encapsulation takes place. Encrypted * encapsulations will then encrypt the data and base64-encode it before returning it. * * The data being encoded correspond to the body > data structure described in the API documentation * * @param string $serverKey The server key we need to encode data * @param mixed $data The data to encode, typically a string, array or object * * @return string The encapsulated data * * @see https://www.akeebabackup.com/documentation/json-api/ar01s02s02.html * * @throws \RuntimeException When the server capabilities don't match the requested encapsulation * @throws \InvalidArgumentException When $data cannot be converted to JSON */ public function encode($serverKey, $data) { return base64_encode(Factory::getEncryption()->AESEncryptCBC($data, $serverKey, 256)); }