Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(unit-test): use blowfish encryption algorithm to replace aes #443

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Base64;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.exception.ExceptionUtils;

import com.oceanbase.tools.dbbrowser.util.StringUtils;

import lombok.NonNull;
Expand All @@ -40,7 +47,7 @@ public abstract class BasePropertiesEnv {
private static final String TEST_CONFIG_FILE = "../../local-unit-test.properties";
private static final String ENCRYPTED_PREFIX = "ENC@";
private static final Properties PROPERTIES = new Properties();
private static final AesBytesEncryptor DECRYPTOR = new AesBytesEncryptor(new SecretKey().getSecretKey(), null, 256);
private static final SecretKey SECRET_KEY = new SecretKey();

static {
try {
Expand Down Expand Up @@ -90,10 +97,36 @@ private static String decryptIfRequired(String value) {
}
value = value.substring(ENCRYPTED_PREFIX.length());
byte[] encrypted = Base64.getDecoder().decode(value.getBytes());
byte[] decrypted = DECRYPTOR.decrypt(encrypted);
byte[] decrypted = decrypt(encrypted, SECRET_KEY.getSecretKey().getBytes());
return new String(decrypted);
}

public static byte[] decrypt(byte[] encrypted, byte[] password) {
Validate.notNull(encrypted, "null input for decrypt");
try {
Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
SecretKeySpec key = new SecretKeySpec(password, "Blowfish");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encrypted);
return zeroUnpad(decrypted);
} catch (Exception e) {
String rootCauseMessage = ExceptionUtils.getRootCauseMessage(e);
throw new RuntimeException(rootCauseMessage);
}
}

private static byte[] zeroUnpad(byte[] decrypted) {
int end = decrypted.length;
while (end > 0) {
if (decrypted[end - 1] == 0) {
end--;
} else {
break;
}
}
return Arrays.copyOf(decrypted, end);
}

private static class SecretKey {

private static final String SECRET_ENV_KEY = "ODC_CONFIG_SECRET";
Expand Down
Loading