Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ghostrocket
Forked from lysender/using-aws-kms.php
Created December 22, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostrocket/5507e23618bee8b58cef573ff98f5d87 to your computer and use it in GitHub Desktop.
Save ghostrocket/5507e23618bee8b58cef573ff98f5d87 to your computer and use it in GitHub Desktop.
Using AWS KMS API via PHP SDK
<?php
use Aws\Kms\KmsClient;
// Somewhere in the controller or model
$this->load->config('aws');
// Not needed for EC2 instance role based authorization - for my local instance only
$key = $this->config->item('aws_s3_access_key');
$secret = $this->config->item('aws_s3_secret_key');
$orig = 'encrypt me please...';
$cryptic = 'CiD/AT9S0xQbpFXHDdw7Mq42htuEVj0vwvZzfR+9GRZCahKbAQEBAgB4/wE/UtMUG6RVxw3cOzKuNobbhFY9L8L2c30fvRkWQmoAAAByMHAGCSqGSIb3DQEHBqBjMGECAQAwXAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAxvNDVWPh6W4STdWakCARCAL/nzjIDZ8uQWAMWI1VBoNPt+TCe9qZMMbY1d1PnVjlJGa/BcVdAyN9KruzEOcFl6';
// Testing the encrypt and decrypt cycle
$kms = KmsClient::factory([
'credentials' => [
'key' => $key,
'secret' => $secret,
],
'region' => 'us-east-1',
]);
// Encrypt - should match $cryptic
$result = $kms->encrypt([
'KeyId' => 'alias/argus-db-crypt-local',
'Plaintext' => $orig,
]);
var_dump(base64_encode($result->get('CiphertextBlob')));
// Decrypt - should match $orig
$result = $kms->decrypt([
'CiphertextBlob' => base64_decode($cryptic),
]);
var_dump($result->get('Plaintext'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment