use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project aliyun-oss-java-sdk by aliyun.
the class KmsEncryptionMaterials method decryptCEK.
/**
* Decrypt the encrypted content encryption key(cek) and encrypted iv and put
* the result into {@link ContentCryptoMaterialRW}.
*
* @param contentMaterialRW
* The materials that contans all content crypto info,
* it must be constructed on outside and filled with the encrypted cek ,encrypted iv,
* key wrap algorithm, encryption materials description and cek generator
* algothrim. Then it will be builded with the cek iv parameters by this method.
*/
@Override
public void decryptCEK(ContentCryptoMaterialRW contentMaterialRW) {
assertParameterNotNull(contentMaterialRW, "ContentCryptoMaterialRW");
assertParameterNotNull(contentMaterialRW.getEncryptedCEK(), "ContentCryptoMaterialRW#getEncryptedCEK");
assertParameterNotNull(contentMaterialRW.getEncryptedIV(), "ContentCryptoMaterialRW#getEncryptedIV");
assertParameterNotNull(contentMaterialRW.getKeyWrapAlgorithm(), "ContentCryptoMaterialRW#getKeyWrapAlgorithm");
if (!contentMaterialRW.getKeyWrapAlgorithm().toLowerCase().equals(KEY_WRAP_ALGORITHM.toLowerCase())) {
throw new ClientException("Unrecognize your object key wrap algorithm: " + contentMaterialRW.getKeyWrapAlgorithm());
}
try {
KmsClientSuite kmsClientSuite = findKmsClientSuiteByDescription(contentMaterialRW.getMaterialsDescription());
if (kmsClientSuite == null) {
Entry<KmsClientSuite, Map<String, String>> entry = getTailByReflection(kmsDescMaterials);
kmsClientSuite = entry.getKey();
}
DecryptResponse decryptIvResp = decryptCipherBlob(kmsClientSuite, BinaryUtil.toBase64String(contentMaterialRW.getEncryptedIV()));
byte[] iv = BinaryUtil.fromBase64String(decryptIvResp.getPlaintext());
DecryptResponse decryptCEKResp = decryptCipherBlob(kmsClientSuite, BinaryUtil.toBase64String(contentMaterialRW.getEncryptedCEK()));
byte[] cekBytes = BinaryUtil.fromBase64String(decryptCEKResp.getPlaintext());
SecretKey cek = new SecretKeySpec(cekBytes, "");
contentMaterialRW.setCEK(cek);
contentMaterialRW.setIV(iv);
} catch (Exception e) {
throw new ClientException("Unable to decrypt content secured key and iv. " + "Please check your kms region and materails description." + e.getMessage(), e);
}
}
use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project java-docs-samples by GoogleCloudPlatform.
the class DecryptSymmetric method decryptSymmetric.
// Decrypt data that was encrypted using a symmetric key.
public void decryptSymmetric(String projectId, String locationId, String keyRingId, String keyId, byte[] ciphertext) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version from the project, location, key ring, and key.
CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// Optional, but recommended: compute ciphertext's CRC32C. See helpers below.
long ciphertextCrc32c = getCrc32cAsLong(ciphertext);
// Decrypt the ciphertext.
DecryptRequest request = DecryptRequest.newBuilder().setName(keyName.toString()).setCiphertext(ByteString.copyFrom(ciphertext)).setCiphertextCrc32C(Int64Value.newBuilder().setValue(ciphertextCrc32c).build()).build();
DecryptResponse response = client.decrypt(request);
// https://cloud.google.com/kms/docs/data-integrity-guidelines
if (!crcMatches(response.getPlaintextCrc32C().getValue(), response.getPlaintext().toByteArray())) {
throw new IOException("Decrypt: response from server corrupted");
}
System.out.printf("Plaintext: %s%n", response.getPlaintext().toStringUtf8());
}
}
use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project java-kms by googleapis.
the class DecryptSymmetric method decryptSymmetric.
// Decrypt data that was encrypted using a symmetric key.
public void decryptSymmetric(String projectId, String locationId, String keyRingId, String keyId, byte[] ciphertext) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, and
// key.
CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// Decrypt the response.
DecryptResponse response = client.decrypt(keyName, ByteString.copyFrom(ciphertext));
System.out.printf("Plaintext: %s%n", response.getPlaintext().toStringUtf8());
}
}
use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.
the class KmsTemplate method decryptBytes.
@Override
public byte[] decryptBytes(String cryptoKey, byte[] cipherText) {
CryptoKeyName cryptoKeyName = KmsPropertyUtils.getCryptoKeyName(cryptoKey, projectIdProvider);
ByteString encryptedByteString = ByteString.copyFrom(cipherText);
long crc32c = longCrc32c(encryptedByteString);
DecryptRequest request = DecryptRequest.newBuilder().setName(cryptoKeyName.toString()).setCiphertext(encryptedByteString).setCiphertextCrc32C(Int64Value.newBuilder().setValue(crc32c).build()).build();
DecryptResponse response = client.decrypt(request);
assertCrcMatch(response);
return response.getPlaintext().toByteArray();
}
use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.
the class KmsTemplateTests method testDecryptCorrupt.
@Test
void testDecryptCorrupt() {
DecryptResponse decryptResponse = DecryptResponse.newBuilder().setPlaintext(ByteString.copyFromUtf8("1234")).setPlaintextCrc32C(Int64Value.newBuilder().setValue(0L).build()).build();
when(this.client.decrypt(any(DecryptRequest.class))).thenReturn(decryptResponse);
String cryptoKeyNameStr = "test-project/europe-west2/key-ring-id/key-id";
assertThatThrownBy(() -> kmsTemplate.decryptText(cryptoKeyNameStr, "1234".getBytes())).isInstanceOf(com.google.cloud.spring.kms.KmsException.class);
}
Aggregations