资讯动态

Navicat密码找回神器:用Hutool解密导出的数据库连接文件(附完整Java代码)

发布时间:2026/8/14 15:23:55 来源:尧图企业网站定制
Navicat连接密码解密实战基于Hutool的自动化解决方案每次接手遗留项目时最头疼的莫过于发现Navicat里保存的数据库连接密码早已遗忘。作为开发者我们经常需要迁移或备份数据库连接配置但Navicat默认加密存储的密码却成了拦路虎。本文将带你深入Navicat连接文件的加密机制并手把手实现一个基于Hutool工具包的密码解密工具。1. Navicat连接文件解析基础Navicat作为主流的数据库管理工具会将所有连接配置以加密形式存储在connections.ncx文件中。这个XML格式的文件位于用户配置目录下通常路径为Windows: C:\Users\[用户名]\AppData\Roaming\Navicat\Navicat Premium\connections.ncx macOS: ~/Library/Application Support/PremiumSoft/Navicat Premium/connections.ncx该文件采用AES-CBC模式加密存储密码核心加密参数如下参数值加密算法AES-CBC密钥libcckeylibcckey初始向量IVlibcciv libcciv 填充模式PKCS7Padding注意不同Navicat版本可能使用不同的加密参数本文方案适用于12-15版本2. 开发环境准备2.1 依赖配置我们需要以下Java库来实现解密功能dependencies !-- Hutool全能工具包 -- dependency groupIdcn.hutool/groupId artifactIdhutool-all/artifactId version5.8.3/version /dependency !-- BouncyCastle加密库 -- dependency groupIdorg.bouncycastle/groupId artifactIdbcprov-jdk15to18/artifactId version1.68/version /dependency !-- DOM4J XML解析 -- dependency groupIdorg.dom4j/groupId artifactIddom4j/artifactId version2.1.3/version /dependency /dependencies2.2 核心工具类设计我们创建一个NavicatPasswordDecoder工具类主要功能包括读取connections.ncx文件解析XML结构解密密码字段输出完整连接信息3. 完整实现代码解析import cn.hutool.core.io.FileUtil; import cn.hutool.crypto.symmetric.AES; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.nio.charset.StandardCharsets; import java.util.Iterator; public class NavicatPasswordDecoder { private static final String AES_KEY libcckeylibcckey; private static final String AES_IV libcciv libcciv ; public static void main(String[] args) { if (args.length 0) { System.err.println(请指定connections.ncx文件路径); return; } String filePath args[0]; try { parseConnectionsFile(filePath); } catch (Exception e) { System.err.println(解析失败: e.getMessage()); e.printStackTrace(); } } public static void parseConnectionsFile(String filePath) throws DocumentException { String xmlContent FileUtil.readString(filePath, StandardCharsets.UTF_8); Document document DocumentHelper.parseText(xmlContent); Element root document.getRootElement(); IteratorElement connIter root.elementIterator(Connection); while (connIter.hasNext()) { Element connection connIter.next(); processConnection(connection); } } private static void processConnection(Element connection) { String connName connection.attributeValue(ConnectionName); String host connection.attributeValue(Host); String port connection.attributeValue(Port); String username connection.attributeValue(UserName); String encryptedPwd connection.attributeValue(Password); AES aes new AES(CBC, PKCS7Padding, AES_KEY.getBytes(), AES_IV.getBytes()); String password aes.decryptStr(encryptedPwd); System.out.println( 连接信息 ); System.out.println(名称: connName); System.out.println(主机: host); System.out.println(端口: port); System.out.println(用户名: username); System.out.println(密码: password); System.out.println(); } }4. 常见问题与解决方案4.1 运行时报错处理问题1NoSuchMethodErrorException in thread main java.lang.NoSuchMethodError: org.bouncycastle.crypto.engines.AESEngine.processBlock([BI[BI)I解决方案检查BouncyCastle版本冲突确保使用1.68版本问题2XML解析失败org.dom4j.DocumentException: Error on line 1 of document : 前言中不允许有内容。解决方案检查文件编码确保使用UTF-8读取String xmlContent FileUtil.readString(filePath, StandardCharsets.UTF_8);4.2 多版本兼容性不同Navicat版本可能使用不同的加密参数。如果上述方案无效可以尝试以下组合版本密钥IV12-15libcckeylibcckeylibcciv libcciv 旧版本navicatnavicatnavicatnavicat4.3 安全注意事项解密后的密码应妥善保存不要明文存储在代码或配置文件中建议将connections.ncx文件权限设置为仅当前用户可读生产环境建议使用专业的密码管理工具而非依赖Navicat存储5. 功能扩展与优化5.1 批量导出连接信息我们可以将解密结果导出为CSV格式方便后续管理public static void exportToCsv(ListConnectionInfo connections, String outputPath) { StringBuilder csv new StringBuilder(); csv.append(名称,主机,端口,用户名,密码\n); for (ConnectionInfo conn : connections) { csv.append(String.format(\%s\,\%s\,\%s\,\%s\,\%s\\n, conn.getName(), conn.getHost(), conn.getPort(), conn.getUsername(), conn.getPassword())); } FileUtil.writeString(csv.toString(), outputPath, StandardCharsets.UTF_8); }5.2 图形界面实现对于非技术用户可以开发简单的Swing界面public class DecryptGUI extends JFrame { private JTextField filePathField; private JTextArea resultArea; public DecryptGUI() { // 初始化UI组件 setLayout(new BorderLayout()); JPanel topPanel new JPanel(); topPanel.add(new JLabel(NCX文件路径:)); filePathField new JTextField(30); topPanel.add(filePathField); JButton browseBtn new JButton(浏览); browseBtn.addActionListener(e - chooseFile()); topPanel.add(browseBtn); add(topPanel, BorderLayout.NORTH); resultArea new JTextArea(15, 50); add(new JScrollPane(resultArea), BorderLayout.CENTER); JButton decryptBtn new JButton(解密); decryptBtn.addActionListener(e - decryptFile()); add(decryptBtn, BorderLayout.SOUTH); } private void chooseFile() { JFileChooser chooser new JFileChooser(); if (chooser.showOpenDialog(this) JFileChooser.APPROVE_OPTION) { filePathField.setText(chooser.getSelectedFile().getPath()); } } private void decryptFile() { try { String result NavicatPasswordDecoder.parseToString(filePathField.getText()); resultArea.setText(result); } catch (Exception ex) { JOptionPane.showMessageDialog(this, 解密失败: ex.getMessage()); } } }5.3 命令行工具封装将核心功能封装为可执行JAR支持命令行操作java -jar navicat-decrypt.jar /path/to/connections.ncx可以添加更多实用参数# 指定输出格式 java -jar navicat-decrypt.jar -f csv -o output.csv connections.ncx # 只解密特定连接 java -jar navicat-decrypt.jar -n 生产数据库 connections.ncx6. 实际应用场景6.1 团队协作配置共享当需要将数据库连接配置共享给团队成员时可以导出connections.ncx文件使用工具解密后生成配置文档通过安全渠道分享给团队成员6.2 服务器迁移迁移到新服务器时可以备份原connections.ncx文件在新服务器上安装Navicat解密获取所有连接密码在新服务器上重新配置连接6.3 自动化部署集成在CI/CD流程中可以通过命令行工具自动获取数据库连接信息# 获取测试数据库密码 TEST_DB_PWD$(java -jar navicat-decrypt.jar -n 测试环境 -q connections.ncx)7. 安全增强建议虽然这个工具很实用但数据库安全至关重要。建议定期轮换数据库密码使用最小权限原则分配数据库账号考虑使用Vault等专业密码管理工具对connections.ncx文件进行加密备份实现一个简单的备份加密public static void backupWithEncryption(String sourcePath, String backupPath) { String content FileUtil.readString(sourcePath, StandardCharsets.UTF_8); AES aes new AES(CBC, PKCS7Padding, backupkey12345678.getBytes(), backupiv12345678.getBytes()); String encrypted aes.encryptBase64(content); FileUtil.writeString(encrypted, backupPath, StandardCharsets.UTF_8); }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价