<?php
require_once('/xampp/php/lib/defuse-crypto.phar');
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
class Crypt {
private $key; public function __construct() {
// make sure the following code points to a file that exists
// and contains a valid key
$keyAscii = file_get_contents('/xampp/php/defuse-key.txt');
$this->key = Key::loadFromAsciiSafeString($keyAscii);
}
public function encrypt($data) {
$encrypted_data = Crypto::encrypt($data, $this->key);
return $encrypted_data;
}
public function decrypt($encrypted_data) {
try {
$data = Crypto::decrypt($encrypted_data, $this->key);
return $data;
} catch (WrongKeyOrModifiedCiphertextException $ex) {
throw new Exception($ex->getMessage());
}
}
} ?>
<?php
require 'crypt.php';
$credit_card_no = '4111111111111111';
// Create the Crypt object
$crypt = new Crypt();
// Use the Crypt object to encrypt the data
$encrypted_data = $crypt->encrypt($credit_card_no);
echo("Encrypted data: $encrypted_data<br />>");
// Use the Crypt object to decrypt the data
try {
$decrypted_data = $crypt->decrypt($encrypted_data);
echo"Decrypted data: $decrypted_data<br />>");
} catch (Exception $ex) {
echo("Exception: " . $ex->getMessage() . "<br />");
}