我正在尝试将 Google Tink 的 KeysetHandle 转换为字节 [] 并从字节 [] 构造回 KeysetHandle。
所以这是我想出的代码
//Two functions for the conversion
public static String keyToString(KeysetHandle keysetHandle) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withOutputStream(baos));
oos.close();
return Arrays.toString(baos.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static KeysetHandle stringToKey(String keysetString) {
keysetString = keysetString.replace("[","").replace("]","").replace(" ","");
try {
List<Byte> list = new ArrayList<>();
String[] splitArray = keysetString.split(",");
for (String x : splitArray) {
list.add((byte) Integer.parseInt(x));
}
byte[] data = Bytes.toArray(list);
System.out.println(Arrays.toString(data));
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
KeysetHandle keysetHandle = CleartextKeysetHandle.read(JsonKeysetReader.withInputStream(ois));
ois.close();
return keysetHandle;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
主功能 :
KeysetHandle keyHandle = null;
try {
AeadConfig.register();
keyHandle = KeysetHandle.generateNew(KeyTemplates.get("AES128_GCM"));
} catch (Exception e) {
e.printStackTrace();
}
String keyString =keyToString(keyHandle);
System.out.println(keyString);
KeysetHandle fromString = stringToKey(keyString);
输出 :
I/System.out: [-84, -19, 0, 5, 123, ...]
I/System.out: [-84, -19, 0, 5, 123, ...]
W/System.err: java.io.IOException: java.lang.IllegalStateException: Not a JSON Object: null
at com.google.crypto.tink.JsonKeysetReader.read(JsonKeysetReader.java:148)
at com.google.crypto.tink.CleartextKeysetHandle.read(CleartextKeysetHandle.java:58)
at com.ebenezer.testing.MainActivity.stringToKey(MainActivity.java:49)
at com.ebenezer.testing.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
W/System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Not a JSON Object: null
at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:91)
at com.google.crypto.tink.JsonKeysetReader.read(JsonKeysetReader.java:145)
... 18 more
我不知道为什么在尝试重建 KeysetHandle 对象时会出现错误。有人可以解释为什么会发生此错误或我在这里做错了什么吗?