如何使用KeyVaultClient(JAVA)加密Azure中的对象

时间:2017-08-30 09:54:17

标签: java azure encryption azure-keyvault

通常,字节数组为plainText加密。

promise = keyVaultClient.encryptAsync(keyId.getBaseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSAOAEP, plainText); 
result = promise.get(); 
cipherText = result.getResult();

KeyVaultClient对象加密byte []并返回Future。

如何加密对象?

1 个答案:

答案 0 :(得分:0)

您可以在azure keyvault java sdk

中查看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
  }
}