Guest User

Untitled

a guest
May 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Rotate each string characters by n positions in ASCII table
  5. * To encode use positive n, to decode - negative.
  6. * With n = 13 (ROT13), encode and decode n can be positive.
  7. *
  8. * @param string $string
  9. * @param integer $n
  10. * @return string
  11. */
  12. function rotate($string, $n) {
  13.  
  14. $length = strlen($string);
  15. $result = '';
  16.  
  17. for($i = 0; $i < $length; $i++) {
  18. $ascii = ord($string{$i});
  19.  
  20. $rotated = $ascii;
  21.  
  22. if ($ascii > 64 && $ascii < 91) {
  23. $rotated += $n;
  24. $rotated > 90 && $rotated += -90 + 64;
  25. $rotated < 65 && $rotated += -64 + 90;
  26. } elseif ($ascii > 96 && $ascii < 123) {
  27. $rotated += $n;
  28. $rotated > 122 && $rotated += -122 + 96;
  29. $rotated < 97 && $rotated += -96 + 122;
  30. }
  31.  
  32. $result .= chr($rotated);
  33. }
  34.  
  35. return $result;
  36. }
  37.  
  38. $enc = rotate('j-o-s-h-[-a-t-]-t-h-e-n-u-l-l-b-y-t-e-[-d-o-t-]-o-r-g', rand(1,25));
  39.  
  40. ?>
Add Comment
Please, Sign In to add comment