Crypt.php
114 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Shlomi Bazak |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\Encryption; |
| 12 | |
| 13 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 14 | |
| 15 | class Crypt { |
| 16 | |
| 17 | const CIPHER_METHOD = "AES-256-CBC"; |
| 18 | const CIPHER_METHOD_V1 = "AES-128-CTR"; |
| 19 | const DELIMITER = "|"; |
| 20 | const CURRENT_VERSION = "v2"; |
| 21 | |
| 22 | private string $iv; // The iv used for encryption, decryption |
| 23 | private string $key=''; // The key used for encryption, decryption |
| 24 | private bool $useBase64=false; // Boolean, eather encrypt/decrypt with assist of base64 string. |
| 25 | |
| 26 | /** |
| 27 | * @param string $key |
| 28 | * @param bool $useBase64 |
| 29 | */ |
| 30 | private function __construct(string $key='', bool $useBase64=true) { |
| 31 | $this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::CIPHER_METHOD)); |
| 32 | $this->setKey( $key ); |
| 33 | $this->useBase64($useBase64); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param bool $use |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public function useBase64(bool $use=true):void { |
| 42 | $this->useBase64 = $use; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $key |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public function setKey(string $key):void { |
| 51 | $this->key = $key; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param string $data |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | private function tobase64(string $data):string { |
| 60 | if($this->useBase64) return base64_encode($data); |
| 61 | return $data; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param string $data |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | private function fromBase64(string $data):string { |
| 70 | if($this->useBase64) return base64_decode($data); |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param string $data |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function _encrypt(string $data):string { |
| 80 | return |
| 81 | $this->tobase64($this->iv) . |
| 82 | self::DELIMITER . |
| 83 | $this->tobase64(openssl_encrypt($data, self::CIPHER_METHOD, $this->key, OPENSSL_RAW_DATA, $this->iv)) . |
| 84 | self::DELIMITER . |
| 85 | self::CURRENT_VERSION; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $data |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function _decrypt(string $data):string { |
| 94 | |
| 95 | $version = substr($data, -3); |
| 96 | |
| 97 | if($version == self::DELIMITER . self::CURRENT_VERSION) { |
| 98 | list($iv, $data) = explode(self::DELIMITER, $data); |
| 99 | $iv = $this->fromBase64($iv); |
| 100 | return trim(openssl_decrypt($this->fromBase64($data), self::CIPHER_METHOD, $this->key, OPENSSL_RAW_DATA, $iv)); |
| 101 | } else { |
| 102 | $iv = '1234567891011121'; |
| 103 | return trim(openssl_decrypt($this->fromBase64($data), self::CIPHER_METHOD_V1, $this->key, OPENSSL_RAW_DATA, $iv)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public static function encrypt(string $data, ?string $key=null, bool $useBase64=true):string { |
| 108 | return (new Crypt($key, $useBase64))->_encrypt($data); |
| 109 | } |
| 110 | |
| 111 | public static function decrypt(string $data, ?string $key=null, bool $useBase64=true):string { |
| 112 | return (new Crypt($key, $useBase64))->_decrypt($data); |
| 113 | } |
| 114 | } |