我是Tink的新手,我想从我生成的KeysetHandle中提取原始密钥数据(字符串形式),如下所示:
keysetHandle = KeysetHandle.generateNew(
AeadKeyTemplates.AES128_GCM);或者其他一些API来获取它。
我如何才能做到这一点?
发布于 2019-01-18 07:18:31
可以使用需要加密的KeysetHandle.write()或其他CleartextKeysetHandle.write()将密钥集写入磁盘。两者都需要BinaryKeysetWriter或JsonKeysetWriter。
发布于 2019-06-22 02:20:18
举个例子会有所帮助。下面是如何使用CleartextKeysetHandle.write()观察关键配置文件:
尝试显示以下内容:
// display key [Caveat: ONLY for observation]
public void display_key_profile_for_test_observation_only(KeysetHandle keysetHandle) throws IOException, GeneralSecurityException
{
System.out.println("\nDisplay key:");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withOutputStream(outputStream));
System.out.println("\n"+ new String(outputStream.toByteArray()));
}因为这属于一个类,所以您可能需要对代码进行一些细微的修改。您会看到关键字this,表示代码片段来自一个类。下面是测试用法:
public void trial_usage_key_generation() throws IOException, GeneralSecurityException {
for (CIPHER_SYMMETRIC_ALGOS algo_type : CIPHER_SYMMETRIC_ALGOS.values()) {
System.out.println("Generating key for : " + algo_type);
KeysetHandle keysetHandle = this.generate_key_for_test_observation_only(algo_type);
this.display_key_profile_for_test_observation_only(keysetHandle);
}
}发布于 2019-09-11 21:51:03
您可以使用反射来获取如下代码的键集,或者使用JsonKeysetWriter来获取base64ed密钥字节串(仍然需要反序列化到相应的key对象以获取原始的密钥字节)。
KeysetHandle keysetHandle = KeysetHandle.generateNew(
AeadKeyTemplates.CHACHA20_POLY1305);
Method method = keysetHandle.getClass().getDeclaredMethod("getKeyset");
method.setAccessible(true);
Keyset keyset = (Keyset) method.invoke(keysetHandle);
ChaCha20Poly1305Key key = ChaCha20Poly1305Key.parseFrom(keyset.getKey(0).getKeyData().getValue());
byte[] keyBytes = key.getKeyValue().toByteArray();https://stackoverflow.com/questions/52702152
复制相似问题