Repository navigation
Expand file tree
/
Copy pathfuncs_php_crypt.php
More file actions
128 lines (116 loc) · 4.59 KB
/
Copy pathfuncs_php_crypt.php
File metadata and controls
128 lines (116 loc) · 4.59 KB
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
//функция для обратимого шифрования, в случае ошибки расшифровки false
//Author: Vladimir Kim (www.vkim.ru) 2010
//Free for use
function encodeKey($String, $Password)
{
if (!$Password) die ("Не задан пароль шифрования");
$Salt='BGuxLWQtKweKEMV4';
$String = substr(pack("H*",sha1($String)),0,1).$String;
$StrLen = strlen($String);
$Seq = $Password;
$Gamma = '';
while (strlen($Gamma)< $StrLen)
{
$Seq = pack("H*",sha1($Seq.$Gamma.$Salt));
$Gamma.=substr($Seq,0,8);
}
return base64_encode($String^$Gamma);
}
function decodeKey($String, $Password,$validate=false)
{
if (!$Password) die ("Не задан пароль для расшифровки");
$Salt='BGuxLWQtKweKEMV4';
$StrLen = strlen($String);
$Seq = $Password;
$Gamma = '';
while (strlen($Gamma)<$StrLen)
{
$Seq = pack("H*",sha1($Seq.$Gamma.$Salt));
$Gamma.=substr($Seq,0,8);
}
$String = base64_decode($String);
$String = $String^$Gamma;
$DecodedString = substr($String, 1);
$Error=false;
if ($validate) $Error = ord(substr($String, 0, 1)
^ substr(pack("H*",sha1($DecodedString)),0,1));
//проверяем
if ($Error) return false;
else return $DecodedString;
}
/**
* функция для обратимого шифрования
* @String Строка с исходным текстом
* @Password ключ для дешифрования
* для расшифровки использовать функцию еще раз
*/
/*
function encode($String, $Password)
{
$Salt='BGuxLWQtKweKEMV4';
$StrLen = strlen($String);
$Seq = $Password;
$Gamma = '';
while (strlen($Gamma)<$StrLen)
{
$Seq = pack("H*",sha1($Gamma.$Seq.$Salt));
$Gamma.=substr($Seq,0,8);
}
return $String^$Gamma;
}
*/
/**
* Обратимое шифрование методом "Двойного квадрата" (Reversible crypting of "Double square" method)
* @param String $input Строка с исходным текстом
* @param bool $decrypt Флаг для дешифрования
* @return String Строка с результатом Шифрования|Дешифрования
* @author runcore
*/
function dsCrypt($input,$decrypt=false) {
$o = $s1 = $s2 = array(); // Arrays for: Output, Square1, Square2
// формируем базовый массив с набором символов
$basea = array('?','(','@',';','$','#',"]","&",'*'); // base symbol set
$basea = array_merge($basea, range('a','z'), range('A','Z'), range(0,9) );
$basea = array_merge($basea, array('!',')','_','+','|','%','/','[','.',' ') );
$dimension=9; // of squares
for($i=0;$i<$dimension;$i++) { // create Squares
for($j=0;$j<$dimension;$j++) {
$s1[$i][$j] = $basea[$i*$dimension+$j];
$s2[$i][$j] = str_rot13($basea[($dimension*$dimension-1) - ($i*$dimension+$j)]);
}
}
unset($basea);
$m = floor(strlen($input)/2)*2; // !strlen%2
$symbl = $m==strlen($input) ? '':$input[strlen($input)-1]; // last symbol (unpaired)
$al = array();
// crypt/uncrypt pairs of symbols
for ($ii=0; $ii<$m; $ii+=2) {
$symb1 = $symbn1 = strval($input[$ii]);
$symb2 = $symbn2 = strval($input[$ii+1]);
$a1 = $a2 = array();
for($i=0;$i<$dimension;$i++) { // search symbols in Squares
for($j=0;$j<$dimension;$j++) {
if ($decrypt) {
if ($symb1===strval($s2[$i][$j]) ) $a1=array($i,$j);
if ($symb2===strval($s1[$i][$j]) ) $a2=array($i,$j);
if (!empty($symbl) && $symbl===strval($s2[$i][$j])) $al=array($i,$j);
}
else {
if ($symb1===strval($s1[$i][$j]) ) $a1=array($i,$j);
if ($symb2===strval($s2[$i][$j]) ) $a2=array($i,$j);
if (!empty($symbl) && $symbl===strval($s1[$i][$j])) $al=array($i,$j);
}
}
}
if (sizeof($a1) && sizeof($a2)) {
$symbn1 = $decrypt ? $s1[$a1[0]][$a2[1]] : $s2[$a1[0]][$a2[1]];
$symbn2 = $decrypt ? $s2[$a2[0]][$a1[1]] : $s1[$a2[0]][$a1[1]];
}
$o[] = $symbn1.$symbn2;
}
if (!empty($symbl) && sizeof($al)) // last symbol
$o[] = $decrypt ? $s1[$al[1]][$al[0]] : $s2[$al[1]][$al[0]];
return implode('',$o);
}
?>