AWS KMS How to use Decrypt function Java

Abhishek Garg

My question might sound too obvious but I am new to Amazon KMS. After reading a lot of docs on AWS I understood that if I am using CMK directly for encryption and decryption I can directly do it by creating encrypt and decrypt request. But what I am not clear is when I generate a data key and debug using that,the documentation says I need to pass encrypted data key to decrypt API and I will get plain text key which I can use to debug text on my "OWN". I don't understand this part. Can anyone please explain this and give a small example on decryption using data keys. Thanks in advance

My Sample code:

public String decrypt(String encryptedTextString) {
    ByteBuffer encryptedText = ByteBuffer.wrap(Base64.getDecoder().decode(encryptedTextString));
    DecryptRequest req=new DecryptRequest().withCiphertextBlob(encryptedText);
    ByteBuffer plainText = client.decrypt(req).getPlaintext();
    return new String(plainText.array());
}

public String encrypt(String plainTextString) {
    ByteBuffer plainText = ByteBuffer.wrap(Base64.getDecoder().decode(plainTextString));
    EncryptRequest req = new EncryptRequest().withKeyId(new String(plainTextKey.array()))
            .withPlaintext(plainText);
    ByteBuffer encryptedText =client.encrypt(req).getCiphertextBlob();
    return new String(encryptedText.array());
}

AWSKMSCryprography() {
    this.setCredential(new ClearCredential());
    this.genrateKey();
}

private void genrateKey() {
    GenerateDataKeyRequest request = new GenerateDataKeyRequest();
    request.setKeyId(keyID);
    request.setKeySpec("AES_128");
    GenerateDataKeyResult dataKeyResult = client.generateDataKey(request);
    plainTextKey = dataKeyResult.getPlaintext();
    encryptedKey = dataKeyResult.getCiphertextBlob();

}
Thien

I'm new to using KMS as well but the tutorial documentation on using encrypt and decrypt is misleading with using the encrypt and decrypt methods. The API documentation for both AWSKMSClient.generateDataKey and AWSKKMSClient.encrypt point out that encrypt() is for specific use cases and a different pattern should be use for using the local key.

A more useful example of KMS can be found in the dynamodb encryption library. Also see http://netnix.org/2015/04/19/aes-encryption-with-hmac-integrity-in-java/ for an overview of basic encryption in general.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use aws kms encrypt when "plaintext" starts with dash (-)

From Dev

How to use substitute function to decrypt and encrypt messages

From Dev

encrypt/decrypt contents of whole folder in powershell using AWS KMS

From Dev

How to use openssl to decrypt data encrypted by Java using AES

From Dev

How to decrypt Java AES on Nodejs

From Dev

How to decrypt an encrypted String in java

From Dev

SSRS to use a java runtime to decrypt to display the data?

From Dev

How to decrypt data which is crypted by CryptProtectData function?

From Dev

How to decrypt hash get from this function

From Dev

how to use function correctly in java

From Dev

How to use a java function in libgdx

From Dev

How to decrypt encrypted image in memory & use in Application

From Dev

How to use IAM role with AWS Java SDK

From Dev

How to decrypt file in java which is encrypted in .net

From Dev

how to decrypt partitions of cipertext - AES, Java

From Dev

How to encrypt in Java and decrypt in Android and iOS

From Dev

How to decrypt AWS S3 file during download?

From Dev

How to use AWS.CloudFront.Signer in Lambda function

From Dev

How to (properly) use external credentials in an AWS Lambda function?

From Dev

aws lambda function how to use responses to perform calculation

From Dev

module initialization error: ReferenceError when using kms.decrypt

From Dev

AWS Lambda Function in java

From Dev

How to use map-function in SPARK with Java

From Dev

how to use zip function spark java

From Dev

How to use opencv snake function in Java?

From Dev

How do I create an encrypt and decrypt function in NodeJS?

From Dev

How do you use AES to Encrypt in One Program, Decrypt in Another

From Dev

How to use ed25519 to encrypt/decrypt data?

From Dev

How to encrypt/decrypt my CoreData(sqlite)? Can I use SQLCipher for it?

Related Related

  1. 1

    How to use aws kms encrypt when "plaintext" starts with dash (-)

  2. 2

    How to use substitute function to decrypt and encrypt messages

  3. 3

    encrypt/decrypt contents of whole folder in powershell using AWS KMS

  4. 4

    How to use openssl to decrypt data encrypted by Java using AES

  5. 5

    How to decrypt Java AES on Nodejs

  6. 6

    How to decrypt an encrypted String in java

  7. 7

    SSRS to use a java runtime to decrypt to display the data?

  8. 8

    How to decrypt data which is crypted by CryptProtectData function?

  9. 9

    How to decrypt hash get from this function

  10. 10

    how to use function correctly in java

  11. 11

    How to use a java function in libgdx

  12. 12

    How to decrypt encrypted image in memory & use in Application

  13. 13

    How to use IAM role with AWS Java SDK

  14. 14

    How to decrypt file in java which is encrypted in .net

  15. 15

    how to decrypt partitions of cipertext - AES, Java

  16. 16

    How to encrypt in Java and decrypt in Android and iOS

  17. 17

    How to decrypt AWS S3 file during download?

  18. 18

    How to use AWS.CloudFront.Signer in Lambda function

  19. 19

    How to (properly) use external credentials in an AWS Lambda function?

  20. 20

    aws lambda function how to use responses to perform calculation

  21. 21

    module initialization error: ReferenceError when using kms.decrypt

  22. 22

    AWS Lambda Function in java

  23. 23

    How to use map-function in SPARK with Java

  24. 24

    how to use zip function spark java

  25. 25

    How to use opencv snake function in Java?

  26. 26

    How do I create an encrypt and decrypt function in NodeJS?

  27. 27

    How do you use AES to Encrypt in One Program, Decrypt in Another

  28. 28

    How to use ed25519 to encrypt/decrypt data?

  29. 29

    How to encrypt/decrypt my CoreData(sqlite)? Can I use SQLCipher for it?

HotTag

Archive