你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

CryptographyAsyncClient 类

  • java.lang.Object
    • com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient

public class CryptographyAsyncClient

CryptographyAsyncClient提供了使用非对称密钥和对称密钥执行加密操作的异步方法。 客户端支持使用配置的密钥加密、解密、包装密钥、解包密钥、签名和验证操作。

入门

若要与 Azure 密钥保管库 服务交互,需要创建 类的CryptographyAsyncClient实例、保管库 URL 和凭据对象。

本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,我们建议使用 托管标识 在生产环境中进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。

示例:构造异步加密客户端

下面的代码示例演示如何创建 CryptographyAsyncClient,使用 CryptographyClientBuilder 对其进行配置。

CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder()
     .keyIdentifier("<your-key-id>")
     .credential(new DefaultAzureCredentialBuilder().build())
     .buildAsyncClient();
JsonWebKey jsonWebKey = new JsonWebKey().setId("SampleJsonWebKey");
 CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder()
     .jsonWebKey(jsonWebKey)
     .buildAsyncClient();

对数据进行加密

CryptographyAsyncClient可用于加密数据。

代码示例:

下面的代码示例演示如何使用 encrypt(EncryptionAlgorithm algorithm, byte[] plaintext) API 异步加密数据。

byte[] plaintext = new byte[100];
 new Random(0x1234567L).nextBytes(plaintext);

 cryptographyAsyncClient.encrypt(EncryptionAlgorithm.RSA_OAEP, plaintext)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(encryptResult ->
         System.out.printf("Received encrypted content of length: %d, with algorithm: %s.%n",
             encryptResult.getCipherText().length, encryptResult.getAlgorithm().toString()));

注意: 有关同步示例,请参阅 CryptographyClient


解密数据

CryptographyAsyncClient可用于解密数据。

代码示例:

以下代码示例演示如何使用 decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext) API 异步解密数据。

byte[] ciphertext = new byte[100];
 new Random(0x1234567L).nextBytes(ciphertext);

 cryptographyAsyncClient.decrypt(EncryptionAlgorithm.RSA_OAEP, ciphertext)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(decryptResult ->
         System.out.printf("Received decrypted content of length: %d%n", decryptResult.getPlainText().length));

注意: 有关同步示例,请参阅 CryptographyClient

方法摘要

修饰符和类型 方法和描述
Mono<DecryptResult> decrypt(DecryptParameters decryptParameters)

使用配置的密钥和指定的算法解密单个加密数据块。

Mono<DecryptResult> decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext)

使用配置的密钥和指定的算法解密单个加密数据块。

Mono<EncryptResult> encrypt(EncryptParameters encryptParameters)

使用配置的密钥加密任意字节序列。

Mono<EncryptResult> encrypt(EncryptionAlgorithm algorithm, byte[] plaintext)

使用配置的密钥加密任意字节序列。

Mono<KeyVaultKey> getKey()

获取已配置密钥的公共部分。

Mono<Response<KeyVaultKey>> getKeyWithResponse()

获取已配置密钥的公共部分。

Mono<SignResult> sign(SignatureAlgorithm algorithm, byte[] digest)

使用配置的密钥从摘要创建签名。

Mono<SignResult> signData(SignatureAlgorithm algorithm, byte[] data)

使用配置的密钥从原始数据创建签名。

Mono<UnwrapResult> unwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey)

使用最初用于包装该密钥的已配置密钥解包对称密钥。

Mono<VerifyResult> verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature)

使用配置的密钥验证签名。

Mono<VerifyResult> verifyData(SignatureAlgorithm algorithm, byte[] data, byte[] signature)

使用配置的密钥针对原始数据验证签名。

Mono<WrapResult> wrapKey(KeyWrapAlgorithm algorithm, byte[] key)

使用配置的密钥包装对称密钥。

方法继承自 java.lang.Object

方法详细信息

decrypt

public Mono decrypt(DecryptParameters decryptParameters)

使用配置的密钥和指定的算法解密单个加密数据块。 请注意,只能解密单个数据块,此块的大小取决于目标密钥和要使用的算法。 非对称密钥和对称密钥都支持解密操作。 此操作需要 keys/decrypt 非本地操作的权限。

EncryptionAlgorithm指示用于解密指定加密内容的算法类型。 非对称键的可能值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPADA128CBC_HS256A128GCMA192CBCA192CBC_HS384A192CBCPAD、、A192GCMA256CBCA256CBCPADA256CBC_HS512 和 。A256GCM

示例代码

解密加密的内容。 异步订阅调用,并在收到响应时输出解密的内容详细信息。

