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