提交 eaf41c62 编写于 作者: T tianhe1986

版本11密码处理。

Signed-off-by: Ntianhe1986 <w1s2j3229@163.com>
上级 06de0369
......@@ -7,10 +7,15 @@ class NavicatPassword
protected $version = 0;
protected $aesKey = 'libcckeylibcckey';
protected $aesIv = 'libcciv libcciv ';
protected $blowString = '3DC5CA39';
protected $blowKey = null;
protected $blowIv = null;
public function __construct($version = 12)
{
$this->version = $version;
$this->blowKey = sha1('3DC5CA39', true);
$this->blowIv = hex2bin('d9c7c3c8870d64bd');
}
public function encrypt($string)
......@@ -32,8 +37,46 @@ class NavicatPassword
protected function encryptEleven($string)
{
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowIv;
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 encryptBlock($block)
{
return openssl_encrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING);
}
protected function decryptBlock($block)
{
return openssl_decrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING);
}
protected function xorBytes($str1, $str2)
{
$result = '';
for ($i = 0; $i < strlen($str1); $i++) {
$result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
}
return $result;
}
protected function encryptTwelve($string)
{
$result = openssl_encrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
......@@ -57,13 +100,31 @@ class NavicatPassword
return $result;
}
protected function decryptEleven($string)
protected function decryptEleven($upperString)
{
$string = hex2bin(strtolower($upperString));
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowIv;
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($string)
protected function decryptTwelve($upperString)
{
$data = hex2bin(strtolower($string));
return openssl_decrypt($data, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
$string = hex2bin(strtolower($upperString));
return openssl_decrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
}
}
\ No newline at end of file
......@@ -15,5 +15,27 @@ class NavicatPasswordTest extends TestCase
$this->assertEquals('833E4ABBC56C89041A9070F043641E3B', $encrypted);
$decrypted = $navicatPassword->decrypt($encrypted);
$this->assertEquals($string, $decrypted);
$string = 'This is a test';
$encrypted = $navicatPassword->encrypt($string);
$this->assertEquals('B75D320B6211468D63EB3B67C9E85933', $encrypted);
$decrypted = $navicatPassword->decrypt($encrypted);
$this->assertEquals($string, $decrypted);
}
public function testHandleEleven()
{
$navicatPassword = new NavicatPassword(11);
$string = '123456';
$encrypted = $navicatPassword->encrypt($string);
$this->assertEquals('15057D7BA390', $encrypted);
$decrypted = $navicatPassword->decrypt($encrypted);
$this->assertEquals($string, $decrypted);
$string = 'This is a test';
$encrypted = $navicatPassword->encrypt($string);
$this->assertEquals('0EA71F51DD37BFB60CCBA219BE3A', $encrypted);
$decrypted = $navicatPassword->decrypt($encrypted);
$this->assertEquals($string, $decrypted);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册