1 | <?php |
||
61 | class GibberishAES |
||
62 | { |
||
63 | // The default key size in bits. |
||
64 | protected static $key_size = 256; |
||
65 | |||
66 | // The allowed key sizes in bits. |
||
67 | protected static $valid_key_sizes = array(128, 192, 256); |
||
68 | |||
69 | protected static $random_bytes_exists = null; |
||
70 | protected static $openssl_encrypt_exists = null; |
||
71 | protected static $openssl_decrypt_exists = null; |
||
72 | protected static $mcrypt_exists = null; |
||
73 | protected static $mbstring_func_overload = null; |
||
74 | |||
75 | final private function __construct() |
||
78 | |||
79 | final private function __clone() |
||
82 | |||
83 | /** |
||
84 | * Crypt AES (256, 192, 128) |
||
85 | * |
||
86 | * @param string $string The input message to be encrypted. |
||
87 | * @param string $pass The secret pass-phrase, choose a long string |
||
88 | * (64 characters for example) for keeping high entropy. |
||
89 | * The pass-phrase is converted internaly into |
||
90 | * a binary key that is to be used for encryption. |
||
91 | * @return mixed base64 encrypted string, FALSE on failure. |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | 4 | public static function enc(string $string, string $pass): string |
|
122 | |||
123 | /** |
||
124 | * Decrypt AES (256, 192, 128) |
||
125 | * |
||
126 | * @param string $string The input message to be decrypted. |
||
127 | * @param string $pass The secret pass-phrase that has been used for encryption. |
||
128 | * @return mixed base64 decrypted string, FALSE on failure. |
||
129 | */ |
||
130 | 4 | public static function dec(string $string, string $pass): string |
|
163 | |||
164 | /** |
||
165 | * Sets the key-size for encryption/decryption in number of bits |
||
166 | * |
||
167 | * @param int|null $newSize The new key size. The valid integer values are: 128, 192, 256 (default) |
||
168 | * $newsize may be NULL or may be omited - in this case |
||
169 | * this method is just a getter of the current key size value. |
||
170 | * |
||
171 | * @return int Returns the old key size value. |
||
172 | * |
||
173 | * @throws \Exception |
||
174 | */ |
||
175 | 3 | public static function size(?int $newSize = null): int |
|
194 | |||
195 | /** |
||
196 | * returns the multibyte string length |
||
197 | * |
||
198 | * @param string $str |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | 4 | protected static function strlen(string $str): int |
|
206 | |||
207 | /** |
||
208 | * returns a substring of given string |
||
209 | * |
||
210 | * @param string $str |
||
211 | * @param int $start |
||
212 | * @param int|null $length |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 4 | protected static function substr(string $str, int $start, ?int $length = null): string |
|
222 | |||
223 | /** |
||
224 | * return random bytes |
||
225 | * |
||
226 | * @param int $length |
||
227 | * |
||
228 | * @return string |
||
229 | * @throws \Exception |
||
230 | */ |
||
231 | 4 | protected static function randomBytes(int $length): string |
|
235 | |||
236 | /** |
||
237 | * encrypt given string with given key |
||
238 | * |
||
239 | * @param string $string |
||
240 | * @param string $key |
||
241 | * @param string $iv |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 4 | protected static function aesCbcEncrypt(string $string, string $key, string $iv): string |
|
251 | |||
252 | /** |
||
253 | * decrypts given string with given key |
||
254 | * |
||
255 | * @param string $crypted |
||
256 | * @param string $key |
||
257 | * @param string $iv |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 4 | protected static function aesCbcDecrypt(string $crypted, string $key, string $iv): string |
|
267 | } |
||
268 |