import javax.json.*;
public final class WildcardDecryptor {
public JsonValue decrypt(String encrypted) {
// 1. Empty or null → JSON null
if (encrypted == null || encrypted.isEmpty()) {
return JsonValue.NULL;
}
// 2. Try number
try {
long n = Long.parseLong(encrypted);
return Json.createValue(n);
} catch (NumberFormatException ignored) {}
// 3. Try boolean
if (encrypted.equalsIgnoreCase("true")) {
return JsonValue.TRUE;
}
if (encrypted.equalsIgnoreCase("false")) {
return JsonValue.FALSE;
}
// 4. Try JSON object
if (encrypted.startsWith("{") && encrypted.endsWith("}")) {
try (JsonReader reader = Json.createReader(new java.io.StringReader(encrypted))) {
return reader.readObject();
} catch (Exception ignored) {}
}
// 5. Try JSON array
if (encrypted.startsWith("[") && encrypted.endsWith("]")) {
try (JsonReader reader = Json.createReader(new java.io.StringReader(encrypted))) {
return reader.readArray();
} catch (Exception ignored) {}
}
// 6. Fallback: treat as string
return Json.createValue(encrypted);
}
}
public final class JsonValueFactory {
public static JsonValue fromNullable(String value) {
return (value == null) ? JsonValue.NULL : Json.createValue(value);
}
}
WildcardDecryptor decryptor = new WildcardDecryptor();
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("name", decryptor.decrypt("Alice"));
builder.add("age", decryptor.decrypt("42"));
builder.add("active", decryptor.decrypt("true"));
builder.add("meta", decryptor.decrypt("{\"role\":\"admin\"}"));
builder.add("empty", decryptor.decrypt(""));
JsonObject obj = builder.build();
System.out.println(obj);
{
"name": "Alice",
"age": 42,
"active": true,
"meta": { "role": "admin" },
"empty": null
}
import javax.json.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.PrivateKey;
import java.util.Optional;
import java.util.logging.Logger;
public final class WildcardDecryptor {
private static final Logger LOG = Logger.getLogger(WildcardDecryptor.class.getName());
private final SecretKeySpec aesKey;
private final PrivateKey rsaKey;
public WildcardDecryptor(SecretKeySpec aesKey, PrivateKey rsaKey) {
this.aesKey = aesKey;
this.rsaKey = rsaKey;
}
public Optional<JsonValue> decrypt(String encrypted) {
try {
if (encrypted == null || encrypted.isEmpty()) {
return Optional.of(JsonValue.NULL);
}
String raw = tryDecrypt(encrypted);
JsonValue value = detectType(raw);
return Optional.of(value);
} catch (Exception ex) {
LOG.warning("Wildcard decrypt failed: " + ex.getMessage());
return Optional.of(JsonValue.NULL);
}
}
private String tryDecrypt(String encrypted) {
// Try AES
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
return new String(cipher.doFinal(encrypted.getBytes()));
} catch (Exception ignored) {}
// Try RSA
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, rsaKey);
return new String(cipher.doFinal(encrypted.getBytes()));
} catch (Exception ignored) {}
// Fallback: treat as plaintext
return encrypted;
}
private JsonValue detectType(String raw) {
// Number
try {
long n = Long.parseLong(raw);
return Json.createValue(n);
} catch (NumberFormatException ignored) {}
// Boolean
if (raw.equalsIgnoreCase("true")) return JsonValue.TRUE;
if (raw.equalsIgnoreCase("false")) return JsonValue.FALSE;
// JSON Object
if (raw.startsWith("{") && raw.endsWith("}")) {
try (JsonReader reader = Json.createReader(new java.io.StringReader(raw))) {
return reader.readObject();
} catch (Exception ignored) {}
}
// JSON Array
if (raw.startsWith("[") && raw.endsWith("]")) {
try (JsonReader reader = Json.createReader(new java.io.StringReader(raw))) {
return reader.readArray();
} catch (Exception ignored) {}
}
// String fallback
return Json.createValue(raw);
}
}






