9951 explained code solutions for 126 technologies


php-awsHow can I use AWS KMS with PHP?


AWS KMS is an Amazon Web Services (AWS) service that enables you to create and manage encryption keys for data encryption and decryption. You can use AWS KMS with PHP to encrypt and decrypt data.

To use AWS KMS with PHP, you will need to install the AWS SDK for PHP. This SDK provides an API for interacting with AWS services, including AWS KMS.

Once the SDK is installed, you can use the API to create and manage encryption keys, and to encrypt and decrypt data. Here is an example of how to use the PHP SDK to encrypt and decrypt data using AWS KMS:

// Include the AWS SDK for PHP
require 'vendor/autoload.php';

// Create an AWS KMS client
$kms = new Aws\Kms\KmsClient([
    'region' => 'us-east-1',
    'version' => 'latest'
]);

// Encrypt data
$result = $kms->encrypt([
    'KeyId' => 'alias/my-key',
    'Plaintext' => 'my-secret-data'
]);
$ciphertext = $result['CiphertextBlob'];

// Decrypt data
$result = $kms->decrypt([
    'CiphertextBlob' => $ciphertext
]);
$plaintext = $result['Plaintext'];

echo $plaintext;
// Output: my-secret-data

For more information on using AWS KMS with PHP, please refer to the AWS KMS documentation.

Edit this code on GitHub