public String decryptOrPlain(AWSKMS kms, String input) {
// 1) base64 decode 먼저 시도 (빠르고 로컬이므로 비용 없음)
byte[] decoded;
try {
decoded = Base64.getDecoder().decode(input);
} catch (IllegalArgumentException e) {
// Base64가 아님 → 평문
return input;
}
// 2) KMS 복호화 시도
try {
DecryptRequest request = new DecryptRequest()
.withCiphertextBlob(ByteBuffer.wrap(decoded));
ByteBuffer plainBytes = kms.decrypt(request).getPlaintext();
return StandardCharsets.UTF_8.decode(plainBytes).toString();
} catch (cohttp://m.amazonaws.services.kms.model.InvalidCiphertextException e) {
// "이건 암호문이 아니다" 라고 AWS가 확정해주는 예외
return input;
} catch (Exception e) {
// 나머지는 실제 장애 → 평문으로 오인하면 절대 안 됨
throw new RuntimeException("KMS decrypt failed", e);
}
}



