use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.
the class KmsTemplateTests method testEncryptDecrypt.
@Test
void testEncryptDecrypt() {
EncryptResponse encryptResponse = createEncryptResponse();
DecryptResponse decryptResponse = createDecryptResponse();
when(this.client.encrypt(any(EncryptRequest.class))).thenReturn(encryptResponse);
when(this.client.decrypt(any(DecryptRequest.class))).thenReturn(decryptResponse);
String cryptoKeyNameStr = "test-project/europe-west2/key-ring-id/key-id";
byte[] encryptedBytes = kmsTemplate.encryptText(cryptoKeyNameStr, "1234");
String decryptedText = kmsTemplate.decryptText(cryptoKeyNameStr, encryptedBytes);
Assert.assertEquals("1234", decryptedText);
}
use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.
the class KmsTemplateTests method testEncryptDecryptMissMatch.
@Test
void testEncryptDecryptMissMatch() {
EncryptResponse encryptResponse = createEncryptResponse();
when(this.client.encrypt(any(EncryptRequest.class))).thenReturn(encryptResponse);
when(this.client.decrypt(any(DecryptRequest.class))).thenThrow(InvalidArgumentException.class);
String cryptoKeyNameStr = "test-project/europe-west2/key-ring-id/key-id";
byte[] encryptedBytes = kmsTemplate.encryptText(cryptoKeyNameStr, "1234");
assertThatThrownBy(() -> kmsTemplate.decryptText(cryptoKeyNameStr, encryptedBytes)).isInstanceOf(com.google.api.gax.rpc.InvalidArgumentException.class);
}
use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project aliyun-oss-java-sdk by aliyun.
the class KmsEncryptionMaterials method encryptPlainText.
/**
* Encrypt the plain text to cipherBlob.
*/
private EncryptResponse encryptPlainText(String keyId, String plainText) throws ClientException {
DefaultAcsClient kmsClient = createKmsClient(region, credentialsProvider);
final EncryptRequest encReq = new EncryptRequest();
encReq.setSysProtocol(ProtocolType.HTTPS);
encReq.setAcceptFormat(FormatType.JSON);
encReq.setSysMethod(MethodType.POST);
encReq.setKeyId(keyId);
encReq.setPlaintext(plainText);
final EncryptResponse encResponse;
try {
encResponse = kmsClient.getAcsResponse(encReq);
} catch (Exception e) {
throw new ClientException("the kms client encrypt data failed." + e.getMessage(), e);
}
return encResponse;
}
use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method testDecryptSymmetric.
@Test
public void testDecryptSymmetric() throws IOException {
String plaintext = "my plaintext";
byte[] ciphertext;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
EncryptResponse result = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
ciphertext = result.getCiphertext().toByteArray();
}
new DecryptSymmetric().decryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, ciphertext);
assertThat(stdOut.toString()).contains(plaintext);
}
use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project java-kms by googleapis.
the class SnippetsIT method testDecryptSymmetric.
@Test
public void testDecryptSymmetric() throws IOException {
String plaintext = "my plaintext";
byte[] ciphertext;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
EncryptResponse result = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
ciphertext = result.getCiphertext().toByteArray();
}
new DecryptSymmetric().decryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, ciphertext);
assertThat(stdOut.toString()).contains(plaintext);
}
Aggregations