-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess.php
More file actions
80 lines (76 loc) · 1.79 KB
/
guess.php
File metadata and controls
80 lines (76 loc) · 1.79 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
<?php
$tmp = json_decode(file_get_contents('php://input'), true);
function getFrequencyShift($str)
{
$frequency=array_fill(0,26,0);
$length=strlen($str);
$arr=array('a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z');
$arr2=array('A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
for($i=0;$i<26;$i++)
for($j=0;$j<$length;$j++)
if(($str[$j]==$arr[$i]) || ($str[$j]==$arr2[$i]))
$frequency[$i]++;
$maxFrequencyIndex=-1;
$maxFrequency=-1;
for($i=0;$i<26;$i++)
if($frequency[$i]>$maxFrequency)
{
$maxFrequency=$frequency[$i];
$maxFrequencyIndex=$i;
}
if($maxFrequencyIndex!=-1)
$shift=$maxFrequencyIndex-4;
return $shift;
}
function shift($char,$sh)
{
$arr=array('a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z');
$arr2=array('A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$n=count($arr);
while($sh>$n-1)
$sh-=$n;
$index=-1;
for($i=0;$i<$n;$i++)
if($arr[$i]==$char)
{
$index=$i;
{
$shift=$index-$sh;
while($shift<0)
$shift+=$n;
return $arr[$shift];
}
}
else if($arr2[$i]==$char)
{
$index=$i;
{
$shift=$index-$sh;
while($shift<0)
$shift+=$n;
return $arr2[$shift];
}
}
if($index==-1)
{
return $char;
}
}
function encrypt_message($str)
{
$sh=getFrequencyShift($str);
$length=strlen($str);
for ($i=0;$i<$length;$i++)
{
$res[$i]=shift($str[$i],$sh);
}
$tmp['result']=implode($res);
$tmp['shift']=$sh;
echo json_encode($tmp);
}
encrypt_message($tmp['source']);
?>