byte[] ciphertextBytes = new byte[100];
 new Random(0x1234567L).nextBytes(ciphertextBytes);
 byte[] iv = {
     (byte) 0x1a, (byte) 0xf3, (byte) 0x8c, (byte) 0x2d, (byte) 0xc2, (byte) 0xb9, (byte) 0x6f, (byte) 0xfd,
     (byte) 0xd8, (byte) 0x66, (byte) 0x94, (byte) 0x09, (byte) 0x23, (byte) 0x41, (byte) 0xbc, (byte) 0x04
 };

 DecryptParameters decryptParameters = DecryptParameters.createA128CbcParameters(ciphertextBytes, iv);

 cryptographyAsyncClient.decrypt(decryptParameters)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(decryptResult ->
         System.out.printf("Received decrypted content of length: %d.%n", decryptResult.getPlainText().length));

Parameters:

decryptParameters - 在解密操作中使用的参数。 例如,Microsoft 建议不要在未首先使用 HMAC 确保密码文本的完整性的情况下使用 CBC。 有关详细信息,请参阅 使用填充对 CBC 模式对称解密的漏洞进行计时

Returns:

一个 Mono 包含已解密 Blob 的 。

decrypt

public Mono decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext)

使用配置的密钥和指定的算法解密单个加密数据块。 请注意,只能解密单个数据块,此块的大小取决于目标密钥和要使用的算法。 非对称密钥和对称密钥都支持解密操作。 此操作需要 keys/decrypt 非本地操作的权限。

EncryptionAlgorithm指示用于解密指定加密内容的算法类型。 非对称键的可能值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPADA128CBC_HS256A128GCMA192CBCA192CBC_HS384A192CBCPAD、、A192GCMA256CBCA256CBCPADA256CBC_HS512 和 。A256GCM

示例代码

解密加密的内容。 异步订阅调用,并在收到响应时输出解密的内容详细信息。

byte[] ciphertext = new byte[100];
 new Random(0x1234567L).nextBytes(ciphertext);

 cryptographyAsyncClient.decrypt(EncryptionAlgorithm.RSA_OAEP, ciphertext)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(decryptResult ->
         System.out.printf("Received decrypted content of length: %d%n", decryptResult.getPlainText().length));

Parameters:

algorithm - 用于解密的算法。
ciphertext - 要解密的内容。 例如,Microsoft 建议不要在未首先使用 HMAC 确保密码文本的完整性的情况下使用 CBC。 有关详细信息,请参阅 使用填充对 CBC 模式对称解密的漏洞进行计时

Returns:

一个 Mono 包含已解密 Blob 的 。

encrypt

public Mono encrypt(EncryptParameters encryptParameters)

使用配置的密钥加密任意字节序列。 请注意,加密操作仅支持单个数据块,其大小取决于目标密钥和要使用的加密算法。 对称密钥和非对称密钥都支持加密操作。 对于非对称密钥,密钥的公共部分用于加密。 此操作需要 keys/encrypt 非本地操作的权限。

指示 EncryptionAlgorithm 用于加密指定的 plaintext的算法类型。 非对称键的可能值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPADA128CBC_HS256A128GCMA192CBCA192CBC_HS384A192CBCPAD、、A192GCMA256CBCA256CBCPADA256CBC_HS512 和 。A256GCM

示例代码

加密内容。 异步订阅调用,并在收到响应时输出加密内容详细信息。

byte[] plaintextBytes = new byte[100];
 new Random(0x1234567L).nextBytes(plaintextBytes);
 byte[] iv = {
     (byte) 0x1a, (byte) 0xf3, (byte) 0x8c, (byte) 0x2d, (byte) 0xc2, (byte) 0xb9, (byte) 0x6f, (byte) 0xfd,
     (byte) 0xd8, (byte) 0x66, (byte) 0x94, (byte) 0x09, (byte) 0x23, (byte) 0x41, (byte) 0xbc, (byte) 0x04
 };

 EncryptParameters encryptParameters = EncryptParameters.createA128CbcParameters(plaintextBytes, iv);

 cryptographyAsyncClient.encrypt(encryptParameters)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(encryptResult ->
         System.out.printf("Received encrypted content of length: %d, with algorithm: %s.%n",
             encryptResult.getCipherText().length, encryptResult.getAlgorithm().toString()));

Parameters:

encryptParameters - 在加密操作中使用的参数。

Returns:

一个 MonoEncryptResultgetCipherText() ,它包含其包含加密内容的 。

encrypt

public Mono encrypt(EncryptionAlgorithm algorithm, byte[] plaintext)

使用配置的密钥加密任意字节序列。 请注意,加密操作仅支持单个数据块,其大小取决于目标密钥和要使用的加密算法。 对称密钥和非对称密钥都支持加密操作。 对于非对称密钥,密钥的公共部分用于加密。 此操作需要 keys/encrypt 非本地操作的权限。

