We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

encrypt and decrypt URL variable

When I edit and delete any data variable or the client code for this URL parameter is sent ¿can be encrypted and decrypted with Phalcon?

Example

https://localhost/tesis/spm_contacto/edit/0401014717


77.7k
Accepted
answer

Sure:

// Template file
<a href="{{ url(['for':'spm_contacto/edit','params':crypt.encrypt(id)]) }}">Edit</a>
// Controller file
pubilc function editAction($encryptedId) {
    $id = $this->crypt->decrypt($encryptedId);
}

Docs: https://docs.phalcon.io/en/latest/reference/crypt.html



81.2k
edited Dec '15

I Excuse me, I have the following error "mcrypt extension is required" I add

<?php use Phalcon\Crypt; ?>

I solved it by enabling extension = mcrypt.so in my php.ini

Now I have this error

Encryption key cannot be empty


81.2k
edited Dec '15

I add in my config / services

$di->set('crypt', function () {

$crypt = new Crypt();

// Set a global encryption key
$crypt->setKey('%31.1e$i86e$f!8jz');

return $crypt;
}, true);

No fails but the link directs me to the same page not another

<?php echo $this->tag->linkTo(array("spm_contacto/edit/" .$this->crypt->encrypt($spm_contacto->CONT_CODIGO), "<img src='" . $this->url->get("img/tolbar/edit-validated32.png") . "'>Editar")); ?></td>


81.2k

By showing the variable encrypted with a fact shows u� i&���SeA ����u�\G�A��e�� � 4Er�vZY�' �Z�>c�낉���g�,J -���s�N�T��j�t���_�F�+0����1��v��D>��T�%#"� �������C���z D"P\�q�ӿ/Y ~SmIo�᰽�����,�'$�\P�H���M����M�.�9�J� bk�ꖸ�����X�L����l+�؋!�[m(�wi�m��{<>:��a�Q�mmxҾq��)цT�=� �I�A�PP�,Ο�g �:���F�&Hp4$Ԍc����7�O��6}�'�z��#p���8 ���3zۨ��w6]�)o?�zuQ���8�����j} E#�3ʕH��4������H`��� �� �B}�j�_J ���[email protected]+A+�E|q�+3,`N�t�-��Dn< � �GAe��*U�sU14��w�B�VM�(� E���ny�_��,I9��a+�͵��t���q��l��Kk�j��+)�����}��t��l�� G��(k��鲗��o$�ѝ�Pd3���P����Z������Yv�y�Q������kX��

My bad, use the encryptBase64 and decryptBase64 functions respectively



81.2k

To which I test if sending values sent encrypted or not

<?php $prueba=$this->crypt->encryptBase64($spm_contacto->CONT_CODIGO);
echo $prueba;
echo $this->crypt->decryptBase64($prueba);
?>

This is the result

SqVg4xBmCQIkHFT656DBiToN0Yz8k4w+wuQQfn+BtGa1bsyee3UsRJoFIX+rL1L2Zrhgl3XiDXGuEr5c4QT+Ew==0401014717 

n my controller that receives the variable is so

echo $CONT_CODIGO;
echo $this->crypt->decryptBase64(trim($CONT_CODIGO));
$spm_contacto = SpmContacto::findFirstByCONT_CODIGO($this->crypt->decryptBase64($CONT_CODIGO));
    if (!$spm_contacto) {
        $this->flash->error("spm_contacto was not found");

        return $this->dispatcher->forward(array(
            "controller" => "spm_contacto",
            "action" => "index"
        ));
    }

But the result is

C9YNI1bl4LQ0OBVoyMSybdVv4�8���C�9��K2�sspm_contacto was not found