3 * A mcrypt-based encryption stream
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
67 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
69 // Generate key, if none provided
72 $key = $this->getRngInstance()->generateKey();
75 // Add some "payload" to the string
76 switch ($this->getRngInstance()->randomNumber(0, 8)) {
78 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
82 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
86 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
90 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
94 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
98 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
102 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
106 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
110 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
114 // Encrypt the string
115 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
122 * Decrypt the string with fixed salt
124 * @param $encrypted Encrypted string
125 * @param $key Optional key, if none provided, a random key will be generated
126 * @return $str The unencrypted string
128 public function decryptStream ($encrypted, $key = NULL) {
129 // Init crypto module
130 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
131 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
133 // Shall we use a default key or custom?
135 // Generate (default) key
136 $key = $this->getRngInstance()->generateKey();
139 // Decrypt the string
140 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
142 // Get the real string out
143 $strArray = explode(self::DATA_PAYLOAD_SEPARATOR, $payloadString);
145 // Does the element count match?
146 assert(count($strArray) == 3);
149 $str = base64_decode($strArray[1]);
151 // Trim trailing nulls away
152 $str = rtrim($str, "\0");
159 * Streams the data and maybe does something to it
161 * @param $data The data (string mostly) to "stream"
162 * @return $data The data (string mostly) to "stream"
163 * @throws UnsupportedOperationException If this method is called (which is a mistake)
165 public function streamData ($data) {
166 self::createDebugInstance(__CLASS__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
167 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);