Skip to content

Instantly share code, notes, and snippets.

@ElisaBaum
Last active June 19, 2017 13:34
Show Gist options
  • Save ElisaBaum/c4980fdade93c4310401096025cb9bab to your computer and use it in GitHub Desktop.
Save ElisaBaum/c4980fdade93c4310401096025cb9bab to your computer and use it in GitHub Desktop.
Util to decrypt AWS KMS encrypted keys
import java.nio.ByteBuffer
import java.nio.charset.Charset
import com.amazonaws.services.kms.AWSKMSClientBuilder
import com.amazonaws.services.kms.model.DecryptRequest
import com.amazonaws.util.Base64
object KMSDecryptionUtil {
private lazy val kmsClient = AWSKMSClientBuilder.defaultClient()
def decrypt(encryptedValue: String): String = {
val encrypted = Base64.decode(encryptedValue)
val decryptedResult = kmsClient
.decrypt(new DecryptRequest().withCiphertextBlob(ByteBuffer.wrap(encrypted)))
.getPlaintext
new String(decryptedResult.array(), Charset.forName("UTF-8"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment