/** * Encrypts the settings using the automatically detected preferred algorithm * * @param $settingsINI string The raw settings INI string * * @return string The encrypted data to store in the database */ public static function encryptSettings($settingsINI, $key = null) { // Do we really support encryption? if (!self::supportsEncryption()) { return $settingsINI; } // Does any of the preferred encryption engines exist? $encryption = self::preferredEncryption(); if (empty($encryption)) { return $settingsINI; } // Do we have a non-empty key to begin with? if (empty($key)) { $key = self::getKey(); } if (empty($key)) { return $settingsINI; } if ($encryption == 'AES128') { $encrypted = AEUtilEncrypt::AESEncryptCBC($settingsINI, $key, 128); if (empty($encrypted)) { $encryption = 'CTR128'; } else { // Note: CBC returns the encrypted data as a binary string and requires Base 64 encoding $settingsINI = '###AES128###' . base64_encode($encrypted); } } if ($encryption == 'CTR128') { $encrypted = AEUtilEncrypt::AESEncryptCtr($settingsINI, $key, 128); if (empty($encrypted)) { $encryption = ''; } else { // Note: CTR returns the encrypted data readily encoded in Base 64 $settingsINI = '###CTR128###' . $encrypted; } } return $settingsINI; }
/** * 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)); switch ($this->method_name) { case 'Download': $data = json_encode($this->data); break; default: $data = $this->json_encode($this->data); break; } if (empty($this->password)) { $this->encapsulation = self::ENCAPSULATION_RAW; } switch ($this->encapsulation) { case self::ENCAPSULATION_RAW: break; case self::ENCAPSULATION_AESCTR128: $data = AEUtilEncrypt::AESEncryptCtr($data, $this->password, 128); break; case self::ENCAPSULATION_AESCTR256: $data = AEUtilEncrypt::AESEncryptCtr($data, $this->password, 256); break; case self::ENCAPSULATION_AESCBC128: $data = base64_encode(AEUtilEncrypt::AESEncryptCBC($data, $this->password, 128)); break; case self::ENCAPSULATION_AESCBC256: $data = base64_encode(AEUtilEncrypt::AESEncryptCBC($data, $this->password, 256)); break; } $response['body']['data'] = $data; switch ($this->method_name) { case 'Download': return '###' . json_encode($response) . '###'; break; default: return '###' . $this->json_encode($response) . '###'; break; } }