找回存储在DBeaver连接中的数据库密码
一、拿到 credentials-config.json 文件
1、打开 Dbeaver 后,点击 “窗口 — 首选项”
2、找到worksapce path
3、进入 workspace path 的文件夹,再进入到 General.dbeaver 文件夹,找到文件 credentials-config.json (可以备份一下这个文件,万一不小心改了内容)。
二、对 credentials-config.json 文件解码
1、方法一:
如果你有安装 ubuntu、centos 等这些 linux 操作系统,并且系统上安装了 openssl,则可以使用 openssl 对credentials-config.json文件解码(以 centos 系统为例):
(1)先把文件复制到 centos 系统某个目录下
(2)还是在这个目录下,使用如下命令
openssl aes-128-cbc -d
-K babb4a9f774ab853c96c2d653dfe544a
-iv 00000000000000000000000000000000
-in credentials-config.json |
dd bs=1 skip=16 2>/dev/null
(3)命令执行后,就得到解码后的json字符串(为了方便查看 json 串,可以借助工具 https://www.json.cn/ 查看)
2、方法二:
如果没有 linux 系统和 openssl ,可以用 windows 系统和 java
(1)在某个文件夹新建文件 DefaultValueEncryptor.txt ,把下面这段代码粘贴进去保存。
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class DefaultValueEncryptor {
public static final String CIPHER_NAME = "AES/CBC/PKCS5Padding";
public static final String KEY_ALGORITHM = "AES";
private final SecretKey secretKey;
private final Cipher cipher;
public DefaultValueEncryptor(SecretKey secretKey) {
this.secretKey = secretKey;
try {
this.cipher = Cipher.getInstance(CIPHER_NAME);
} catch (Exception e) {
System.out.println("Internal error during encrypted init" + e);
throw new RuntimeException(e);
}
}
public byte[] decryptValue(byte[] value) {
try (InputStream byteStream = new ByteArrayInputStream(value)) {
byte[] fileIv = new byte[16];
byteStream.read(fileIv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
try (CipherInputStream cipherIn = new CipherInputStream(byteStream, cipher)) {
ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();
int bufferSize = 100;
byte[] buffer = new byte[bufferSize];
while ((bufferSize = cipherIn.read(buffer)) != -1) {
resultBuffer.write(buffer, 0,bufferSize);
}
return resultBuffer.toByteArray();
}
} catch (Exception e) {
System.out.println("Error decrypting value" + e);
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("plese input param1: full path to your credentials-config.json file");
System.exit(1);
}
final byte[] LOCAL_KEY_CACHE = new byte[]{-70, -69, 74, -97, 119, 74, -72, 83, -55, 108, 45, 101, 61, -2, 84, 74};
SecretKey aes = new SecretKeySpec(LOCAL_KEY_CACHE, KEY_ALGORITHM);
DefaultValueEncryptor encryptor = new DefaultValueEncryptor(aes);
byte[] credentialsConfigBytesSecret = Files.readAllBytes(Paths.get(args[0]));
byte[] credentialsConfigBytesPlain = encryptor.decryptValue(credentialsConfigBytesSecret);
System.out.println(new String(credentialsConfigBytesPlain));
}
}
(2)然后再把文件名的后缀从 .txt 改为 .java ,并把之前的 credentials-config.json 文件也复制到同个目录下。
(3)cmd 命令行进入到该目录,然后依次执行命令 ,即可。
javac DefaultValueEncryptor.java
java DefaultValueEncryptor credentials-config.json