-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaesar-Cipher.php
More file actions
58 lines (58 loc) · 1.86 KB
/
Caesar-Cipher.php
File metadata and controls
58 lines (58 loc) · 1.86 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
<?php
/**
* @file Class realize Caesar encryption metods.
* @class caesarCipher
* @author Full_droper <full_droper@pm.me>
* @version 0.0.1
*/
class caesarCipher {
/**
* Encode input data by Caesar Cipher.
* @name encode
* @param {string} rawData - raw data for encode.
* @param {number\string} key - (default 1) count of shift.
* @return {string} The encoded by Caesar Cipher data
*/
function encode($rawData,$key = 1) {
if (
empty($rawData) ||
gettype($rawData) != "string" ||
(gettype($key) != "integer" &&
gettype($key) != "string")
) return null;
if (gettype($key) == "string") $key = intval($key,10);
if ($key < 1 ) $key = 1;
$data = str_split($rawData);
foreach ($data as &$value) {
$value = chr(ord($value) + $key);
}
return implode("",$data);
}
/**
* Decode input data by Caesar Cipher.
* @name decode
* @param {string} rawData - raw data for decode.
* @param {number\string} key - (default 1) count of shift.
* @return {string} The decoded by Caesar Cipher data
*/
function decode($rawData,$key = 1) {
if (
empty($rawData) ||
gettype($rawData) != "string" ||
(gettype($key) != "integer" &&
gettype($key) != "string")
) return null;
if (gettype($key) == "string") $key = intval($key,10);
if ($key < 1 ) $key = 1;
$data = str_split($rawData);
foreach ($data as &$value) {
$value = chr(ord($value) - $key);
}
return implode("",$data);
}
}
// $conv = new caesarCipher();
// $str1 = $conv->encode("Hello world", 2);
// $str2 = $conv->decode($str1,2);
// echo "Hello world -> ".$str1." -> ".$str2;
?>