3 * A mcrypt-based encryption stream
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 * Seperator on many places
28 private $seperator = '|';
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 final static 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 * @return $encrypted Encrypted string
63 public function encryptStream ($str) {
65 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
66 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
69 $key = $this->getRngInstance()->generateKey();
71 // Add some "garbage" to the string
72 switch ($this->getRngInstance()->randomNumber(0, 8)) {
74 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
78 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
82 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
86 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
90 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
94 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
98 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
102 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
106 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
110 // Encrypt the string
111 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
118 * Decrypt the string with fixed salt
120 * @param $encrypted Encrypted string
121 * @return $str The unencrypted string
123 public function decryptStream ($encrypted) {
124 // Init crypto module
125 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
126 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
129 $key = $this->getRngInstance()->generateKey();
131 // Decrypt the string
132 $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
134 // Get the real string out
135 $strArray = explode($this->seperator, $garbageString);
137 // Does the element count match?
138 assert(count($strArray) == 3);
141 $str = base64_decode($strArray[1]);
143 // Trim trailing nulls away
144 $str = rtrim($str, "\0");