Home page / PHP Tips'n'tricks

Shiftare l' ASCII

Offuscare dati e' molto importante. Se si hanno a disposizione sistemi di criptatura "strong" tipo GPG, PGP, Crypto o altri conviene SEMPRE usare quelli. Nel caso in cui ci si trovi nell' esigenza di dover fare tutto senza questi preziosi strumenti si puo' scrivere qualche algoritmo "all' acqua di rose" ma che comunque scoraggia chi non esperto si trova davanti a caratteri che non comprende.

Istanziare il coderino ...

$myCoder = new Encoder();

$myCoder->key = md5('lamiasegretissimachiave');

$myCoder->numberX = 17; // scaramanticamente

 

Offuscare:

$stringaOffuscata = $myCoder->Encode($laMiaPassword);

 

Rivedere:

$laMiaPassword = $myCoder->Decode($stringaOffuscata);

 

/////////////////////////////// CODING
class Encoder{
    var 
$key;
    var 
$numberX;

    function 
Encoder(){
        
$this->numberX=144;
        
$this->key="la tua chiave";
    }
    function 
Encode($strToEncode){
        
$strEncoded="";    $y 0;
        for (
$i 0$i strlen($strToEncode); $i++){
            
$letter substr($strToEncode$i1);
            if (
$y >= strlen($this->key))
                
$y -= strlen($this->key);
            
$l substr($this->key$y1);
            
$y++;
            
$NewC = (ord($letter)+$this->numberX) + ord($l);
            while (
$NewC 255)    $NewC -= 255;
            
$strEncoded .= chr($NewC);
        }
        return 
$strEncoded;
    }    
    function 
Decode($strEncoded){
        
$strDecoded=""$y 0;
        for (
$i 0$i strlen($strEncoded); $i++){
            
$letter substr($strEncoded$i1);
            if (
$y >= strlen($this->key))
                
$y -= strlen($this->key);
            
$l substr($this->key$y1);
            
$y++;
            
$NewC = (ord($letter)-$this->numberX) - ord($l);
            while (
$NewC 0$NewC += 255;
            
$strDecoded .= chr($NewC);
        }
        return 
$strDecoded;
    }
}
/////////////////////////////// END CODING


Autore: #ffffff (10-02-2007 00:00:00)

07-01-2009 - PM 02:53