Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/529f3a028e3c8f2640da94b13257c516 to your computer and use it in GitHub Desktop.
Save mlbd/529f3a028e3c8f2640da94b13257c516 to your computer and use it in GitHub Desktop.
<?php
// Function to generate a random encryption key
function generateEncryptionKey($length = 32)
{
return bin2hex(random_bytes($length));
}
// Function to encrypt credit card information
function encryptCreditCard($creditCardNumber, $encryptionKey)
{
$iv = openssl_random_pseudo_bytes(16); // Initialization Vector
// Encrypt the credit card number using AES-256-CBC algorithm
$encryptedCreditCard = openssl_encrypt(
$creditCardNumber,
'aes-256-cbc',
$encryptionKey,
0, // Options
$iv
);
// Return encrypted data along with the initialization vector
return base64_encode($iv . $encryptedCreditCard);
}
// Function to decrypt credit card information
function decryptCreditCard($encryptedCreditCard, $encryptionKey)
{
// Decode the base64-encoded string
$decodedData = base64_decode($encryptedCreditCard);
// Extract the initialization vector and encrypted credit card data
$iv = substr($decodedData, 0, 16);
$encryptedCreditCardData = substr($decodedData, 16);
// Decrypt the credit card number using AES-256-CBC algorithm
$decryptedCreditCard = openssl_decrypt(
$encryptedCreditCardData,
'aes-256-cbc',
$encryptionKey,
0, // Options
$iv
);
return $decryptedCreditCard;
}
// Example usage
$creditCardNumber = '4111111111111111'; // Replace with actual credit card number
$encryptionKey = generateEncryptionKey();
// Encrypt the credit card number
$encryptedCreditCard = encryptCreditCard($creditCardNumber, $encryptionKey);
// Decrypt the credit card number
$decryptedCreditCard = decryptCreditCard($encryptedCreditCard, $encryptionKey);
// Display results
echo "Original Credit Card Number: $creditCardNumber\n";
echo "Encrypted Credit Card Number: $encryptedCreditCard\n";
echo "Decrypted Credit Card Number: $decryptedCreditCard\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment