safedb 디버깅

This commit is contained in:
Rinjae
2025-09-25 20:23:27 +09:00
parent f2f27cdb42
commit 0407747bdd
3 changed files with 22 additions and 10 deletions
@@ -1,23 +1,29 @@
package com.eactive.ext.kjb.safedb;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
/**
* SafeDBFaker provides a fake encryption method for testing purposes when the real SafeDB library is not available.
*/
@Slf4j
public abstract class SafeDBFaker {
private static final String AES_256_KEY = "1234567890123456" + "1234567890123456";
private static final byte[] IV = "1234567890123456".getBytes();
public byte[] encrypt(String userId, String table, String column, byte[] data) {
if (SafeDBColumns.PSWD.isEqualsIgnoreCase(column)) {
// SHA256 + Base64 인코딩된 가짜 비밀번호 반환
return sha256(data);
//return sha256Base64(data);
} else {
// AES256 암호화된 가짜 데이터 반환
return aes256Encrypt(data);
@@ -51,9 +57,9 @@ public abstract class SafeDBFaker {
// AES256 암호화
private byte[] aes256Encrypt(byte[] data) {
try {
Cipher cipher = javax.crypto.Cipher.getInstance("AES");
Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new javax.crypto.spec.SecretKeySpec(AES_256_KEY.getBytes(), "AES");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, keySpec);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(IV));
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException("Error during AES encryption", e);
@@ -66,20 +72,23 @@ public abstract class SafeDBFaker {
private byte[] aes256Decrypt(byte[] encryptedData) {
try {
Cipher cipher = javax.crypto.Cipher.getInstance("AES");
Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new javax.crypto.spec.SecretKeySpec(AES_256_KEY.getBytes(), "AES");
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(IV));
log.debug("encryptedData : {}", Arrays.toString(encryptedData));
log.debug("encryptedData toString() : {}", new String(encryptedData));
return cipher.doFinal(encryptedData);
} catch (Exception e) {
throw new RuntimeException("Error during AES decryption", e);
}
}
public String encryptString(String safedb, String s, String notRnno, String data) {
return new String(this.encrypt(safedb, s, notRnno, data.getBytes()));
public String encryptString(String userId, String table, String column, String data) {
// return new String(this.encrypt(safedb, s, notRnno, data.getBytes()));
return Base64Utils.encodeToString(this.encrypt(userId, table, column, data.getBytes()));
}
protected String decryptString(String userId, String table, String column, String data) {
return new String(this.decrypt(userId, table, column, data.getBytes()));
return new String(this.decrypt(userId, table, column, Base64Utils.decodeFromString(data)));
}
}
@@ -8,7 +8,6 @@ import org.springframework.data.repository.init.ResourceReader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Base64;
@Slf4j
public class SafeDBWrapper extends SafeDBBroker {
@@ -104,7 +103,7 @@ public class SafeDBWrapper extends SafeDBBroker {
throw new SafeDBWrapperSdkException("[SafeDBWrapper] Encrypt error: " + e.getMessage(), e);
}
} else {
return this.encryptString(userId, tables, column, data);
return super.encryptString(userId, tables, column, data);
}
}
@@ -42,14 +42,18 @@ public class SafeDbWrapperTest {
for (int len = 1; len <= 255; len++) {
String originalData = repeat("A", len); // JDK8 호환 반복 문자열
try {
log.debug("OriginalData: {}", originalData);
// 암호화 (Base64 인코딩 결과 가정)
String encryptedData = wrapper.encryptNotRnnoString(originalData);
log.debug("Encrypted Data: {}", encryptedData);
// 예상 암호문 길이 계산
int expectedLen = getExpectedCipherLength(len, true);
// 복호화
String decryptedData = wrapper.decryptNotRnno(encryptedData);
log.debug("Decrypted Data: {}", decryptedData);
assertEquals(originalData, decryptedData, "Decrypted data does not match original at plain length=" + len);
// 로그 출력