27 $this->errors = array();
33 $this->scramble1 =
'! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
34 $this->scramble2 =
'f^jAE]okIOzU[2&q1{3`h5w_794p@6s8?BgP>dFV=m D<TcS%Ze|r:lGK/uCy.Jx)HiQ!#$~(;Lt-R}Ma,NvW+Ynb*0X';
36 if (strlen($this->scramble1) <> strlen($this->scramble2)) {
37 trigger_error(
'** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
46 function decrypt ($key, $source)
49 $this->errors = array();
52 $fudgefactor = $this->_convertKey($key);
53 if ($this->errors)
return;
56 $this->errors[] =
'No value has been supplied for decryption';
63 for ($i = 0; $i < strlen($source); $i++) {
65 if (function_exists(
'mb_substr')) {
66 $char2 = mb_substr($source, $i, 1);
68 $char2 = substr($source, $i, 1);
72 $num2 = strpos($this->scramble2, $char2);
73 if ($num2 ===
false) {
74 $this->errors[] =
"Source string contains an invalid character ($char2)";
79 $adj = $this->_applyFudgeFactor($fudgefactor);
81 $factor1 = $factor2 + $adj;
82 $num1 = $num2 - round($factor1);
83 $num1 = $this->_checkRange($num1);
84 $factor2 = $factor1 + $num2;
87 if (function_exists(
'mb_substr')) {
88 $char1 = mb_substr($this->scramble1, $num1, 1);
90 $char1 = substr($this->scramble1, $num1, 1);
100 return rtrim($target);
105 function encrypt ($key, $source, $sourcelen = 0)
108 $this->errors = array();
111 $fudgefactor = $this->_convertKey($key);
112 if ($this->errors)
return;
114 if (empty($source)) {
115 $this->errors[] =
'No value has been supplied for encryption';
120 $source = str_pad($source, $sourcelen);
125 for ($i = 0; $i < strlen($source); $i++) {
127 if (function_exists(
'mb_substr')) {
128 $char1 = mb_substr($source, $i, 1);
130 $char1 = substr($source, $i, 1);
134 $num1 = strpos($this->scramble1, $char1);
135 if ($num1 ===
false) {
136 $this->errors[] =
"Source string contains an invalid character ($char1)";
141 $adj = $this->_applyFudgeFactor($fudgefactor);
143 $factor1 = $factor2 + $adj;
144 $num2 = round($factor1) + $num1;
145 $num2 = $this->_checkRange($num2);
146 $factor2 = $factor1 + $num2;
149 if (function_exists(
'mb_substr')) {
150 $char2 = mb_substr($this->scramble2, $num2, 1);
152 $char2 = substr($this->scramble2, $num2, 1);
167 function getAdjustment ()
175 function getModulus ()
183 function setAdjustment ($adj)
186 $this->adj = (float)$adj;
191 function setModulus ($mod)
194 $this->mod = (int)abs($mod);
201 function _applyFudgeFactor (&$fudgefactor)
205 $fudge = array_shift($fudgefactor);
206 $fudge = $fudge + $this->adj;
207 $fudgefactor[] = $fudge;
209 if (!empty($this->mod)) {
210 if ($fudge % $this->mod == 0) {
211 $fudge = $fudge * -1;
220 function _checkRange ($num)
225 $limit = strlen($this->scramble1);
227 while ($num >= $limit) {
228 $num = $num - $limit;
231 $num = $num + $limit;
239 function _convertKey ($key)
243 $this->errors[] =
'No value has been supplied for the encryption key';
247 $array[] = strlen($key);
250 for ($i = 0; $i < strlen($key); $i++) {
252 if (function_exists(
'mb_substr')) {
253 $char = mb_substr($key, $i, 1);
255 $char = substr($key, $i, 1);
259 $num = strpos($this->scramble1, $char);
260 if ($num ===
false) {
261 $this->errors[] =
"Key contains an invalid character ($char)";