Cäsar-Chiffre: Quellcode


Dies ist der gewünschte Quellcode zum Ver-/Entschlüsseln der Cäsar-Chiffre in PHP. Er liegt direkt als Funktion vor, ist also sofort einsetzbar. Der Code ist öffentlich zugänglich, da ich der Meinung bin, dass Quellcode immer OpenSource sein sollte.

  Zurück zum Tool    Erklärung der ‘Cäsar-Chiffre’

PHP-Implementierung:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if($richtung=="ent"){
//ent=Entschluesseln, am besten mit select-Element (HTML)
$key=$key*(-1);
//negierung zum Entschlüsseln (-k nach Rechts verschieben)
}
   $encrypted="";
   for ($i=0;$i<strlen($text); $i++){
      $nextchar=ord(substr($text,$i,1))+$key;
      if($nextchar>90 && $nextchar < 97){
         $nextchar = 96 + $nextchar - 90;
      }
      if ($nextchar < 65){
         $nextchar = 65;
      }
      if ($nextchar > 122){
         $nextchar = 64 + $nextchar - 122;
      }
      $encrypted.= chr($nextchar);
   }
   echo $encrypted; //Ver- bzw. Entschlüsselter Text
}

Facebook Twitter RSS MyNameIsE YouTube