|
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
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
if (in_array(static::ALGORITHM_NAME, openssl_get_cipher_methods(), true)) { |
|
43
|
|
|
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
|
|
|
} |
|
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
|
|
|
public function encryptData($plainData) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->useNative) { |
|
69
|
|
|
return parent::encryptData($plainData); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->validatePlainDataForEncryption($plainData); |
|
72
|
|
|
|
|
73
|
|
|
$plainData = ($plainData === '') ? ' ' : $plainData; |
|
74
|
|
|
|
|
75
|
|
|
$cipherData = \CryptoManana\Compatibility\NativeRc4::encryptData($this->key, $plainData); |
|
76
|
|
|
|
|
77
|
|
|
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
|
|
|
public function decryptData($cipherData) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->useNative) { |
|
92
|
|
|
return parent::decryptData($cipherData); |
|
93
|
|
|
} else { |
|
94
|
|
|
$this->validateCipherDataForDecryption($cipherData); |
|
95
|
|
|
|
|
96
|
|
|
$cipherData = $this->changeOutputFormat($cipherData, false); |
|
97
|
|
|
|
|
98
|
|
|
return \CryptoManana\Compatibility\NativeRc4::decryptData($this->key, $cipherData); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|