1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The RC4-128 encryption algorithm class. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\SymmetricEncryption; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractStreamCipherAlgorithm as SymmetricStreamCipherAlgorithm; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Rc4 - The RC4-128 encryption algorithm object. |
13
|
|
|
* |
14
|
|
|
* @package CryptoManana\SymmetricEncryption |
15
|
|
|
*/ |
16
|
|
|
class Rc4 extends SymmetricStreamCipherAlgorithm |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The internal name of the algorithm. |
20
|
|
|
*/ |
21
|
|
|
const ALGORITHM_NAME = 'RC4'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The internal secret key size measured in raw bytes length for the algorithm |
25
|
|
|
* |
26
|
|
|
* @internal For the current algorithm: 128 bits (16 bytes) |
27
|
|
|
*/ |
28
|
|
|
const KEY_SIZE = 16; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Flag to force native code polyfill realizations, if available. |
32
|
|
|
* |
33
|
|
|
* @var bool Flag to force native realizations. |
34
|
|
|
*/ |
35
|
|
|
protected $useNative = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Stream cipher algorithm constructor. |
39
|
|
|
*/ |
40
|
50 |
|
public function __construct() |
41
|
|
|
{ |
42
|
50 |
|
if (in_array(static::ALGORITHM_NAME, openssl_get_cipher_methods(), true)) { |
43
|
50 |
|
parent::__construct(); |
44
|
|
|
} else { |
45
|
|
|
// @codeCoverageIgnoreStart |
46
|
|
|
$this->useNative = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@internal Serialization and initialization purposes for the default key. }} |
50
|
|
|
*/ |
51
|
|
|
if (strlen($this->key) < static::KEY_SIZE) { |
52
|
|
|
$this->key = str_pad($this->key, static::KEY_SIZE, "\x0", STR_PAD_RIGHT); |
53
|
|
|
} |
54
|
|
|
// @codeCoverageIgnoreEnd |
55
|
|
|
} |
56
|
50 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Encrypts the given plain data. |
60
|
|
|
* |
61
|
|
|
* @param string $plainData The plain input string. |
62
|
|
|
* |
63
|
|
|
* @return string The cipher/encrypted data. |
64
|
|
|
* @throws \Exception Validation errors. |
65
|
|
|
*/ |
66
|
22 |
|
public function encryptData($plainData) |
67
|
|
|
{ |
68
|
22 |
|
if ($this->useNative) { |
69
|
4 |
|
return parent::encryptData($plainData); |
70
|
|
|
} else { |
71
|
22 |
|
$this->validatePlainDataForEncryption($plainData); |
72
|
|
|
|
73
|
20 |
|
$plainData = ($plainData === '') ? ' ' : $plainData; |
74
|
|
|
|
75
|
20 |
|
$cipherData = \CryptoManana\Compatibility\NativeRc4::encryptData($this->key, $plainData); |
76
|
|
|
|
77
|
20 |
|
return $this->changeOutputFormat($cipherData, true); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Decrypts the given cipher data. |
83
|
|
|
* |
84
|
|
|
* @param string $cipherData The encrypted input string. |
85
|
|
|
* |
86
|
|
|
* @return string The decrypted/plain data. |
87
|
|
|
* @throws \Exception Validation errors. |
88
|
|
|
*/ |
89
|
18 |
|
public function decryptData($cipherData) |
90
|
|
|
{ |
91
|
18 |
|
if ($this->useNative) { |
92
|
4 |
|
return parent::decryptData($cipherData); |
93
|
|
|
} else { |
94
|
18 |
|
$this->validateCipherDataForDecryption($cipherData); |
95
|
|
|
|
96
|
14 |
|
$cipherData = $this->changeOutputFormat($cipherData, false); |
97
|
|
|
|
98
|
14 |
|
return \CryptoManana\Compatibility\NativeRc4::decryptData($this->key, $cipherData); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|