Search in sources :

Example 1 with EncryptRequest

use of com.amazonaws.services.kms.model.EncryptRequest in project spring-cloud-config-aws-kms by zalando.

the class KmsTextEncryptorTest method setUp.

@Before
public void setUp() throws Exception {
    mockKms = mock(AWSKMS.class);
    textEncryptor = new KmsTextEncryptor(mockKms, KMS_KEY_ID);
    expectedEncryptRequest = new EncryptRequest();
    expectedEncryptRequest.setKeyId(KMS_KEY_ID);
    expectedEncryptRequest.setPlaintext(wrap(PLAINTEXT.getBytes()));
    encryptResult = new EncryptResult();
    encryptResult.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
    when(mockKms.encrypt(any(EncryptRequest.class))).thenReturn(encryptResult);
    expectedDecryptRequest = new DecryptRequest();
    expectedDecryptRequest.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
    decryptResult = new DecryptResult();
    decryptResult.setPlaintext(wrap(PLAINTEXT.getBytes()));
    when(mockKms.decrypt(any(DecryptRequest.class))).thenReturn(decryptResult);
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) EncryptResult(com.amazonaws.services.kms.model.EncryptResult) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) AWSKMS(com.amazonaws.services.kms.AWSKMS) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest) Before(org.junit.Before)

Example 2 with EncryptRequest

use of com.amazonaws.services.kms.model.EncryptRequest in project testcases by coheigea.

the class KMSPasswordEncryptor method encrypt.

@Override
public String encrypt(String password) {
    final AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
    AWSKMSClient kms = new AWSKMSClient(creds);
    kms.setEndpoint(endpoint);
    ByteBuffer plaintext = ByteBuffer.wrap(password.getBytes());
    EncryptRequest req = new EncryptRequest().withPlaintext(plaintext);
    req.setKeyId(masterKeyId);
    ByteBuffer encryptedKey = kms.encrypt(req).getCiphertextBlob();
    byte[] key = new byte[encryptedKey.remaining()];
    encryptedKey.get(key);
    return Base64.encode(key);
}
Also used : AWSKMSClient(com.amazonaws.services.kms.AWSKMSClient) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) ByteBuffer(java.nio.ByteBuffer) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest)

Example 3 with EncryptRequest

use of com.amazonaws.services.kms.model.EncryptRequest in project spring-cloud-config-aws-kms by zalando.

the class KmsTextEncryptor method encrypt.

@Override
public String encrypt(final String text) {
    Assert.hasText(kmsKeyId, "kmsKeyId must not be blank");
    if (text == null || text.isEmpty()) {
        return EMPTY_STRING;
    } else {
        final EncryptRequest encryptRequest = // 
        new EncryptRequest().withKeyId(kmsKeyId).withPlaintext(ByteBuffer.wrap(text.getBytes()));
        final ByteBuffer encryptedBytes = kms.encrypt(encryptRequest).getCiphertextBlob();
        return extractString(ByteBuffer.wrap(Base64.encode(encryptedBytes.array())));
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest)

Example 4 with EncryptRequest

use of com.amazonaws.services.kms.model.EncryptRequest in project aws-doc-sdk-examples by awsdocs.

the class EncryptDataKey method main.

public static void main(String[] args) {
    AWSKMS kmsClient = AWSKMSClientBuilder.standard().build();
    // Encrypt a data key
    // 
    // Replace the following fictitious CMK ARN with a valid CMK ID or ARN
    String keyId = "1234abcd-12ab-34cd-56ef-1234567890ab";
    ByteBuffer plaintext = ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
    EncryptRequest req = new EncryptRequest().withKeyId(keyId).withPlaintext(plaintext);
    ByteBuffer ciphertext = kmsClient.encrypt(req).getCiphertextBlob();
}
Also used : ByteBuffer(java.nio.ByteBuffer) AWSKMS(com.amazonaws.services.kms.AWSKMS) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest)

Aggregations

EncryptRequest (com.amazonaws.services.kms.model.EncryptRequest)4 ByteBuffer (java.nio.ByteBuffer)3 AWSKMS (com.amazonaws.services.kms.AWSKMS)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AWSKMSClient (com.amazonaws.services.kms.AWSKMSClient)1 DecryptRequest (com.amazonaws.services.kms.model.DecryptRequest)1 DecryptResult (com.amazonaws.services.kms.model.DecryptResult)1 EncryptResult (com.amazonaws.services.kms.model.EncryptResult)1 Before (org.junit.Before)1