当前位置: 代码网 > it编程>编程语言>Java > 使用Java实现Navicat密码的加密与解密的代码解析

使用Java实现Navicat密码的加密与解密的代码解析

2025年05月21日 Java 我要评论
在日常开发过程中,我们有时需要处理各种软件保存的凭据信息,比如数据库连接密码等。这篇文章将介绍如何使用java对navicat保存的数据库密码进行加密和解密。一、背景介绍navicat是一款强大的数据

在日常开发过程中,我们有时需要处理各种软件保存的凭据信息,比如数据库连接密码等。这篇文章将介绍如何使用java对navicat保存的数据库密码进行加密和解密。

一、背景介绍

navicat是一款强大的数据库管理工具,支持多种数据库系统。为了保护用户的数据库凭据,navicat对用户输入的密码进行了加密处理。本文基于navicat 12及以后版本使用的aes加密算法,以及navicat 11及以前版本使用的blowfish加密算法,展示如何通过java代码实现这些密码的加密和解密。

二、环境准备

确保您的开发环境中已经配置好java,并且引入了必要的安全库来支持加密操作。对于本教程,我们将直接使用jdk自带的javax.crypto包。

三、代码解析

加密逻辑

  • aes加密(适用于navicat 12及以上版本):采用固定密钥和初始化向量(iv)。
  • blowfish加密(适用于navicat 11及以下版本):同样使用固定的密钥和iv,但加密过程包括了一步异或操作。

解密逻辑

解密逻辑相对应地分为aes解密和blowfish解密两种情况,具体实现请参见上述代码片段中的decryptaesdecryptblowfish方法。

四、核心代码展示

package com.aigc.admin.controller;
import javax.crypto.cipher;
import javax.crypto.spec.ivparameterspec;
import javax.crypto.spec.secretkeyspec;
import java.nio.charset.standardcharsets;
import java.security.messagedigest;
import java.util.arrays;
/**
 * @author albert_luo@lizipro.cn
 * @version 1.0
 * @description: todo
 * @date 04 apr 2025 00:29
 */