指示 EncryptionAlgorithm 用于加密指定的 plaintext的算法类型。 非对称键的可能值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPADA128CBC_HS256A128GCMA192CBCA192CBC_HS384A192CBCPAD、、A192GCMA256CBCA256CBCPADA256CBC_HS512 和 。A256GCM

示例代码

加密内容。 异步订阅调用,并在收到响应时输出加密内容详细信息。

byte[] plaintext = new byte[100];
 new Random(0x1234567L).nextBytes(plaintext);

 cryptographyAsyncClient.encrypt(EncryptionAlgorithm.RSA_OAEP, plaintext)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(encryptResult ->
         System.out.printf("Received encrypted content of length: %d, with algorithm: %s.%n",
             encryptResult.getCipherText().length, encryptResult.getAlgorithm().toString()));

Parameters:

algorithm - 用于加密的算法。
plaintext - 要加密的内容。

Returns:

包含MonoEncryptResultgetCipherText()包含加密内容的 。

getKey

public Mono getKey()

获取已配置密钥的公共部分。 获取密钥操作适用于所有密钥类型, keys/get 并且需要非本地操作的权限。

示例代码

获取客户端中配置的密钥。 异步订阅调用,并在收到响应时输出返回的密钥详细信息。

cryptographyAsyncClient.getKey()
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(key ->
         System.out.printf("Key returned with name: %s, and id: %s.%n", key.getName(), key.getId()));

Returns:

包含 Mono 请求的 KeyVaultKey

getKeyWithResponse

public Mono> getKeyWithResponse()

获取已配置密钥的公共部分。 获取密钥操作适用于所有密钥类型, keys/get 并且需要非本地操作的权限。

示例代码

获取客户端中配置的密钥。 异步订阅调用,并在收到响应时输出返回的密钥详细信息。

cryptographyAsyncClient.getKeyWithResponse()
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(keyResponse ->
         System.out.printf("Key returned with name: %s, and id: %s.%n", keyResponse.getValue().getName(),
             keyResponse.getValue().getId()));

Returns:

一个 MonoResponse<T>value ,它包含请求的 。KeyVaultKey

sign

public Mono sign(SignatureAlgorithm algorithm, byte[] digest)

使用配置的密钥从摘要创建签名。 签名操作同时支持非对称密钥和对称密钥。 此操作需要 keys/sign 非本地操作的权限。

指示 SignatureAlgorithm 用于从摘要创建签名的算法类型。 可能的值包括:ES256、、ES384ES512ES256KPS256RS384RS256RS512、、RS384、 和 。RS512

示例代码

唱摘要。 异步订阅调用,并在收到响应时输出签名详细信息。

byte[] data = new byte[100];
 new Random(0x1234567L).nextBytes(data);
 MessageDigest md = MessageDigest.getInstance("SHA-256");
 md.update(data);
 byte[] digest = md.digest();

 cryptographyAsyncClient.sign(SignatureAlgorithm.ES256, digest)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(signResult ->
         System.out.printf("Received signature of length: %d, with algorithm: %s.%n",
             signResult.getSignature().length, signResult.getAlgorithm()));

Parameters:

algorithm - 用于签名的算法。
digest - 要从中创建签名的内容。

Returns:

一个MonoSignResult包含其getSignature()所创建签名的 。

signData

public Mono signData(SignatureAlgorithm algorithm, byte[] data)

使用配置的密钥从原始数据创建签名。 签名数据操作支持非对称密钥和对称密钥。 此操作需要 keys/sign 非本地操作的权限。

SignatureAlgorithm指示用于对摘要进行签名的算法类型。 可能的值包括:ES256、、ES384ES512ES256KPS256RS384RS256RS512、、RS384、 和 。RS512

示例代码

对原始数据进行签名。 异步订阅调用,并在收到响应时输出签名详细信息。

byte[] data = new byte[100];
 new Random(0x1234567L).nextBytes(data);

 cryptographyAsyncClient.sign(SignatureAlgorithm.ES256, data)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(signResult ->
         System.out.printf("Received signature of length: %d, with algorithm: %s.%n",
             signResult.getSignature().length, signResult.getAlgorithm()));

Parameters:

algorithm - 用于签名的算法。
data - 要从中创建签名的内容。

Returns:

一个MonoSignResult包含其getSignature()所创建签名的 。

unwrapKey

public Mono unwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey)

使用最初用于包装该密钥的已配置密钥解包对称密钥。 此操作与包装操作相反。 解包操作支持非对称密钥和对称密钥进行解包。 此操作需要 keys/unwrapKey 非本地操作的权限。

