1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File AesEncrypter.php |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Tebru\AesEncryption; |
7
|
|
|
|
8
|
|
|
use Tebru; |
9
|
|
|
use Tebru\AesEncryption\Enum\AesEnum; |
10
|
|
|
use Tebru\AesEncryption\Exception\IvSizeMismatchException; |
11
|
|
|
use Tebru\AesEncryption\Exception\MacHashMismatchException; |
12
|
|
|
use Tebru\AesEncryption\Strategy\AesEncryptionStrategy; |
13
|
|
|
use Tebru\AesEncryption\Strategy\McryptStrategy; |
14
|
|
|
use Tebru\AesEncryption\Strategy\OpenSslStrategy; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AesEncrypter |
18
|
|
|
* |
19
|
|
|
* @author Nate Brunette <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class AesEncrypter |
22
|
|
|
{ |
23
|
|
|
const STRATEGY_OPENSSL = 'openssl'; |
24
|
|
|
const STRATEGY_MCRYPT = 'mcrypt'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AesEncryptionStrategy |
28
|
|
|
*/ |
29
|
|
|
private $strategy; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param string $key The secret key |
35
|
|
|
* @param string $method |
36
|
|
|
* @param AesEncryptionStrategy $strategy |
37
|
|
|
*/ |
38
|
|
|
public function __construct($key, $method = AesEnum::METHOD_256, $strategy = null) |
39
|
|
|
{ |
40
|
|
|
if (null === $strategy) { |
41
|
|
|
// use openssl if it exists |
42
|
|
|
$strategy = (function_exists('openssl_encrypt')) |
43
|
|
|
? new OpenSslStrategy($key, $method) |
44
|
|
|
: new McryptStrategy($key, $method); |
45
|
|
|
} else { |
46
|
|
|
$strategy = (self::STRATEGY_OPENSSL === $strategy) |
47
|
|
|
? new OpenSslStrategy($key, $method) |
48
|
|
|
: new McryptStrategy($key, $method); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->strategy = $strategy; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Encrypts any data using mac-then-encrypt method |
56
|
|
|
* |
57
|
|
|
* @param mixed $data |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function encrypt($data) |
61
|
|
|
{ |
62
|
|
|
$serializedData = serialize($data); |
63
|
|
|
$iv = $this->strategy->createIv(); |
64
|
|
|
$encrypted = $this->strategy->encryptData($serializedData, $iv); |
65
|
|
|
$mac = $this->strategy->getMac($encrypted); |
66
|
|
|
$encoded = $this->strategy->encodeData($encrypted, $mac, $iv); |
67
|
|
|
|
68
|
|
|
return $encoded; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Decrypts data encrypted through encrypt() method |
73
|
|
|
* |
74
|
|
|
* @param string $data |
75
|
|
|
* @return mixed |
76
|
|
|
* @throws IvSizeMismatchException If the IV length has been altered |
77
|
|
|
* @throws MacHashMismatchException If the data has been altered |
78
|
|
|
*/ |
79
|
|
|
public function decrypt($data) |
80
|
|
|
{ |
81
|
|
|
// if this is not an encrypted string |
82
|
|
|
if (false === strpos($data, '|')) { |
83
|
|
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
list($encryptedData, $mac, $iv) = $this->strategy->decodeData($data); |
87
|
|
|
|
88
|
|
|
Tebru\assert($mac === $this->strategy->getMac($encryptedData), new MacHashMismatchException('MAC hashes do not match')); |
89
|
|
|
Tebru\assert(strlen($iv) === $this->strategy->getIvSize(), new IvSizeMismatchException('IV size does not match expectation')); |
90
|
|
|
|
91
|
|
|
$serializedData = $this->strategy->decryptData($encryptedData, $iv); |
92
|
|
|
|
93
|
|
|
$decrypted = unserialize($serializedData); |
94
|
|
|
|
95
|
|
|
return $decrypted; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|