public class navicatpasswordutil {
    public static void main(string[] args) throws exception {
        // 创建 navicatpasswordutil 实例
        navicatpasswordutil passwordutil = new navicatpasswordutil();
        // 待解密的密码字符串
        string encryptedpassword = "30a228e829283fea8540da18d2b6a302";
        // 解密 navicat 12 及以后的版本
        string decryptedpassword = passwordutil.decryptpassword(encryptedpassword, navicatversion.version_12);
        // 正则替换控制符(如响铃、退格等)
        decryptedpassword = decryptedpassword.replaceall("\\p{cntrl}", "");
        // 输出解密后的明文 结果为 shiguang
        system.out.println("解密后的密码: " + decryptedpassword);
    }
    // aes 加密密钥
    private static final string aes_key = "libcckeylibcckey";
    // aes 加密向量
    private static final string aes_iv = "libcciv libcciv ";
    // blowfish 加密密钥
    private static final string blowfish_key = "3dc5ca39";
    // blowfish 加密向量
    private static final string blowfish_iv = "d9c7c3c8870d64bd";
    /**
     * 加密密码
     *
     * @param plaintextpassword 明文密码
     * @param navicatversion    加密版本(navicatversion.version_11 或 navicatversion.version_12)
     * @return 加密后的密文密码
     * @throws exception 加密过程中可能抛出的异常
     */
    public string encryptpassword(string plaintextpassword, navicatversion navicatversion) throws exception {
        switch (navicatversion) {
            case version_11:
                return encryptblowfish(plaintextpassword);
            case version_12:
                return encryptaes(plaintextpassword);
            default:
                throw new illegalargumentexception("不支持的 navicat 版本");
        }
    }
    /**
     * 解密密码
     *
     * @param encryptedpassword 密文密码
     * @param navicatversion    解密版本(navicatversion.version_11 或 navicatversion.version_12)
     * @return 解密后的明文密码
     * @throws exception 解密过程中可能抛出的异常
     */
    public string decryptpassword(string encryptedpassword, navicatversion navicatversion) throws exception {
        switch (navicatversion) {
            case version_11:
                return decryptblowfish(encryptedpassword);
            case version_12:
                return decryptaes(encryptedpassword);
            default:
                throw new illegalargumentexception("不支持的 navicat 版本");
        }
    }
    /**
     * 使用 blowfish 加密密码(适用于 navicat 11 及以前的版本)
     *
     * @param plaintextpassword 明文密码
     * @return 加密后的密文密码
     * @throws exception 加密过程中可能抛出的异常
     */
    private string encryptblowfish(string plaintextpassword) throws exception {
        byte[] iv = hexstringtobytearray(blowfish_iv);
        byte[] key = hashtobytes(blowfish_key);
        int round = plaintextpassword.length() / 8;
        int leftlength = plaintextpassword.length() % 8;
        stringbuilder encryptedresult = new stringbuilder();
        byte[] currentvector = iv.clone();
        cipher cipher = cipher.getinstance("blowfish/ecb/nopadding");
        secretkeyspec secretkeyspec = new secretkeyspec(key, "blowfish");
        cipher.init(cipher.encrypt_mode, secretkeyspec);
        for (int i = 0; i < round; i++) {
            byte[] block = xorbytes(plaintextpassword.substring(i * 8, (i + 1) * 8).getbytes(), currentvector);
            byte[] encryptedblock = cipher.dofinal(block);
            currentvector = xorbytes(currentvector, encryptedblock);
            encryptedresult.append(bytestohex(encryptedblock));
        }
        if (leftlength > 0) {
            currentvector = cipher.dofinal(currentvector);
            byte[] block = xorbytes(plaintextpassword.substring(round * 8).getbytes(), currentvector);
            encryptedresult.append(bytestohex(block));
        }
        return encryptedresult.tostring().touppercase();
    }
    /**
     * 使用 aes 加密密码(适用于 navicat 12 及以后的版本)
     *
     * @param plaintextpassword 明文密码
     * @return 加密后的密文密码
     * @throws exception 加密过程中可能抛出的异常
     */
    private string encryptaes(string plaintextpassword) throws exception {
        byte[] iv = aes_iv.getbytes();
        byte[] key = aes_key.getbytes();
        cipher cipher = cipher.getinstance("aes/cbc/nopadding");
        secretkeyspec secretkeyspec = new secretkeyspec(key, "aes");
        ivparameterspec ivparameterspec = new ivparameterspec(iv);
        cipher.init(cipher.encrypt_mode, secretkeyspec, ivparameterspec);
        byte[] encryptedresult = cipher.dofinal(plaintextpassword.getbytes());
        return bytestohex(encryptedresult).touppercase();
    }
    /**
     * 使用 blowfish 解密密码(适用于 navicat 11 及以前的版本)
     *
     * @param encryptedpassword 密文密码
     * @return 解密后的明文密码
     * @throws exception 解密过程中可能抛出的异常
     */
    private string decryptblowfish(string encryptedpassword) throws exception {
        byte[] iv = hexstringtobytearray(blowfish_iv);
        byte[] key = hashtobytes(blowfish_key);
        byte[] encryptedbytes = hexstringtobytearray(encryptedpassword.tolowercase());
        int round = encryptedbytes.length / 8;
        int leftlength = encryptedbytes.length % 8;
        stringbuilder decryptedresult = new stringbuilder();
        byte[] currentvector = iv.clone();
        cipher cipher = cipher.getinstance("blowfish/ecb/nopadding");
        secretkeyspec secretkeyspec = new secretkeyspec(key, "blowfish");
        cipher.init(cipher.decrypt_mode, secretkeyspec);
        for (int i = 0; i < round; i++) {
            byte[] encryptedblock = arrays.copyofrange(encryptedbytes, i * 8, (i + 1) * 8);
            byte[] decryptedblock = xorbytes(cipher.dofinal(encryptedblock), currentvector);
            currentvector = xorbytes(currentvector, encryptedblock);
            decryptedresult.append(new string(decryptedblock));
        }
        if (leftlength > 0) {
            currentvector = cipher.dofinal(currentvector);
            byte[] block = arrays.copyofrange(encryptedbytes, round * 8, round * 8 + leftlength);
            decryptedresult.append(new string(xorbytes(block, currentvector), standardcharsets.utf_8));
        }
        return decryptedresult.tostring();
    }
    /**
     * 使用 aes 解密密码(适用于 navicat 12 及以后的版本)
     *
     * @param encryptedpassword 密文密码
     * @return 解密后的明文密码
     * @throws exception 解密过程中可能抛出的异常
     */
    private string decryptaes(string encryptedpassword) throws exception {
        byte[] iv = aes_iv.getbytes();
        byte[] key = aes_key.getbytes();
        byte[] encryptedbytes = hexstringtobytearray(encryptedpassword.tolowercase());
        cipher cipher = cipher.getinstance("aes/cbc/nopadding");
        secretkeyspec secretkeyspec = new secretkeyspec(key, "aes");
        ivparameterspec ivparameterspec = new ivparameterspec(iv);
        cipher.init(cipher.decrypt_mode, secretkeyspec, ivparameterspec);
        byte[] decryptedresult = cipher.dofinal(encryptedbytes);
        return new string(decryptedresult);
    }
    /**
     * 对两个字节数组进行异或操作
     *
     * @param bytes1 第一个字节数组
     * @param bytes2 第二个字节数组
     * @return 异或结果字节数组
     */
    private static byte[] xorbytes(byte[] bytes1, byte[] bytes2) {
        byte[] result = new byte[bytes1.length];
        for (int i = 0; i < bytes1.length; i++) {
            result[i] = (byte) (bytes1[i] ^ bytes2[i]);
        }
        return result;
    }
    /**
     * 将十六进制字符串转换为字节数组
     *
     * @param hexstring 十六进制字符串
     * @return 字节数组
     */
    private static byte[] hexstringtobytearray(string hexstring) {
        int len = hexstring.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((character.digit(hexstring.charat(i), 16) << 4) + character.digit(hexstring.charat(i + 1), 16));
        }
        return data;
    }
    /**
     * 将字符串哈希为字节数组
     *
     * @param inputstring 输入字符串
     * @return 哈希后的字节数组
     * @throws exception 哈希过程中可能抛出的异常
     */
    private static byte[] hashtobytes(string inputstring) throws exception {
        return messagedigest.getinstance("sha-1").digest(inputstring.getbytes());
    }
    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param bytearray 字节数组
     * @return 十六进制字符串
     */
    private static string bytestohex(byte[] bytearray) {
        stringbuilder result = new stringbuilder();
        for (byte b : bytearray) {
            result.append(string.format("%02x", b));
        }
        return result.tostring();
    }
}
/**
 * navicat 版本枚举
 */
enum navicatversion {
    version_11,
    version_12
}
 

五、总结

通过本文,我们了解了如何利用java语言实现对navicat保存的数据库密码进行加密和解密。这不仅有助于加深对加密技术的理解,同时也为实际项目中处理类似需求提供了参考方案。需要注意的是,在真实的应用场景中,应当遵循最佳的安全实践,如定期更换密钥、避免硬编码敏感信息等。

希望这篇博客能帮助读者更好地理解和应用java中的加密技术。如果想要深入了解,建议尝试修改并运行上述代码,观察不同参数设置下的输出结果。

到此这篇关于使用java实现navicat密码的加密与解密的代码解析的文章就介绍到这了,更多相关java navicat密码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com