指示 KeyWrapAlgorithm 用于解包指定加密密钥内容的算法类型。 非对称键的可能值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括: A128KWA192KWA256KW

示例代码

解包密钥内容。 异步订阅调用,并在收到响应时输出未包装的密钥详细信息。

byte[] keyToWrap = new byte[100];
 new Random(0x1234567L).nextBytes(key);

 cryptographyAsyncClient.wrapKey(KeyWrapAlgorithm.RSA_OAEP, keyToWrap)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(wrapResult ->
         cryptographyAsyncClient.unwrapKey(KeyWrapAlgorithm.RSA_OAEP, wrapResult.getEncryptedKey())
             .subscribe(keyUnwrapResult ->
                 System.out.printf("Received key of length: %d.%n", keyUnwrapResult.getKey().length)));

Parameters:

algorithm - 用于包装密钥的加密算法。
encryptedKey - 要解包的加密密钥内容。

Returns:

一个 MonoUnwrapResult ,包含其 decrypted key 包含未包装的密钥结果的 。

verify

public Mono verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature)

使用配置的密钥验证签名。 验证操作同时支持对称密钥和非对称密钥。 在非对称密钥的情况下,密钥的公共部分用于验证签名。 此操作需要 keys/verify 非本地操作的权限。

SignatureAlgorithm指示用于验证签名的算法类型。 可能的值包括:ES256、、ES384ES512ES256KPS256RS384RS256RS512、、RS384、 和 。RS512

示例代码

根据指定的摘要验证签名。 异步订阅调用,并在收到响应时输出验证详细信息。

byte[] myData = new byte[100];
 new Random(0x1234567L).nextBytes(myData);
 MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
 messageDigest.update(myData);
 byte[] myDigest = messageDigest.digest();

 // A signature can be obtained from the SignResult returned by the CryptographyAsyncClient.sign() operation.
 cryptographyAsyncClient.verify(SignatureAlgorithm.ES256, myDigest, signature)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(verifyResult ->
         System.out.printf("Verification status: %s.%n", verifyResult.isValid()));

Parameters:

algorithm - 用于签名的算法。
digest - 从中创建签名的内容。
signature - 要验证的签名。

Returns:

包含 MonoVerifyResultisValid()

verifyData

public Mono verifyData(SignatureAlgorithm algorithm, byte[] data, byte[] signature)

使用配置的密钥针对原始数据验证签名。 验证操作同时支持对称密钥和非对称密钥。 在非对称密钥的情况下,密钥的公共部分用于验证签名。 此操作需要 keys/verify 非本地操作的权限。

SignatureAlgorithm指示用于验证签名的算法类型。 可能的值包括:ES256、、ES384ES512ES256KPS256RS384RS256RS512、、RS384、 和 。RS512

示例代码

根据原始数据验证签名。 异步订阅调用,并在收到响应时输出验证详细信息。

byte[] myData = new byte[100];
 new Random(0x1234567L).nextBytes(myData);

 // A signature can be obtained from the SignResult returned by the CryptographyAsyncClient.sign() operation.
 cryptographyAsyncClient.verify(SignatureAlgorithm.ES256, myData, signature)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(verifyResult ->
         System.out.printf("Verification status: %s.%n", verifyResult.isValid()));

Parameters:

algorithm - 用于签名的算法。
data - 要对其验证签名的原始内容。
signature - 要验证的签名。

Returns:

包含 MonoVerifyResultisValid()

wrapKey

public Mono wrapKey(KeyWrapAlgorithm algorithm, byte[] key)

使用配置的密钥包装对称密钥。 包装操作支持使用对称和非对称密钥包装对称密钥。 此操作需要 keys/wrapKey 非本地操作的权限。

指示 KeyWrapAlgorithm 用于包装指定键内容的算法类型。 可能的值包括: RSA1_5RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括: A128KWA192KWA256KW

示例代码

包装密钥内容。 异步订阅调用,并在收到响应时输出包装的密钥详细信息。

byte[] key = new byte[100];
 new Random(0x1234567L).nextBytes(key);

 cryptographyAsyncClient.wrapKey(KeyWrapAlgorithm.RSA_OAEP, key)
     .contextWrite(Context.of("key1", "value1", "key2", "value2"))
     .subscribe(wrapResult ->
         System.out.printf("Received encrypted key of length: %d, with algorithm: %s.%n",
             wrapResult.getEncryptedKey().length, wrapResult.getAlgorithm().toString()));

Parameters:

algorithm - 用于包装密钥的加密算法。
key - 要包装的关键内容。

Returns:

包含MonoWrapResultgetEncryptedKey()包含已包装的键结果的 。

适用于