/
encryptormini.php
163 lines (142 loc) · 5.17 KB
/
encryptormini.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
class encryption_class
{
var $scramble1; // 1st string of ASCII characters
var $scramble2; // 2nd string of ASCII characters
var $errors; // array of error messages
var $adj; // 1st adjustment value (optional)
var $mod; // 2nd adjustment value (optional)
function encryption_class ()
{
// Each of these two strings must contain the same characters, but in a different order.
// Use only printable characters from the ASCII table.
// Do not use single quote, double quote or backslash as these have special meanings in PHP.
// Each character can only appear once in each string.
$this->scramble1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghi0jklmnopqrstuvwxyz';
$this->scramble2 = 'lpokjih5umnbygvr1dtcfxeaw7q2szQS0WDE38FRGTHYJUK94ILOPAZ6MCBXNV';
if (strlen($this->scramble1) <> strlen($this->scramble2)) {
trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
} // if
$this->adj = 1.75; // this value is added to the rolling fudgefactors
$this->mod = 3; // if divisible by this the adjustment is made negative
} // constructor
function encrypt ($key, $source, $sourcelen=0)
{
$this->errors = array();
$fudgefactor = $this->_convertKey($key);
if ($this->errors) return $this->errors;
if (empty($source))
{
$this->errors[] = 'No value has been supplied for encryption';
return;
} // if
while (strlen($source) < $sourcelen)
{
$source .= ' ';
} // while
$target = NULL;
$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++)
{
$char1 = substr($source, $i, 1);
$num1 = strpos($this->scramble1, $char1);
if ($num1 === false)
{
$this->errors[] = "Source string contains an invalid character ($char1)";
return;
} // if
$adj = $this->_applyFudgeFactor($fudgefactor);
$factor1 = $factor2 + $adj; // accumulate in $factor1;
$num2 = round($factor1) + $num1; // generate offset for $scramble2
$num2 = $this->_checkRange($num2); // check range
$factor2 = $factor1 + $num2; // accumulate in $factor
$char2 = substr($this->scramble2, $num2, 1);
$target .= $char2;
} // for
return $target;
} // encrypt
function _convertKey ($key)
{
if (empty($key))
{
$this->errors[] = 'No value has been supplied for the encryption key';
return;
} // if
$array[] = strlen($key);
$tot = 0;
for ($i = 0; $i < strlen($key); $i++)
{
$char = substr($key, $i, 1);
$num = strpos($this->scramble1, $char);
if ($num === false)
{
$this->errors[] = "Key contains an invalid character ($char)";
return;
} // if
$array[] = $num;
$tot = $tot + $num;
} // for
$array[] = $tot;
return $array;
} // _convertKey
function _applyFudgeFactor (&$fudgefactor)
{
$fudge = array_shift($fudgefactor);
$fudge = $fudge + $this->adj;
$fudgefactor[] = $fudge;
if (!empty($this->mod))
{ // if modifier has been supplied
if ($fudge % $this->mod == 0)
{ // if it is divisible by modifier
$fudge = $fudge * -1; // reverse then sign
} // if
} // if
return $fudge;
} // _applyFudgeFactor
function _checkRange ($num)
{
$num = round($num);
$limit = strlen($this->scramble1);
while ($num >= $limit)
{
$num = $num - $limit;
} // while
while ($num < 0)
{
$num = $num + $limit;
} // while
return $num;
} // _checkRange
function decrypt ($key, $source)
{
$this->errors = array();
$fudgefactor = $this->_convertKey($key);
if ($this->errors) return;
if (empty($source))
{
$this->errors[] = 'No value has been supplied for decryption';
return;
} // if
$target = NULL;
$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++)
{
$char2 = substr($source, $i, 1);
$num2 = strpos($this->scramble2, $char2);
if ($num2 === false)
{
$this->errors[] = "Source string contains an invalid character ($char2)";
return;
} // if
$adj = $this->_applyFudgeFactor($fudgefactor);
$factor1 = $factor2 + $adj;
$num1 = $num2 - round($factor1); // generate offset for $scramble1
$num1 = $this->_checkRange($num1); // check range
$factor2 = $factor1 + $num2; // accumulate in $factor2
$char1 = substr($this->scramble1, $num1, 1);
$target .= $char1;
} // for
return rtrim($target);
} // decrypt
}
?>