3 * A mcrypt-based encryption stream
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class McryptStream extends BaseStream implements EncryptableStream {
26 * Separator on many places
28 const DATA_PAYLOAD_SEPARATOR = '|';
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this node class
43 * @param $rngInstance An RNG instance
44 * @return $streamInstance An instance of this node class
46 public static final function createMcryptStream (RandomNumberGenerator $rngInstance) {
48 $streamInstance = new McryptStream();
50 // Set the RNG instance
51 $streamInstance->setRngInstance($rngInstance);
53 // Return the instance
54 return $streamInstance;
58 * Encrypt the string with fixed salt
60 * @param $str The unencrypted string
61 * @param $key Optional key, if none provided, a random key will be generated
62 * @return $encrypted Encrypted string
64 public function encryptStream ($str, $key = NULL) {
66 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: key[' . gettype($key) . ']=' . $key);
69 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
70 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
72 // Generate key, if none provided
75 $key = $this->getRngInstance()->generateKey();
78 // Add some "payload" to the string
79 switch ($this->getRngInstance()->randomNumber(0, 8)) {
81 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
85 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
89 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
93 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
97 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
101 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
105 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
109 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
113 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
117 // Encrypt the string
118 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
125 * Decrypt the string with fixed salt
127 * @param $encrypted Encrypted string
128 * @param $key Optional key, if none provided, a random key will be generated
129 * @return $str The unencrypted string
131 public function decryptStream ($encrypted, $key = NULL) {
132 // Init crypto module
133 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
134 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
136 // Shall we use a default key or custom?
138 // Generate (default) key
139 $key = $this->getRngInstance()->generateKey();
142 // Decrypt the string
143 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
145 // Get the real string out
146 $strArray = explode(self::DATA_PAYLOAD_SEPARATOR, $payloadString);
148 // Does the element count match?
149 assert(count($strArray) == 3);
152 $str = base64_decode($strArray[1]);
154 // Trim trailing nulls away
155 $str = rtrim($str, "\0");
162 * Streams the data and maybe does something to it
164 * @param $data The data (string mostly) to "stream"
165 * @return $data The data (string mostly) to "stream"
166 * @throws UnsupportedOperationException If this method is called (which is a mistake)
168 public function streamData ($data) {
169 self::createDebugInstance(__CLASS__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
170 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);