3 namespace Org\Mxchange\CoreFramework\Stream\Crypto\Mcrypt;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream;
10 use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream;
13 * A mcrypt-based encryption stream
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
20 * @todo mcrypt will become deprecated, rewrite to OpenSSL
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class McryptStream extends BaseCryptoStream implements EncryptableStream {
37 * Protected constructor
41 private function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this node class
49 * @param $rngInstance An RNG instance
50 * @return $streamInstance An instance of this node class
52 public static final function createMcryptStream (RandomNumberGenerator $rngInstance) {
54 $streamInstance = new McryptStream();
56 // Set the RNG instance
57 $streamInstance->setRngInstance($rngInstance);
59 // Return the instance
60 return $streamInstance;
64 * Encrypt the string with fixed salt
66 * @param $str The unencrypted string
67 * @param $key Optional key, if none provided, a random key will be generated
68 * @return $encrypted Encrypted string
70 public function encryptStream (string $str, string $key = NULL) {
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MCRYPT-STREAM: key[' . gettype($key) . ']=' . $key);
75 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
76 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
78 // Generate key, if none provided
81 $key = $this->getRngInstance()->generateKey();
84 // Add some "payload" to the string
85 switch ($this->getRngInstance()->randomNumber(0, 8)) {
87 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
91 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
95 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
99 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
103 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
107 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
111 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
115 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
119 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
123 // Encrypt the string
124 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
131 * Decrypt the string with fixed salt
133 * @param $encrypted Encrypted string
134 * @param $key Optional key, if none provided, a random key will be generated
135 * @return $str The unencrypted string
137 public function decryptStream (string $encrypted, string $key = NULL) {
138 // Init crypto module
139 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
140 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
142 // Shall we use a default key or custom?
144 // Generate (default) key
145 $key = $this->getRngInstance()->generateKey();
148 // Decrypt the string
149 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
151 // Get the real string out
152 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
154 // Does the element count match?
155 assert(count($strArray) == 3);
158 $str = base64_decode($strArray[1]);
160 // Trim trailing nulls away
161 $str = rtrim($str, "\0");
168 * Streams the data and maybe does something to it
170 * @param $data The data (string mostly) to "stream"
171 * @return $data The data (string mostly) to "stream"
172 * @throws UnsupportedOperationException If this method is called (which is a mistake)
174 public function streamData (string $data) {
175 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
176 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);