通常,字节数组为plainText加密。
promise = keyVaultClient.encryptAsync(keyId.getBaseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSAOAEP, plainText);
result = promise.get();
cipherText = result.getResult();
KeyVaultClient对象加密byte []并返回Future。
如何加密对象?
答案 0 :(得分:0)
encryptAsync
方法的搜索链代码
public ServiceFuture<KeyOperationResult> encryptAsync(String keyIdentifier, JsonWebKeyEncryptionAlgorithm algorithm, byte[] value, final ServiceCallback<KeyOperationResult> serviceCallback) {
KeyIdentifier id = new KeyIdentifier(keyIdentifier);
return innerKeyVaultClient.encryptAsync(id.vault, id.name, id.version == null ? "" : id.version, algorithm, value, serviceCallback);
}
观察此方法所需的参数,并且不难发现它需要byte []类型的参数,因此您只需将对象转换为byte []。
您可以参考SO线程中提到的代码:Java Serializable Object to Byte Array。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}