1 <?php
2 3 4 5 6 7
8
9
10 defined('FOF_INCLUDED') or die;
11
12 class FOFEncryptAesOpenssl extends FOFEncryptAesAbstract implements FOFEncryptAesInterface
13 {
14 15 16 17 18
19 protected $openSSLOptions = 0;
20
21 22 23 24 25
26 protected $method = 'aes-128-cbc';
27
28 public function __construct()
29 {
30 $this->openSSLOptions = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
31 }
32
33 public function setEncryptionMode($mode = 'cbc', $strength = 128)
34 {
35 static $availableAlgorithms = null;
36 static $defaultAlgo = 'aes-128-cbc';
37
38 if (!is_array($availableAlgorithms))
39 {
40 $availableAlgorithms = openssl_get_cipher_methods();
41
42 foreach (array('aes-256-cbc', 'aes-256-ecb', 'aes-192-cbc',
43 'aes-192-ecb', 'aes-128-cbc', 'aes-128-ecb') as $algo)
44 {
45 if (in_array($algo, $availableAlgorithms))
46 {
47 $defaultAlgo = $algo;
48 break;
49 }
50 }
51 }
52
53 $strength = (int) $strength;
54 $mode = strtolower($mode);
55
56 if (!in_array($strength, array(128, 192, 256)))
57 {
58 $strength = 256;
59 }
60
61 if (!in_array($mode, array('cbc', 'ebc')))
62 {
63 $mode = 'cbc';
64 }
65
66 $algo = 'aes-' . $strength . '-' . $mode;
67
68 if (!in_array($algo, $availableAlgorithms))
69 {
70 $algo = $defaultAlgo;
71 }
72
73 $this->method = $algo;
74 }
75
76 public function encrypt($plainText, $key, $iv = null)
77 {
78 $iv_size = $this->getBlockSize();
79 $key = $this->resizeKey($key, $iv_size);
80 $iv = $this->resizeKey($iv, $iv_size);
81
82 if (empty($iv))
83 {
84 $randVal = new FOFEncryptRandval();
85 $iv = $randVal->generate($iv_size);
86 }
87
88 $plainText .= $this->getZeroPadding($plainText, $iv_size);
89 $cipherText = openssl_encrypt($plainText, $this->method, $key, $this->openSSLOptions, $iv);
90 $cipherText = $iv . $cipherText;
91
92 return $cipherText;
93 }
94
95 public function decrypt($cipherText, $key)
96 {
97 $iv_size = $this->getBlockSize();
98 $key = $this->resizeKey($key, $iv_size);
99 $iv = substr($cipherText, 0, $iv_size);
100 $cipherText = substr($cipherText, $iv_size);
101 $plainText = openssl_decrypt($cipherText, $this->method, $key, $this->openSSLOptions, $iv);
102
103 return $plainText;
104 }
105
106 public function isSupported(FOFUtilsPhpfunc $phpfunc = null)
107 {
108 if (!is_object($phpfunc) || !($phpfunc instanceof $phpfunc))
109 {
110 $phpfunc = new FOFUtilsPhpfunc();
111 }
112
113 if (!$phpfunc->function_exists('openssl_get_cipher_methods'))
114 {
115 return false;
116 }
117
118 if (!$phpfunc->function_exists('openssl_random_pseudo_bytes'))
119 {
120 return false;
121 }
122
123 if (!$phpfunc->function_exists('openssl_cipher_iv_length'))
124 {
125 return false;
126 }
127
128 if (!$phpfunc->function_exists('openssl_encrypt'))
129 {
130 return false;
131 }
132
133 if (!$phpfunc->function_exists('openssl_decrypt'))
134 {
135 return false;
136 }
137
138 if (!$phpfunc->function_exists('hash'))
139 {
140 return false;
141 }
142
143 if (!$phpfunc->function_exists('hash_algos'))
144 {
145 return false;
146 }
147
148 $algorightms = $phpfunc->openssl_get_cipher_methods();
149
150 if (!in_array('aes-128-cbc', $algorightms))
151 {
152 return false;
153 }
154
155 $algorightms = $phpfunc->hash_algos();
156
157 if (!in_array('sha256', $algorightms))
158 {
159 return false;
160 }
161
162 return true;
163 }
164
165 166 167
168 public function getBlockSize()
169 {
170 return openssl_cipher_iv_length($this->method);
171 }
172 }