<?php 
/*  
	Sistema de Portales de Soler y Palau SA de CV
	(2021) Desarrollos y Servicios en Tecnología Inteligente S de RL de CV
	Versión 1.0,
	Última Actualización: 04/08/2021,
	Developer: José Luis Brito Nava
*/

function encriptarDatos($data){
    $public = base64_decode("7HI2Fx6qL7MNJz9k/Ebj//Hz5A/TwE810JNlzHVQaDE=");
    $secret = base64_decode("141zCWF72vmuWgiUad8ropL0LRGtL5fMGICS5u96+Zs=");

    $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);

    $encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey($secret, $public);
    $encrypted = sodium_crypto_box($data, $nonce, $encryption_key);

    $arrEnc = [base64_encode($encrypted), base64_encode($nonce)];
    $jsonEnc = json_encode($arrEnc);
    $strEnc = base64_encode($jsonEnc);
    
    return $strEnc;
}
function desencriptarDatos($data){
    $strDec = base64_decode($data);
    $jsonDec = json_decode($strDec);

    $encrypted = base64_decode($jsonDec[0]);
    $nonce = base64_decode($jsonDec[1]);

    $public = base64_decode("7HI2Fx6qL7MNJz9k/Ebj//Hz5A/TwE810JNlzHVQaDE=");
    $secret = base64_decode("141zCWF72vmuWgiUad8ropL0LRGtL5fMGICS5u96+Zs=");

    $decryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey($secret, $public);
    $decrypted = sodium_crypto_box_open($encrypted, $nonce, $decryption_key);
    
    return $decrypted;
}