String file = “C:\Users\test\AppData\Roaming\DBeaverData\workspace6\mydeaver\.dbeaver\”;
填写自己对应的路径
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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;
import java.util.Map;
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 {
String file = "C:\\Users\\test\\AppData\\Roaming\\DBeaverData\\workspace6\\mydeaver\\.dbeaver\\";
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(file+"credentials-config.json"));
byte[] credentialsConfigBytesPlain = encryptor.decryptValue(credentialsConfigBytesSecret);
String password = new String(credentialsConfigBytesPlain);
String data_sources = new String(Files.readAllBytes(Paths.get(file+"data-sources.json")));
JSONObject pass_obj = JSON.parseObject(password);
JSONObject data_obj = JSON.parseObject(data_sources);
JSONObject data_conn_obj = (JSONObject) data_obj.get("connections");
System.out.println("---------------------------");
for (String key : pass_obj.keySet()) {
JSONObject pass_conn_obj = (JSONObject) pass_obj.get(key);
JSONObject data_all_obj = (JSONObject)data_conn_obj.get(key);
JSONObject data_conf_obj = (JSONObject)data_all_obj.get("configuration");
data_conf_obj.putAll((Map<? extends String, ?>) pass_conn_obj.get("#connection"));
System.out.println(data_all_obj);
System.out.println("---------------------------");
}
}
}
输出结果: