| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | function snafCrypt($str) {
    $seeds = array_merge ( range('A', 'Z'), range('0', '9') );
    $crypted = '';
    foreach(str_split($str) as $letter) {
        $offset = (int) array_search($letter, $seeds) + 5;
        if ($offset>count($seeds)-1) $offset -= count($seeds);
        $crypted .= $seeds[$offset];
    }
    return $crypted;
}
 
function snafDecrypt($str) {
    $seeds = array_merge ( range('A', 'Z'), range('0', '9') );
    $clear = '';
    foreach(str_split($str) as $letter) {
        $offset = (int) array_search($letter, $seeds) - 5;
        if ($offset<0) $offset += count($seeds);
        $clear .= $seeds[$offset];
    }
    return $clear;
}
 
print snafCrypt('ACG52K');
print "\n";
print snafDecrypt('ACG52K');
print "\n";
print snafDecrypt(snafCrypt('AZ159'));
print "\n"; | 
Partager