/system/library/encryption.php
PHP | 47 lines | 35 code | 12 blank | 0 comment | 4 complexity | b69920de29322296604622ce582d7a2b MD5 | raw file
- <?php
- final class Encryption {
- private $key;
-
- function __construct($key) {
- $this->key = $key;
- }
-
- function encrypt($value) {
- if (!$this->key) {
- return $value;
- }
-
- $output = '';
-
- for ($i = 0; $i < strlen($value); $i++) {
- $char = substr($value, $i, 1);
- $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1);
- $char = chr(ord($char) + ord($keychar));
-
- $output .= $char;
- }
-
- return base64_encode($output);
- }
-
- function decrypt($value) {
- if (!$this->key) {
- return $value;
- }
-
- $output = '';
-
- $value = base64_decode($value);
-
- for ($i = 0; $i < strlen($value); $i++) {
- $char = substr($value, $i, 1);
- $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1);
- $char = chr(ord($char) - ord($keychar));
-
- $output .= $char;
- }
-
- return $output;
- }
- }
- ?>