你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
CryptographyAsyncClient 类
- java.
lang. Object - com.
azure. security. keyvault. keys. cryptography. CryptographyAsyncClient
- com.
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。
方法摘要
方法继承自 java.lang.Object
方法详细信息
decrypt
public Mono
使用配置的密钥和指定的算法解密单个加密数据块。 请注意,只能解密单个数据块,此块的大小取决于目标密钥和要使用的算法。 非对称密钥和对称密钥都支持解密操作。 此操作需要 keys/decrypt
非本地操作的权限。
EncryptionAlgorithm指示用于解密指定加密内容的算法类型。 非对称键的可能值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPAD、A128CBC_HS256、A128GCMA192CBC、A192CBC_HS384A192CBCPAD、、A192GCM、A256CBC、 A256CBCPADA256CBC_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:
Returns:
decrypt
public Mono
使用配置的密钥和指定的算法解密单个加密数据块。 请注意,只能解密单个数据块,此块的大小取决于目标密钥和要使用的算法。 非对称密钥和对称密钥都支持解密操作。 此操作需要 keys/decrypt
非本地操作的权限。
EncryptionAlgorithm指示用于解密指定加密内容的算法类型。 非对称键的可能值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPAD、A128CBC_HS256、A128GCMA192CBC、A192CBC_HS384A192CBCPAD、、A192GCM、A256CBC、 A256CBCPADA256CBC_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:
Returns:
encrypt
public Mono
使用配置的密钥加密任意字节序列。 请注意,加密操作仅支持单个数据块,其大小取决于目标密钥和要使用的加密算法。 对称密钥和非对称密钥都支持加密操作。 对于非对称密钥,密钥的公共部分用于加密。 此操作需要 keys/encrypt
非本地操作的权限。
指示 EncryptionAlgorithm 用于加密指定的 plaintext
的算法类型。 非对称键的可能值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPAD、A128CBC_HS256、A128GCMA192CBC、A192CBC_HS384A192CBCPAD、、A192GCM、A256CBC、 A256CBCPADA256CBC_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:
Returns:
encrypt
public Mono
使用配置的密钥加密任意字节序列。 请注意,加密操作仅支持单个数据块,其大小取决于目标密钥和要使用的加密算法。 对称密钥和非对称密钥都支持加密操作。 对于非对称密钥,密钥的公共部分用于加密。 此操作需要 keys/encrypt
非本地操作的权限。
指示 EncryptionAlgorithm 用于加密指定的 plaintext
的算法类型。 非对称键的可能值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括:A128CBC、、A128CBCPAD、A128CBC_HS256、A128GCMA192CBC、A192CBC_HS384A192CBCPAD、、A192GCM、A256CBC、 A256CBCPADA256CBC_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:
Returns:
getKey
public Mono
获取已配置密钥的公共部分。 获取密钥操作适用于所有密钥类型, 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:
getKeyWithResponse
public Mono
获取已配置密钥的公共部分。 获取密钥操作适用于所有密钥类型, 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:
sign
public Mono
使用配置的密钥从摘要创建签名。 签名操作同时支持非对称密钥和对称密钥。 此操作需要 keys/sign
非本地操作的权限。
指示 SignatureAlgorithm 用于从摘要创建签名的算法类型。 可能的值包括:ES256、、ES384、ES512、ES256K、PS256RS384RS256RS512、、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:
Returns:
signData
public Mono
使用配置的密钥从原始数据创建签名。 签名数据操作支持非对称密钥和对称密钥。 此操作需要 keys/sign
非本地操作的权限。
SignatureAlgorithm指示用于对摘要进行签名的算法类型。 可能的值包括:ES256、、ES384、ES512、ES256K、PS256RS384RS256RS512、、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:
Returns:
unwrapKey
public Mono
使用最初用于包装该密钥的已配置密钥解包对称密钥。 此操作与包装操作相反。 解包操作支持非对称密钥和对称密钥进行解包。 此操作需要 keys/unwrapKey
非本地操作的权限。
指示 KeyWrapAlgorithm 用于解包指定加密密钥内容的算法类型。 非对称键的可能值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括: A128KW和 A192KWA256KW。
示例代码
解包密钥内容。 异步订阅调用,并在收到响应时输出未包装的密钥详细信息。
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:
Returns:
verify
public Mono
使用配置的密钥验证签名。 验证操作同时支持对称密钥和非对称密钥。 在非对称密钥的情况下,密钥的公共部分用于验证签名。 此操作需要 keys/verify
非本地操作的权限。
SignatureAlgorithm指示用于验证签名的算法类型。 可能的值包括:ES256、、ES384、ES512、ES256K、PS256RS384RS256RS512、、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:
Returns:
verifyData
public Mono
使用配置的密钥针对原始数据验证签名。 验证操作同时支持对称密钥和非对称密钥。 在非对称密钥的情况下,密钥的公共部分用于验证签名。 此操作需要 keys/verify
非本地操作的权限。
SignatureAlgorithm指示用于验证签名的算法类型。 可能的值包括:ES256、、ES384、ES512、ES256K、PS256RS384RS256RS512、、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:
Returns:
wrapKey
public Mono
使用配置的密钥包装对称密钥。 包装操作支持使用对称和非对称密钥包装对称密钥。 此操作需要 keys/wrapKey
非本地操作的权限。
指示 KeyWrapAlgorithm 用于包装指定键内容的算法类型。 可能的值包括: RSA1_5和 RSA_OAEPRSA_OAEP_256。 对称密钥的可能值包括: A128KW和 A192KWA256KW。
示例代码
包装密钥内容。 异步订阅调用,并在收到响应时输出包装的密钥详细信息。
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:
Returns: