3 namespace CoreFramework\Stream\Crypto;
5 // Import framework stuff
6 use CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
9 * A mcrypt-based encryption stream
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
16 * @todo mcrypt will become deprecated, rewrite to OpenSSL
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class McryptStream extends BaseStream implements EncryptableStream {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this node class
45 * @param $rngInstance An RNG instance
46 * @return $streamInstance An instance of this node class
48 public static final function createMcryptStream (RandomNumberGenerator $rngInstance) {
50 $streamInstance = new McryptStream();
52 // Set the RNG instance
53 $streamInstance->setRngInstance($rngInstance);
55 // Return the instance
56 return $streamInstance;
60 * Encrypt the string with fixed salt
62 * @param $str The unencrypted string
63 * @param $key Optional key, if none provided, a random key will be generated
64 * @return $encrypted Encrypted string
66 public function encryptStream ($str, $key = NULL) {
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: key[' . gettype($key) . ']=' . $key);
71 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
72 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
74 // Generate key, if none provided
77 $key = $this->getRngInstance()->generateKey();
80 // Add some "payload" to the string
81 switch ($this->getRngInstance()->randomNumber(0, 8)) {
83 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
87 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
91 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
95 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
99 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
103 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
107 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
111 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
115 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
119 // Encrypt the string
120 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
127 * Decrypt the string with fixed salt
129 * @param $encrypted Encrypted string
130 * @param $key Optional key, if none provided, a random key will be generated
131 * @return $str The unencrypted string
133 public function decryptStream ($encrypted, $key = NULL) {
134 // Init crypto module
135 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
136 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
138 // Shall we use a default key or custom?
140 // Generate (default) key
141 $key = $this->getRngInstance()->generateKey();
144 // Decrypt the string
145 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
147 // Get the real string out
148 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
150 // Does the element count match?
151 assert(count($strArray) == 3);
154 $str = base64_decode($strArray[1]);
156 // Trim trailing nulls away
157 $str = rtrim($str, "\0");
164 * Streams the data and maybe does something to it
166 * @param $data The data (string mostly) to "stream"
167 * @return $data The data (string mostly) to "stream"
168 * @throws UnsupportedOperationException If this method is called (which is a mistake)
170 public function streamData ($data) {
171 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
172 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);