Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. function dsCrypt($input,$decrypt=false) {
  2. $o = $s1 = $s2 = array(); // Arrays for: Output, Square1, Square2
  3. // формируем базовый массив с набором символов
  4. $basea = array('?','(','@',';','$','#',"]","&",'*'); // base symbol set
  5. $basea = array_merge($basea, range('a','z'), range('A','Z'), range(0,9) );
  6. $basea = array_merge($basea, array('!',')','_','+','|','%','/','[','.',' ') );
  7. $dimension=9; // of squares
  8. for($i=0;$i<$dimension;$i++) { // create Squares
  9. for($j=0;$j<$dimension;$j++) {
  10. $s1[$i][$j] = $basea[$i*$dimension+$j];
  11. $s2[$i][$j] = str_rot13($basea[($dimension*$dimension-1) - ($i*$dimension+$j)]);
  12. }
  13. }
  14. unset($basea);
  15. $m = floor(strlen($input)/2)*2; // !strlen%2
  16. $symbl = $m==strlen($input) ? '':$input[strlen($input)-1]; // last symbol (unpaired)
  17. $al = array();
  18. // crypt/uncrypt pairs of symbols
  19. for ($ii=0; $ii<$m; $ii+=2) {
  20. $symb1 = $symbn1 = strval($input[$ii]);
  21. $symb2 = $symbn2 = strval($input[$ii+1]);
  22. $a1 = $a2 = array();
  23. for($i=0;$i<$dimension;$i++) { // search symbols in Squares
  24. for($j=0;$j<$dimension;$j++) {
  25. if ($decrypt) {
  26. if ($symb1===strval($s2[$i][$j]) ) $a1=array($i,$j);
  27. if ($symb2===strval($s1[$i][$j]) ) $a2=array($i,$j);
  28. if (!empty($symbl) && $symbl===strval($s2[$i][$j])) $al=array($i,$j);
  29. }
  30. else {
  31. if ($symb1===strval($s1[$i][$j]) ) $a1=array($i,$j);
  32. if ($symb2===strval($s2[$i][$j]) ) $a2=array($i,$j);
  33. if (!empty($symbl) && $symbl===strval($s1[$i][$j])) $al=array($i,$j);
  34. }
  35. }
  36. }
  37. if (sizeof($a1) && sizeof($a2)) {
  38. $symbn1 = $decrypt ? $s1[$a1[0]][$a2[1]] : $s2[$a1[0]][$a2[1]];
  39. $symbn2 = $decrypt ? $s2[$a2[0]][$a1[1]] : $s1[$a2[0]][$a1[1]];
  40. }
  41. $o[] = $symbn1.$symbn2;
  42. }
  43. if (!empty($symbl) && sizeof($al)) // last symbol
  44. $o[] = $decrypt ? $s1[$al[1]][$al[0]] : $s2[$al[1]][$al[0]];
  45. return implode('',$o);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement