|
@@ -1,37 +1,40 @@
|
1
|
|
-<?
|
2
|
|
-class Crypto {
|
3
|
|
- /**
|
4
|
|
- * Encrypts input text for use in database
|
5
|
|
- *
|
6
|
|
- * @param string $plaintext
|
7
|
|
- * @return encrypted string or false if DB key not accessible
|
8
|
|
- */
|
9
|
|
- public static function encrypt($plaintext) {
|
10
|
|
- if (apcu_exists('DBKEY')) {
|
11
|
|
- $iv_size = openssl_cipher_iv_length('AES-128-CBC');
|
12
|
|
- $iv = openssl_random_pseudo_bytes($iv_size);
|
13
|
|
- $ret = base64_encode($iv.openssl_encrypt($plaintext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv));
|
14
|
|
- return $ret;
|
15
|
|
- } else {
|
16
|
|
- return false;
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+class Crypto
|
|
4
|
+{
|
|
5
|
+ /**
|
|
6
|
+ * Encrypts input text for use in database
|
|
7
|
+ *
|
|
8
|
+ * @param string $plaintext
|
|
9
|
+ * @return encrypted string or false if DB key not accessible
|
|
10
|
+ */
|
|
11
|
+ public static function encrypt($plaintext)
|
|
12
|
+ {
|
|
13
|
+ if (apcu_exists('DBKEY')) {
|
|
14
|
+ $iv_size = openssl_cipher_iv_length('AES-128-CBC');
|
|
15
|
+ $iv = openssl_random_pseudo_bytes($iv_size);
|
|
16
|
+ $ret = base64_encode($iv.openssl_encrypt($plaintext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv));
|
|
17
|
+ return $ret;
|
|
18
|
+ } else {
|
|
19
|
+ return false;
|
|
20
|
+ }
|
17
|
21
|
}
|
18
|
|
- }
|
19
|
22
|
|
20
|
|
- /**
|
21
|
|
- * Decrypts input text from database
|
22
|
|
- *
|
23
|
|
- * @param string $ciphertext
|
24
|
|
- * @return decrypted string string or false if DB key not accessible
|
25
|
|
- */
|
26
|
|
- public static function decrypt($ciphertext) {
|
27
|
|
- if (apcu_exists('DBKEY')) {
|
28
|
|
- $iv_size = openssl_cipher_iv_length('AES-128-CBC');
|
29
|
|
- $iv = substr(base64_decode($ciphertext), 0, $iv_size);
|
30
|
|
- $ciphertext = substr(base64_decode($ciphertext), $iv_size);
|
31
|
|
- return openssl_decrypt($ciphertext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv);
|
32
|
|
- } else {
|
33
|
|
- return false;
|
|
23
|
+ /**
|
|
24
|
+ * Decrypts input text from database
|
|
25
|
+ *
|
|
26
|
+ * @param string $ciphertext
|
|
27
|
+ * @return decrypted string string or false if DB key not accessible
|
|
28
|
+ */
|
|
29
|
+ public static function decrypt($ciphertext)
|
|
30
|
+ {
|
|
31
|
+ if (apcu_exists('DBKEY')) {
|
|
32
|
+ $iv_size = openssl_cipher_iv_length('AES-128-CBC');
|
|
33
|
+ $iv = substr(base64_decode($ciphertext), 0, $iv_size);
|
|
34
|
+ $ciphertext = substr(base64_decode($ciphertext), $iv_size);
|
|
35
|
+ return openssl_decrypt($ciphertext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv);
|
|
36
|
+ } else {
|
|
37
|
+ return false;
|
|
38
|
+ }
|
34
|
39
|
}
|
35
|
|
- }
|
36
|
40
|
}
|
37
|
|
-?>
|