Questa è una funzione presa pari pari dal manuale ufficiale.
Permette una volta criptata la stringa di decriptarla basandosi su una chiave che decidi tu.
Codice PHP:
function encrypt($s, $key)
{
for($i=0;$i<=strlen($s);$i++)
$r.=substr(str_shuffle(md5($key)),($i % strlen(md5($key))),1).$s[$i];
for($i=1;$i<=strlen($r);$i++) $s[$i-1]=chr(ord($r[$i-1])+ord(substr(md5($key),($i % strlen(md5($key)))-1,1)));
return urlencode(base64_encode($s));
}
function decrypt($s, $key)
{
$s=base64_decode(urldecode($s));
for($i=1;$i<=strlen($s);$i++) $s[$i-1]=chr(ord($s[$i-1])-ord(substr(md5($key),($i % strlen(md5($key)))-1,1)));
for($i=1;$i<=strlen($s)-2;$i=$i+2) $r.=$s[$i];
return $r;
}
$key='chiave'; // qui inserisci la chiave di codifica/decodifica
$testoDaCriptare = "Questo è il mio codice in chiaro 1AFG3J";
echo $testoDaCriptare;
echo "
";
$encrypted = encrypt($testoDaCriptare,$key); //encrypts the data using the key
echo "$encrypted<hr>";
echo "
";
$decrypted = decrypt($encrypted,$key); //decrypts the data using the key
echo $decrypted;