方法

首先确定Navicat的版本,是11还是12

如果是11

win+R 输入regedit打开注册表管理器
找到HKEY_CURRENT_USER\SOFTWARE\PremiumSoft\Navicat\Servers\
2024-01-23T13:05:56.png
找到你要查看密码的连接,复制PWD的值。

如果是12

文件 > 导出连接
勾选导出密码
2024-01-23T13:09:40.png
然后用记事本打开connections.ncx,搜索Password=
复制password双引号里面的值

解密脚本(我发现网上大部分教程都没有给这个代码添加注释,那我就给这个代码加一下注释吧,方便大家理解)

<?php
namespace FatSmallTools;

class NavicatPassword
{
    // Navicat版本号
    protected $version = 0;

    // 版本12的AES加密密钥和初始化向量
    protected $aesKey = 'libcckeylibcckey';
    protected $aesIv = 'libcciv libcciv ';

    // Blowfish加密所需的常量、密钥和初始化向量
    protected $blowString = '3DC5CA39';
    protected $blowKey = null;
    protected $blowIv = null;

    // 构造函数,设置Navicat版本和Blowfish的密钥和初始化向量
    public function __construct($version = 12)
    {
        $this->version = $version;
        $this->blowKey = sha1('3DC5CA39', true);
        $this->blowIv = hex2bin('d9c7c3c8870d64bd');
    }

    // 加密方法,根据版本调用对应的加密方法
    public function encrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->encryptEleven($string);  // 版本11的加密
                break;
            case 12:
                $result = $this->encryptTwelve($string);  // 版本12的加密
                break;
            default:
                break;
        }
        return $result;
    }

    // 版本11的加密方法
    protected function encryptEleven($string)
    {
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;

        // 逐个加密8字节块
        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));
    }

    // Blowfish加密单个块
    protected function encryptBlock($block)
    {
        return openssl_encrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
    }

    // AES加密方法
    protected function encryptTwelve($string)
    {
        $result = openssl_encrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
        return strtoupper(bin2hex($result));
    }

    // 解密方法,根据版本调用对应的解密方法
    public function decrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->decryptEleven($string);  // 版本11的解密
                break;
            case 12:
                $result = $this->decryptTwelve($string);  // 版本12的解密
                break;
            default:
                break;
        }
        return $result;
    }

    // 版本11的解密方法
    protected function decryptEleven($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;

        // 逐个解密8字节块
        for ($i = 0; $i < $round; $i++) {
            $encryptedBlock = substr($string, 8 * $i, 8);
            $temp = $this->xorBytes($this->decryptBlock($encryptedBlock), $currentVector);
            $currentVector = $this->xorBytes($currentVector, $encryptedBlock);
            $result .= $temp;
        }

        // 处理剩余的字节
        if ($leftLength) {
            $currentVector = $this->encryptBlock($currentVector);
            $result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
        }

        return $result;
    }

    // 解密Blowfish加密的块
    protected function decryptBlock($block)
    {
        return openssl_decrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
    }

    // 版本12的解密方法
    protected function decryptTwelve($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        return openssl_decrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
    }

    // 执行异或操作
    protected function xorBytes($str1, $str2)
    {
        $result = '';
        for ($i = 0; $i < strlen($str1); $i++) {
            $result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
        }
        return $result;
    }
}

use FatSmallTools\NavicatPassword;

// 实例化NavicatPassword类,选择指定版本,现在为12(有时候11版本会输出乱码,建议能12版本解密就12版本)
$navicatPassword = new NavicatPassword(12);

// 解密
$decode = $navicatPassword->decrypt('加密密码值');
echo $decode . "\n";

最后随便找一个PHP在线运行网站即可
2024-01-23T13:23:20.png

文章目录