key = $this->hex2bin(md5($key)); $this->createIvBytes(); } /** * Generates the iv out of the preshared key * * @return string * */ private function createIvBytes() { $this->iv = base64_decode("HeY8YU0ykRqc9gB7Qp1+kQ=="); } /** * Encrypt the given string, care about base64 encoding and other needed stuff * * @param string $str * * @return string * */ public function encryptString($str) { if ($str === '') { return ''; } $method = 'AES-128-CFB8'; $encrypted = openssl_encrypt($str, $method, $this->key, OPENSSL_RAW_DATA, $this->iv); return str_replace(array('+', '/'), array('-', '_'), base64_encode($encrypted)); } /** * Transform hex to binary data * * @param string $hexdata * * @return string * */ private function hex2bin($hexdata) { $bindata = ''; for ($i = 0; $i < strlen($hexdata); $i += 2) $bindata .= chr(hexdec(substr($hexdata, $i, 2))); return $bindata; } }