Skip to content

Commit

Permalink
版本11密码处理。
Browse files Browse the repository at this point in the history
Signed-off-by: tianhe1986 <[email protected]>
  • Loading branch information
tianhe1986 committed Nov 30, 2018
1 parent 06de036 commit eaf41c6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
69 changes: 65 additions & 4 deletions src/NavicatPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -32,8 +37,46 @@ public function encrypt($string)

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);
Expand All @@ -57,13 +100,31 @@ public function decrypt($string)
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);
}
}
22 changes: 22 additions & 0 deletions tests/NavicatPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,27 @@ public function testHandleTwelve()
$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);
}
}

0 comments on commit eaf41c6

Please sign in to comment.