Skip to content

Instantly share code, notes, and snippets.

@catwhocode
Created June 16, 2022 06:44
Show Gist options
  • Save catwhocode/93218aa066d9f381e0d8e990143c808d to your computer and use it in GitHub Desktop.
Save catwhocode/93218aa066d9f381e0d8e990143c808d to your computer and use it in GitHub Desktop.
Encryt and Decrypt with Defuse
<?php
require 'defuse-crypto.phar';
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
function loadEncryptionKeyFromConfig()
{
$keyAscii = 'def00000288b40f6e9f784932da638b3a01079fd620f75f8ac77e3999dbf0dc76b96fef7a52bb1b931196f1693a45a0921373d2b0542e70f0c1abc399a849ce4e174e2e9';
return Key::loadFromAsciiSafeString($keyAscii);
}
$key = loadEncryptionKeyFromConfig();
$teksAsli = 'Aa Ezha Ganteng';
$ciphertext = Crypto::encrypt($teksAsli, $key);
echo 'Teks asli: ' . $teksAsli . "\n";
echo 'Ciphertext: ' . $ciphertext . "\n";
try {
$secret_data = Crypto::decrypt($ciphertext, $key);
echo 'Secret data: ' . $secret_data . "\n";
} catch (\Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException $ex) {
echo 'wrong key' . "\n";
echo 'ciphertext has changed since it was created' . "\n";
echo 'ciphertext is corrupted' . "\n";
echo 'ciphertext is intentionally modified' . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment