/** * @param null|string $input * @return string */ public function generateKey($input = NULL) { $text = $input === NULL ? $this->username . ';' . date('d/m/Y H:i:s') . ';' . $this->ip : $input; $cipher = new \Crypt_Rijndael(); $cipher->setKeyLength(256); $cipher->setBlockLength(128); $cipher->setKey(base64_decode($this->key)); $cipher->setIV(base64_decode($this->iv)); return base64_encode($cipher->encrypt($text)); }
/** * Initilizes cryptographic scheme */ private static function init() { if (is_null(self::$cryptographicScheme)) { $key = KeyHandler::readKey(); $mysqlKey = ""; for ($a = 0; $a < strlen($key); $a++) { $mysqlKey[$a % 16] = chr(ord($mysqlKey[$a % 16]) ^ ord($key[$a])); } $aes = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_ECB); $aes->setKeyLength(128); $aes->setBlockLength(128); $aes->setKey($mysqlKey); self::$cryptographicScheme = $aes; } }
/** * Sets the key length * * Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. * * @see Crypt_Rijndael:setKeyLength() * @access public * @param Integer $length */ function setKeyLength($length) { switch ($length) { case 160: $length = 192; break; case 224: $length = 256; } parent::setKeyLength($length); }
/** * * Encrypts given value, with given key, and hex encodes it before * returning. * * Compatible with mysql: "hex(aes_encrypt($val, $key)) * * @param string $val - value to encrypt * @param string $ky - key * @return string encrypted value */ public function encrypt($val, $key) { if (empty($val)) { return $val; } $mysqlKey = ""; for ($a = 0; $a < strlen($key); $a++) { $mysqlKey[$a % 16] = chr(ord($mysqlKey[$a % 16]) ^ ord($key[$a])); } $aes = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_ECB); $aes->setKeyLength(128); $aes->setBlockLength(128); $aes->setKey($mysqlKey); $encrypt = $aes->encrypt($val); $encrypt = strtoupper(bin2hex($encrypt)); return $encrypt; }