gmlscripts

vigenere_ascii (PHP)

May 16th, 2021
613
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// vigenere_ascii(in,key,mode)
  2. //
  3. //  Returns the given string enciphered or deciphered
  4. //  using a simple Vigenere style cipher, and filtering
  5. //  out non-printable characters.
  6. //
  7. //      in          input, string
  8. //      key         enciphering key, string
  9. //      mode        0 = decipher, 1 = encipher
  10. //
  11. /// GMLscripts.com/license
  12. function vigenere_ascii($in, $key, $mode)
  13. {
  14.     $out = "";
  15.     $inLen = strlen($in);
  16.     $keyLen = strlen($key);
  17.     $loVal = 32;
  18.     $hiVal = 126;
  19.     $span = ($hiVal - $loVal) + 1;
  20.     for ($pos=0; $pos<$inLen; $pos+=1) {
  21.         $inChar = substr($in, $pos, 1);
  22.         $keyChar = substr($key, $pos % $keyLen, 1);
  23.         $inVal = min(max($loVal, ord($inChar)), $hiVal) - $loVal;
  24.         $keyVal = min(max($loVal, ord($keyChar)), $hiVal) - $loVal;
  25.         if ($mode) {
  26.             $outVal = (($inVal + $keyVal) % $span) + $loVal;
  27.         }else{
  28.             $outVal = (($span + $inVal - $keyVal) % $span) + $loVal;
  29.         }
  30.         $outChar = chr($outVal);
  31.         $out = $out . $outChar;
  32.     }
  33.     return $out;
  34. }
RAW Paste Data