<?php
class NavicatPassword
{
protected $version = 0;
protected $aesKey = 'libcckeylibcckey';
protected $aesIv = 'libcciv libcciv ';
protected $blowfishKey = '3DC5CA39';
protected $blowfishIv = 'x\l4H]zy';
public function __construct($version = 12)
{
$this->version = $version;
}
public function encrypt($string)
{
$result = FALSE;
switch ($this->version) {
case 11:
$result = $this->encryptEleven($string);
break;
case 12:
$result = $this->encryptTwelve($string);
break;
default:
break;
}
return $result;
}
public function decrypt($string)
{
$result = FALSE;
switch ($this->version) {
case 11:
$result = $this->decryptEleven($string);
break;
case 12:
$result = $this->decryptTwelve($string);
break;
default:
break;
}
return $result;
}
protected function encryptEleven($string)
{
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowfishIv;
for ($i = 0; $i < $round; $i++) {
$temp = $this->encryptBlock($this->xorBytes(substr($string, 8 * $i, 8), $currentVector));
$currentVector = $this->xorBytes($currentVector, $temp);
$result .= $temp;
}
if ($leftLength) {
$currentVector = $this->encryptBlock($currentVector);
$result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
}
return strtoupper(bin2hex($result));
}
protected function encryptTwelve($string)
{
$result = openssl_encrypt(
$string,
'AES-256-CBC',
$this->aesKey,
OPENSSL_RAW_DATA,
$this->aesIv
);
return strtoupper(bin2hex($result));
}
protected function decryptEleven($encryptedString)
{
$string = hex2bin(strtolower($encryptedString));
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowfishIv;
for ($i = 0; $i < $round; $i++) {
$temp = $this->xorBytes($this->decryptBlock(substr($string, 8 * $i, 8)), $currentVector);
$currentVector = $this->xorBytes($currentVector, substr($string, 8 * $i, 8));
$result .= $temp;
}
if ($leftLength) {
$currentVector = $this->encryptBlock($currentVector);
$result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
}
return $result;
}
protected function decryptTwelve($encryptedString)
{
return openssl_decrypt(
hex2bin(strtolower($encryptedString)),
'AES-256-CBC',
$this->aesKey,
OPENSSL_RAW_DATA,
$this->aesIv
);
}
protected function encryptBlock($block)
{
return mcrypt_encrypt(
MCRYPT_BLOWFISH,
$this->blowfishKey,
$block,
MCRYPT_MODE_ECB
);
}
protected function decryptBlock($block)
{
return mcrypt_decrypt(
MCRYPT_BLOWFISH,
$this->blowfishKey,
$block,
MCRYPT_MODE_ECB
);
}
protected function xorBytes($str1, $str2)
{
$result = '';
for ($i = 0; $i < strlen($str1); $i++) {
$result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
}
return $result;
}
}
// 使用示例 - 修改此处的版本和加密字符串
$version = 12; // 根据你的Navicat版本选择11或12
$encryptedPassword = 'FA1D72628376C076'; // 替换为你导出的加密密码
$navicatPassword = new NavicatPassword($version);
$decryptedPassword = $navicatPassword->decrypt($encryptedPassword);
echo "解密后的密码: " . $decryptedPassword . "\n";
?>