Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active July 19, 2019 18:34
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 parzibyte/c778bdcf91af0b3163082afab9dbf0e1 to your computer and use it in GitHub Desktop.
Save parzibyte/c778bdcf91af0b3163082afab9dbf0e1 to your computer and use it in GitHub Desktop.
Decrypt data with PHP using php-encryption https://parzibyte.me/blog
def0000033794dc126bf1be7994dab4ec8db7dcd8efb32308a70fa592b7a41cb231494dc6a1689639b006796bec780132bbfa1c7b7415d15d8c797ca107b86e74e9e3cba
<?php
/**
* Descifrar datos con PHP usando php-encryption
*
* https://parzibyte.me/blog
*
*/
require_once "vendor/autoload.php";
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use Defuse\Crypto\Key\Exception\WrongKeyOrModifiedCiphertextException;
// El mensaje o datos cifrados; los cuales deberían recuperarse de una base de datos
$mensajeCifrado = "def5020008531ed581af5efd03d66bb886f85ceba5182ae7479af6921cc3a7a69f3418b7744bdbefe39e4d79a0e0b1ee1d89843d5ba7823ea1a9ae1c95d55f08eafe214554b8cb6667727d43768eabed8d67beb6f756cecef7d63eeecbdaf779b86c95c2aebac725f4f39ac48438c2c2859e6c8e0e5f121c29a6";
// No olvides guardar la clave en un lugar seguro; aquí lo pongo así de simple para ejemplos de
// simplicidad
$contenido = file_get_contents("clave.txt");
// Cargar la clave desde una cadena ASCII (pues la clave no es tan legible ni entendible como una simple cadena)
$clave = Key::loadFromAsciiSafeString($contenido);
// Y ya podemos descifrar datos. El método decrypt lanza una excepción si detecta
// datos corruptos o una clave incorrecta
try {
$mensajeOriginal = Crypto::decrypt($mensajeCifrado, $clave);
// Este mensaje es el original
echo $mensajeOriginal;
} catch (WrongKeyOrModifiedCiphertextException $e) {
exit("Los datos están corruptos o la clave es incorrecta");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment