@@ -38,61 +38,61 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_Rijndael_128 extends Cipher_Rijndael |
40 | 40 | { |
41 | - /** @type integer BITS_BLOCK The size of the block, in bits */ |
|
42 | - const BYTES_BLOCK = 16; |
|
41 | + /** @type integer BITS_BLOCK The size of the block, in bits */ |
|
42 | + const BYTES_BLOCK = 16; |
|
43 | 43 | |
44 | - //const BITS_KEY = 0; |
|
44 | + //const BITS_KEY = 0; |
|
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Constructor |
|
49 | - * Sets the key used for encryption. Also sets the requied block size |
|
50 | - * This should only be used when calling this class directly, for classes |
|
51 | - * that extend this class, they should call __construct1() |
|
52 | - * |
|
53 | - * @param string $key string containing the user supplied encryption key |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function __construct($key) |
|
57 | - { |
|
58 | - // Set up the key |
|
59 | - parent::__construct(PHP_Crypt::CIPHER_RIJNDAEL_128, $key); |
|
47 | + /** |
|
48 | + * Constructor |
|
49 | + * Sets the key used for encryption. Also sets the requied block size |
|
50 | + * This should only be used when calling this class directly, for classes |
|
51 | + * that extend this class, they should call __construct1() |
|
52 | + * |
|
53 | + * @param string $key string containing the user supplied encryption key |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function __construct($key) |
|
57 | + { |
|
58 | + // Set up the key |
|
59 | + parent::__construct(PHP_Crypt::CIPHER_RIJNDAEL_128, $key); |
|
60 | 60 | |
61 | - // required block size in bits |
|
62 | - $this->blockSize(self::BYTES_BLOCK); |
|
61 | + // required block size in bits |
|
62 | + $this->blockSize(self::BYTES_BLOCK); |
|
63 | 63 | |
64 | - // expand the key |
|
65 | - $this->expandKey(); |
|
66 | - } |
|
64 | + // expand the key |
|
65 | + $this->expandKey(); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Constructor, used only by classes that extend this class |
|
71 | - * |
|
72 | - * @param string $cipher_name The pre-defined cipher name of the child class |
|
73 | - * @param string $key The key used for encryption/decryption |
|
74 | - * @param integer $req_key_len The required key length, in bits |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - protected function __construct1($cipher_name, $key, $req_key_len) |
|
78 | - { |
|
79 | - parent::__construct($cipher_name, $key, $req_key_len); |
|
69 | + /** |
|
70 | + * Constructor, used only by classes that extend this class |
|
71 | + * |
|
72 | + * @param string $cipher_name The pre-defined cipher name of the child class |
|
73 | + * @param string $key The key used for encryption/decryption |
|
74 | + * @param integer $req_key_len The required key length, in bits |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + protected function __construct1($cipher_name, $key, $req_key_len) |
|
78 | + { |
|
79 | + parent::__construct($cipher_name, $key, $req_key_len); |
|
80 | 80 | |
81 | - // required block size in bits |
|
82 | - $this->blockSize(self::BYTES_BLOCK); |
|
81 | + // required block size in bits |
|
82 | + $this->blockSize(self::BYTES_BLOCK); |
|
83 | 83 | |
84 | - // expand the key |
|
85 | - $this->expandKey(); |
|
86 | - } |
|
84 | + // expand the key |
|
85 | + $this->expandKey(); |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | - /** |
|
90 | - * Destructor |
|
91 | - * |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function __destruct() |
|
95 | - { |
|
96 | - parent::__destruct(); |
|
97 | - } |
|
89 | + /** |
|
90 | + * Destructor |
|
91 | + * |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function __destruct() |
|
95 | + { |
|
96 | + parent::__destruct(); |
|
97 | + } |
|
98 | 98 | } |
@@ -44,298 +44,298 @@ |
||
44 | 44 | */ |
45 | 45 | class Cipher_RC2 extends Cipher |
46 | 46 | { |
47 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
48 | - const BYTES_BLOCK = 8; // 64 bits |
|
49 | - |
|
50 | - // rc2 has a variable length key, between 1 - 128 bytes (8-1024 bits) |
|
51 | - //const BYTES_KEY = 0; |
|
52 | - |
|
53 | - /** @type array $_sbox RC2's sBox, initialized in initTables() */ |
|
54 | - private static $_sbox = array(); |
|
55 | - |
|
56 | - /** @type string $xkey The expanded key */ |
|
57 | - private $xkey = ""; |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Constructor |
|
62 | - * |
|
63 | - * @param string $key The key used for Encryption/Decryption |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function __construct($key) |
|
67 | - { |
|
68 | - // the key must be between 1 and 128 bytes, keys larger than |
|
69 | - // 128 bytes are truncated in expandedKey() |
|
70 | - $keylen = strlen($key); |
|
71 | - if($keylen < 1) |
|
72 | - { |
|
73 | - $err = "Key size is $keylen bits, Key size must be between 1 and 128 bytes"; |
|
74 | - trigger_error($err, E_USER_WARNING); |
|
75 | - } |
|
76 | - |
|
77 | - // set the key |
|
78 | - parent::__construct(PHP_Crypt::CIPHER_RC2, $key, $keylen); |
|
79 | - |
|
80 | - // set the block size |
|
81 | - $this->blockSize(self::BYTES_BLOCK); |
|
82 | - |
|
83 | - // initialize the tables |
|
84 | - $this->initTables(); |
|
85 | - |
|
86 | - // expand the key to 128 bytes |
|
87 | - $this->expandKey(); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Destructor |
|
93 | - * |
|
94 | - * @return void |
|
95 | - */ |
|
96 | - public function __destruct() |
|
97 | - { |
|
98 | - parent::__destruct(); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Encrypt plain text data using RC2 |
|
104 | - * |
|
105 | - * @param string $text A 64 bit (8 byte) plain text string |
|
106 | - * @return boolean Returns true |
|
107 | - */ |
|
108 | - public function encrypt(&$text) |
|
109 | - { |
|
110 | - $this->operation(parent::ENCRYPT); |
|
111 | - |
|
112 | - // split up the message and key into 4 16 bit parts (2 byte words), |
|
113 | - // then convert each array element to an integer |
|
114 | - $w = self::splitBytes($text); |
|
115 | - $k = self::splitBytes($this->xkey); |
|
116 | - $j = 0; // the key index |
|
117 | - |
|
118 | - for($i = 0; $i < 16; ++$i) |
|
119 | - { |
|
120 | - $j = $i * 4; |
|
121 | - |
|
122 | - /* This is where it gets ugly, RC2 relies on unsigned ints. PHP does |
|
47 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
48 | + const BYTES_BLOCK = 8; // 64 bits |
|
49 | + |
|
50 | + // rc2 has a variable length key, between 1 - 128 bytes (8-1024 bits) |
|
51 | + //const BYTES_KEY = 0; |
|
52 | + |
|
53 | + /** @type array $_sbox RC2's sBox, initialized in initTables() */ |
|
54 | + private static $_sbox = array(); |
|
55 | + |
|
56 | + /** @type string $xkey The expanded key */ |
|
57 | + private $xkey = ""; |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Constructor |
|
62 | + * |
|
63 | + * @param string $key The key used for Encryption/Decryption |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function __construct($key) |
|
67 | + { |
|
68 | + // the key must be between 1 and 128 bytes, keys larger than |
|
69 | + // 128 bytes are truncated in expandedKey() |
|
70 | + $keylen = strlen($key); |
|
71 | + if($keylen < 1) |
|
72 | + { |
|
73 | + $err = "Key size is $keylen bits, Key size must be between 1 and 128 bytes"; |
|
74 | + trigger_error($err, E_USER_WARNING); |
|
75 | + } |
|
76 | + |
|
77 | + // set the key |
|
78 | + parent::__construct(PHP_Crypt::CIPHER_RC2, $key, $keylen); |
|
79 | + |
|
80 | + // set the block size |
|
81 | + $this->blockSize(self::BYTES_BLOCK); |
|
82 | + |
|
83 | + // initialize the tables |
|
84 | + $this->initTables(); |
|
85 | + |
|
86 | + // expand the key to 128 bytes |
|
87 | + $this->expandKey(); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Destructor |
|
93 | + * |
|
94 | + * @return void |
|
95 | + */ |
|
96 | + public function __destruct() |
|
97 | + { |
|
98 | + parent::__destruct(); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Encrypt plain text data using RC2 |
|
104 | + * |
|
105 | + * @param string $text A 64 bit (8 byte) plain text string |
|
106 | + * @return boolean Returns true |
|
107 | + */ |
|
108 | + public function encrypt(&$text) |
|
109 | + { |
|
110 | + $this->operation(parent::ENCRYPT); |
|
111 | + |
|
112 | + // split up the message and key into 4 16 bit parts (2 byte words), |
|
113 | + // then convert each array element to an integer |
|
114 | + $w = self::splitBytes($text); |
|
115 | + $k = self::splitBytes($this->xkey); |
|
116 | + $j = 0; // the key index |
|
117 | + |
|
118 | + for($i = 0; $i < 16; ++$i) |
|
119 | + { |
|
120 | + $j = $i * 4; |
|
121 | + |
|
122 | + /* This is where it gets ugly, RC2 relies on unsigned ints. PHP does |
|
123 | 123 | * not have a nice way to handle unsigned ints, so we have to rely on sprintf. |
124 | 124 | * To make RC2 compatible with mCrypt, I also forced everything to 32 bit |
125 | 125 | * When I test against mcrypt and the original rc2 C source, I get 32 bit |
126 | 126 | * results, even on a 64 bit platform |
127 | 127 | */ |
128 | 128 | |
129 | - // 16 rounds of RC2's Mixing algorithm for each 2 byte 'word' as |
|
130 | - // required by RC2 |
|
131 | - $w[0] += parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
|
132 | - $w[0] = sprintf("%u", $w[0] << 1) + sprintf("%u", $w[0] >> 15 & 1); |
|
133 | - $w[0] = parent::uInt32($w[0]); |
|
134 | - |
|
135 | - $w[1] += parent::uInt($w[2] & ~$w[0]) + parent::uInt($w[3] & $w[0]) + $k[$j + 1]; |
|
136 | - $w[1] = parent::uInt($w[1] << 2) + parent::uInt($w[1] >> 14 & 3); |
|
137 | - $w[1] = parent::uInt32($w[1]); |
|
138 | - |
|
139 | - $w[2] += parent::uInt($w[3] & ~$w[1]) + parent::uInt($w[0] & $w[1]) + $k[$j + 2]; |
|
140 | - $w[2] = parent::uInt($w[2] << 3) + parent::uInt($w[2] >> 13 & 7); |
|
141 | - $w[2] = parent::uInt32($w[2]); |
|
142 | - |
|
143 | - $w[3] += parent::uInt($w[0] & ~$w[2]) + parent::uInt($w[1] & $w[2]) + $k[$j + 3]; |
|
144 | - $w[3] = parent::uInt($w[3] << 5) + parent::uInt($w[3] >> 11 & 31); |
|
145 | - $w[3] = parent::uInt32($w[3]); |
|
146 | - |
|
147 | - // rounds 5 and 11 get rc2's Mash |
|
148 | - if($i == 4 || $i == 10) |
|
149 | - { |
|
150 | - $w[0] += $k[$w[3] & 63]; |
|
151 | - $w[1] += $k[$w[0] & 63]; |
|
152 | - $w[2] += $k[$w[1] & 63]; |
|
153 | - $w[3] += $k[$w[2] & 63]; |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /* truthfully, I am not clear why this is required. It was not mentioned |
|
129 | + // 16 rounds of RC2's Mixing algorithm for each 2 byte 'word' as |
|
130 | + // required by RC2 |
|
131 | + $w[0] += parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
|
132 | + $w[0] = sprintf("%u", $w[0] << 1) + sprintf("%u", $w[0] >> 15 & 1); |
|
133 | + $w[0] = parent::uInt32($w[0]); |
|
134 | + |
|
135 | + $w[1] += parent::uInt($w[2] & ~$w[0]) + parent::uInt($w[3] & $w[0]) + $k[$j + 1]; |
|
136 | + $w[1] = parent::uInt($w[1] << 2) + parent::uInt($w[1] >> 14 & 3); |
|
137 | + $w[1] = parent::uInt32($w[1]); |
|
138 | + |
|
139 | + $w[2] += parent::uInt($w[3] & ~$w[1]) + parent::uInt($w[0] & $w[1]) + $k[$j + 2]; |
|
140 | + $w[2] = parent::uInt($w[2] << 3) + parent::uInt($w[2] >> 13 & 7); |
|
141 | + $w[2] = parent::uInt32($w[2]); |
|
142 | + |
|
143 | + $w[3] += parent::uInt($w[0] & ~$w[2]) + parent::uInt($w[1] & $w[2]) + $k[$j + 3]; |
|
144 | + $w[3] = parent::uInt($w[3] << 5) + parent::uInt($w[3] >> 11 & 31); |
|
145 | + $w[3] = parent::uInt32($w[3]); |
|
146 | + |
|
147 | + // rounds 5 and 11 get rc2's Mash |
|
148 | + if($i == 4 || $i == 10) |
|
149 | + { |
|
150 | + $w[0] += $k[$w[3] & 63]; |
|
151 | + $w[1] += $k[$w[0] & 63]; |
|
152 | + $w[2] += $k[$w[1] & 63]; |
|
153 | + $w[3] += $k[$w[2] & 63]; |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /* truthfully, I am not clear why this is required. It was not mentioned |
|
158 | 158 | * in any of the documentation I read, however reading the original RC2 C |
159 | 159 | * source code, this was done and in order for me to get the |
160 | 160 | * correct results in PHP I needed to do this as well |
161 | 161 | */ |
162 | - $max = count($w); |
|
163 | - for($i = 0; $i < $max; ++$i) |
|
164 | - { |
|
165 | - $pos = $i * 2; |
|
166 | - $text[$pos] = chr($w[$i]); |
|
167 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
168 | - } |
|
169 | - |
|
170 | - return true; |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Decrypt a RC2 encrypted string |
|
176 | - * |
|
177 | - * @param string $text A RC2 encrypted string |
|
178 | - * @return boolean Returns true |
|
179 | - */ |
|
180 | - public function decrypt(&$text) |
|
181 | - { |
|
182 | - $this->operation(parent::DECRYPT); |
|
183 | - |
|
184 | - // first split up the message into four 16 bit parts (2 bytes), |
|
185 | - // then convert each array element to an integer |
|
186 | - $w = self::splitBytes($text); |
|
187 | - $k = self::splitBytes($this->xkey); |
|
188 | - $j = 0; // the key index |
|
189 | - |
|
190 | - for($i = 15; $i >= 0; --$i) |
|
191 | - { |
|
192 | - $j = $i * 4; |
|
193 | - |
|
194 | - /* This is where it gets ugly, RC2 relies on unsigned ints. PHP does |
|
162 | + $max = count($w); |
|
163 | + for($i = 0; $i < $max; ++$i) |
|
164 | + { |
|
165 | + $pos = $i * 2; |
|
166 | + $text[$pos] = chr($w[$i]); |
|
167 | + $text[$pos+1] = chr($w[$i] >> 8); |
|
168 | + } |
|
169 | + |
|
170 | + return true; |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Decrypt a RC2 encrypted string |
|
176 | + * |
|
177 | + * @param string $text A RC2 encrypted string |
|
178 | + * @return boolean Returns true |
|
179 | + */ |
|
180 | + public function decrypt(&$text) |
|
181 | + { |
|
182 | + $this->operation(parent::DECRYPT); |
|
183 | + |
|
184 | + // first split up the message into four 16 bit parts (2 bytes), |
|
185 | + // then convert each array element to an integer |
|
186 | + $w = self::splitBytes($text); |
|
187 | + $k = self::splitBytes($this->xkey); |
|
188 | + $j = 0; // the key index |
|
189 | + |
|
190 | + for($i = 15; $i >= 0; --$i) |
|
191 | + { |
|
192 | + $j = $i * 4; |
|
193 | + |
|
194 | + /* This is where it gets ugly, RC2 relies on unsigned ints. PHP does |
|
195 | 195 | * not have a nice way to handle unsigned ints, so we have to rely on sprintf. |
196 | 196 | * To make RC2 compatible with mCrypt, I also forced everything to 32 bit |
197 | 197 | * When I test against mcrypt and the original rc2 C source, I get 32 bit |
198 | 198 | * results, even on a 64 bit platform |
199 | 199 | */ |
200 | 200 | |
201 | - $w[3] &= 65535; |
|
202 | - $w[3] = parent::uInt($w[3] << 11) + parent::uInt($w[3] >> 5); |
|
203 | - $w[3] -= parent::uInt($w[0] & ~$w[2]) + parent::uInt($w[1] & $w[2]) + $k[$j + 3]; |
|
204 | - $w[3] = parent::uInt32($w[3]); |
|
205 | - |
|
206 | - $w[2] &= 65535; |
|
207 | - $w[2] = parent::uInt($w[2] << 13) + parent::uInt($w[2] >> 3); |
|
208 | - $w[2] -= parent::uInt($w[3] & ~$w[1]) + parent::uInt($w[0] & $w[1]) + $k[$j + 2]; |
|
209 | - $w[2] = parent::uInt32($w[2]); |
|
210 | - |
|
211 | - $w[1] &= 65535; |
|
212 | - $w[1] = parent::uInt($w[1] << 14) + parent::uInt($w[1] >> 2); |
|
213 | - $w[1] -= parent::uInt($w[2] & ~$w[0]) + parent::uInt($w[3] & $w[0]) + $k[$j + 1]; |
|
214 | - $w[1] = parent::uInt32($w[1]); |
|
215 | - |
|
216 | - $w[0] &= 65535; |
|
217 | - $w[0] = parent::uInt($w[0] << 15) + parent::uInt($w[0] >> 1); |
|
218 | - $w[0] -= parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
|
219 | - $w[0] = parent::uInt32($w[0]); |
|
220 | - |
|
221 | - if($i == 5 || $i == 11) |
|
222 | - { |
|
223 | - $w[3] -= $k[$w[2] & 63]; |
|
224 | - $w[2] -= $k[$w[1] & 63]; |
|
225 | - $w[1] -= $k[$w[0] & 63]; |
|
226 | - $w[0] -= $k[$w[3] & 63]; |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /* I am not clear why this is required. It was not mentioned |
|
201 | + $w[3] &= 65535; |
|
202 | + $w[3] = parent::uInt($w[3] << 11) + parent::uInt($w[3] >> 5); |
|
203 | + $w[3] -= parent::uInt($w[0] & ~$w[2]) + parent::uInt($w[1] & $w[2]) + $k[$j + 3]; |
|
204 | + $w[3] = parent::uInt32($w[3]); |
|
205 | + |
|
206 | + $w[2] &= 65535; |
|
207 | + $w[2] = parent::uInt($w[2] << 13) + parent::uInt($w[2] >> 3); |
|
208 | + $w[2] -= parent::uInt($w[3] & ~$w[1]) + parent::uInt($w[0] & $w[1]) + $k[$j + 2]; |
|
209 | + $w[2] = parent::uInt32($w[2]); |
|
210 | + |
|
211 | + $w[1] &= 65535; |
|
212 | + $w[1] = parent::uInt($w[1] << 14) + parent::uInt($w[1] >> 2); |
|
213 | + $w[1] -= parent::uInt($w[2] & ~$w[0]) + parent::uInt($w[3] & $w[0]) + $k[$j + 1]; |
|
214 | + $w[1] = parent::uInt32($w[1]); |
|
215 | + |
|
216 | + $w[0] &= 65535; |
|
217 | + $w[0] = parent::uInt($w[0] << 15) + parent::uInt($w[0] >> 1); |
|
218 | + $w[0] -= parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
|
219 | + $w[0] = parent::uInt32($w[0]); |
|
220 | + |
|
221 | + if($i == 5 || $i == 11) |
|
222 | + { |
|
223 | + $w[3] -= $k[$w[2] & 63]; |
|
224 | + $w[2] -= $k[$w[1] & 63]; |
|
225 | + $w[1] -= $k[$w[0] & 63]; |
|
226 | + $w[0] -= $k[$w[3] & 63]; |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /* I am not clear why this is required. It was not mentioned |
|
232 | 232 | * in any of the documentation I read, however reading the original RC2 C |
233 | 233 | * source code, this was done and in order for me to get the |
234 | 234 | * correct results in PHP I needed to do this as well |
235 | 235 | */ |
236 | - $max = count($w); |
|
237 | - for($i = 0; $i < $max; ++$i) |
|
238 | - { |
|
239 | - $pos = $i * 2; |
|
240 | - $text[$pos] = chr($w[$i]); |
|
241 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
242 | - } |
|
243 | - |
|
244 | - return true; |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * Splits an 8 byte string into four array elements of 2 bytes each, swaps the bytes |
|
250 | - * of each array element, and converts the result into an integer |
|
251 | - * |
|
252 | - * @param string $str The 8 byte string to split and convert |
|
253 | - * @return array An array of 4 elements, each with 2 bytes |
|
254 | - */ |
|
255 | - private static function splitBytes($str) |
|
256 | - { |
|
257 | - $arr = str_split($str, 2); |
|
258 | - |
|
259 | - return array_map(function($b){ |
|
260 | - return Core::str2Dec($b[1].$b[0]); |
|
261 | - }, $arr); |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * Expands the key to 128 bits. RC2 uses an initial key size between |
|
267 | - * 8 - 128 bits, then takes the initial key and expands it out to 128 |
|
268 | - * bits, which is used for encryption |
|
269 | - * |
|
270 | - * @return void |
|
271 | - */ |
|
272 | - private function expandKey() |
|
273 | - { |
|
274 | - // start by copying the key to the xkey variable |
|
275 | - $this->xkey = $this->key(); |
|
276 | - $len = $this->keySize(); |
|
277 | - |
|
278 | - // the max length of the key is 128 bytes |
|
279 | - if($len > 128) |
|
280 | - $this->xkey = substr($this->xkey, 0, 128); |
|
281 | - |
|
282 | - // now expanded the rest of the key to 128 bytes, using the sbox |
|
283 | - for($i = $len; $i < 128; ++$i) |
|
284 | - { |
|
285 | - $byte1 = ord($this->xkey[$i - $len]); |
|
286 | - $byte2 = ord($this->xkey[$i - 1]); |
|
287 | - $pos = ($byte1 + $byte2); |
|
288 | - |
|
289 | - // the sbox is only 255 bytes, so if we extend past that |
|
290 | - // we need to modulo 256 so we have a valid position |
|
291 | - if($pos > 255) |
|
292 | - $pos -= 256; |
|
293 | - |
|
294 | - $this->xkey .= chr(self::$_sbox[$pos]); |
|
295 | - } |
|
296 | - |
|
297 | - // now replace the first byte of the key with it's position in the sbox |
|
298 | - $pos = ord($this->xkey[0]); |
|
299 | - $this->xkey[0] = chr(self::$_sbox[$pos]); |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Initialize tables |
|
305 | - * |
|
306 | - * @return void |
|
307 | - */ |
|
308 | - private function initTables() |
|
309 | - { |
|
310 | - self::$_sbox = array( |
|
311 | - 0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d, |
|
312 | - 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2, |
|
313 | - 0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32, |
|
314 | - 0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82, |
|
315 | - 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc, |
|
316 | - 0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26, |
|
317 | - 0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03, |
|
318 | - 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7, |
|
319 | - 0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a, |
|
320 | - 0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec, |
|
321 | - 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39, |
|
322 | - 0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31, |
|
323 | - 0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9, |
|
324 | - 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9, |
|
325 | - 0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e, |
|
326 | - 0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad |
|
327 | - ); |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Indicates this is a block cipher |
|
333 | - * |
|
334 | - * @return integer Returns Cipher::BLOCK |
|
335 | - */ |
|
336 | - public function type() |
|
337 | - { |
|
338 | - return parent::BLOCK; |
|
339 | - } |
|
236 | + $max = count($w); |
|
237 | + for($i = 0; $i < $max; ++$i) |
|
238 | + { |
|
239 | + $pos = $i * 2; |
|
240 | + $text[$pos] = chr($w[$i]); |
|
241 | + $text[$pos+1] = chr($w[$i] >> 8); |
|
242 | + } |
|
243 | + |
|
244 | + return true; |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * Splits an 8 byte string into four array elements of 2 bytes each, swaps the bytes |
|
250 | + * of each array element, and converts the result into an integer |
|
251 | + * |
|
252 | + * @param string $str The 8 byte string to split and convert |
|
253 | + * @return array An array of 4 elements, each with 2 bytes |
|
254 | + */ |
|
255 | + private static function splitBytes($str) |
|
256 | + { |
|
257 | + $arr = str_split($str, 2); |
|
258 | + |
|
259 | + return array_map(function($b){ |
|
260 | + return Core::str2Dec($b[1].$b[0]); |
|
261 | + }, $arr); |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * Expands the key to 128 bits. RC2 uses an initial key size between |
|
267 | + * 8 - 128 bits, then takes the initial key and expands it out to 128 |
|
268 | + * bits, which is used for encryption |
|
269 | + * |
|
270 | + * @return void |
|
271 | + */ |
|
272 | + private function expandKey() |
|
273 | + { |
|
274 | + // start by copying the key to the xkey variable |
|
275 | + $this->xkey = $this->key(); |
|
276 | + $len = $this->keySize(); |
|
277 | + |
|
278 | + // the max length of the key is 128 bytes |
|
279 | + if($len > 128) |
|
280 | + $this->xkey = substr($this->xkey, 0, 128); |
|
281 | + |
|
282 | + // now expanded the rest of the key to 128 bytes, using the sbox |
|
283 | + for($i = $len; $i < 128; ++$i) |
|
284 | + { |
|
285 | + $byte1 = ord($this->xkey[$i - $len]); |
|
286 | + $byte2 = ord($this->xkey[$i - 1]); |
|
287 | + $pos = ($byte1 + $byte2); |
|
288 | + |
|
289 | + // the sbox is only 255 bytes, so if we extend past that |
|
290 | + // we need to modulo 256 so we have a valid position |
|
291 | + if($pos > 255) |
|
292 | + $pos -= 256; |
|
293 | + |
|
294 | + $this->xkey .= chr(self::$_sbox[$pos]); |
|
295 | + } |
|
296 | + |
|
297 | + // now replace the first byte of the key with it's position in the sbox |
|
298 | + $pos = ord($this->xkey[0]); |
|
299 | + $this->xkey[0] = chr(self::$_sbox[$pos]); |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Initialize tables |
|
305 | + * |
|
306 | + * @return void |
|
307 | + */ |
|
308 | + private function initTables() |
|
309 | + { |
|
310 | + self::$_sbox = array( |
|
311 | + 0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d, |
|
312 | + 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2, |
|
313 | + 0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32, |
|
314 | + 0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82, |
|
315 | + 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc, |
|
316 | + 0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26, |
|
317 | + 0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03, |
|
318 | + 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7, |
|
319 | + 0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a, |
|
320 | + 0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec, |
|
321 | + 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39, |
|
322 | + 0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31, |
|
323 | + 0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9, |
|
324 | + 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9, |
|
325 | + 0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e, |
|
326 | + 0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad |
|
327 | + ); |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Indicates this is a block cipher |
|
333 | + * |
|
334 | + * @return integer Returns Cipher::BLOCK |
|
335 | + */ |
|
336 | + public function type() |
|
337 | + { |
|
338 | + return parent::BLOCK; |
|
339 | + } |
|
340 | 340 | } |
341 | 341 | ?> |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | // the key must be between 1 and 128 bytes, keys larger than |
69 | 69 | // 128 bytes are truncated in expandedKey() |
70 | 70 | $keylen = strlen($key); |
71 | - if($keylen < 1) |
|
71 | + if ($keylen < 1) |
|
72 | 72 | { |
73 | 73 | $err = "Key size is $keylen bits, Key size must be between 1 and 128 bytes"; |
74 | 74 | trigger_error($err, E_USER_WARNING); |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $k = self::splitBytes($this->xkey); |
116 | 116 | $j = 0; // the key index |
117 | 117 | |
118 | - for($i = 0; $i < 16; ++$i) |
|
118 | + for ($i = 0; $i < 16; ++$i) |
|
119 | 119 | { |
120 | 120 | $j = $i * 4; |
121 | 121 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | $w[3] = parent::uInt32($w[3]); |
146 | 146 | |
147 | 147 | // rounds 5 and 11 get rc2's Mash |
148 | - if($i == 4 || $i == 10) |
|
148 | + if ($i == 4 || $i == 10) |
|
149 | 149 | { |
150 | 150 | $w[0] += $k[$w[3] & 63]; |
151 | 151 | $w[1] += $k[$w[0] & 63]; |
@@ -160,11 +160,11 @@ discard block |
||
160 | 160 | * correct results in PHP I needed to do this as well |
161 | 161 | */ |
162 | 162 | $max = count($w); |
163 | - for($i = 0; $i < $max; ++$i) |
|
163 | + for ($i = 0; $i < $max; ++$i) |
|
164 | 164 | { |
165 | 165 | $pos = $i * 2; |
166 | 166 | $text[$pos] = chr($w[$i]); |
167 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
167 | + $text[$pos + 1] = chr($w[$i] >> 8); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | return true; |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | $k = self::splitBytes($this->xkey); |
188 | 188 | $j = 0; // the key index |
189 | 189 | |
190 | - for($i = 15; $i >= 0; --$i) |
|
190 | + for ($i = 15; $i >= 0; --$i) |
|
191 | 191 | { |
192 | 192 | $j = $i * 4; |
193 | 193 | |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | $w[0] -= parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
219 | 219 | $w[0] = parent::uInt32($w[0]); |
220 | 220 | |
221 | - if($i == 5 || $i == 11) |
|
221 | + if ($i == 5 || $i == 11) |
|
222 | 222 | { |
223 | 223 | $w[3] -= $k[$w[2] & 63]; |
224 | 224 | $w[2] -= $k[$w[1] & 63]; |
@@ -234,11 +234,11 @@ discard block |
||
234 | 234 | * correct results in PHP I needed to do this as well |
235 | 235 | */ |
236 | 236 | $max = count($w); |
237 | - for($i = 0; $i < $max; ++$i) |
|
237 | + for ($i = 0; $i < $max; ++$i) |
|
238 | 238 | { |
239 | 239 | $pos = $i * 2; |
240 | 240 | $text[$pos] = chr($w[$i]); |
241 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
241 | + $text[$pos + 1] = chr($w[$i] >> 8); |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | return true; |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | { |
257 | 257 | $arr = str_split($str, 2); |
258 | 258 | |
259 | - return array_map(function($b){ |
|
259 | + return array_map(function($b) { |
|
260 | 260 | return Core::str2Dec($b[1].$b[0]); |
261 | 261 | }, $arr); |
262 | 262 | } |
@@ -276,11 +276,11 @@ discard block |
||
276 | 276 | $len = $this->keySize(); |
277 | 277 | |
278 | 278 | // the max length of the key is 128 bytes |
279 | - if($len > 128) |
|
279 | + if ($len > 128) |
|
280 | 280 | $this->xkey = substr($this->xkey, 0, 128); |
281 | 281 | |
282 | 282 | // now expanded the rest of the key to 128 bytes, using the sbox |
283 | - for($i = $len; $i < 128; ++$i) |
|
283 | + for ($i = $len; $i < 128; ++$i) |
|
284 | 284 | { |
285 | 285 | $byte1 = ord($this->xkey[$i - $len]); |
286 | 286 | $byte2 = ord($this->xkey[$i - 1]); |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | |
289 | 289 | // the sbox is only 255 bytes, so if we extend past that |
290 | 290 | // we need to modulo 256 so we have a valid position |
291 | - if($pos > 255) |
|
291 | + if ($pos > 255) |
|
292 | 292 | $pos -= 256; |
293 | 293 | |
294 | 294 | $this->xkey .= chr(self::$_sbox[$pos]); |
@@ -276,8 +276,9 @@ discard block |
||
276 | 276 | $len = $this->keySize(); |
277 | 277 | |
278 | 278 | // the max length of the key is 128 bytes |
279 | - if($len > 128) |
|
280 | - $this->xkey = substr($this->xkey, 0, 128); |
|
279 | + if($len > 128) { |
|
280 | + $this->xkey = substr($this->xkey, 0, 128); |
|
281 | + } |
|
281 | 282 | |
282 | 283 | // now expanded the rest of the key to 128 bytes, using the sbox |
283 | 284 | for($i = $len; $i < 128; ++$i) |
@@ -288,8 +289,9 @@ discard block |
||
288 | 289 | |
289 | 290 | // the sbox is only 255 bytes, so if we extend past that |
290 | 291 | // we need to modulo 256 so we have a valid position |
291 | - if($pos > 255) |
|
292 | - $pos -= 256; |
|
292 | + if($pos > 255) { |
|
293 | + $pos -= 256; |
|
294 | + } |
|
293 | 295 | |
294 | 296 | $this->xkey .= chr(self::$_sbox[$pos]); |
295 | 297 | } |
@@ -38,39 +38,39 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_Rijndael_192 extends Cipher_Rijndael |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 24; // 192 bits |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 24; // 192 bits |
|
43 | 43 | |
44 | - //const BITS_KEY = 0; |
|
44 | + //const BITS_KEY = 0; |
|
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Constructor |
|
49 | - * Sets the key used for encryption. Also sets the requied block size |
|
50 | - * |
|
51 | - * @param string $key string containing the user supplied encryption key |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function __construct($key) |
|
55 | - { |
|
56 | - // Set up the key |
|
57 | - parent::__construct(PHP_CRYPT::CIPHER_RIJNDAEL_192, $key); |
|
47 | + /** |
|
48 | + * Constructor |
|
49 | + * Sets the key used for encryption. Also sets the requied block size |
|
50 | + * |
|
51 | + * @param string $key string containing the user supplied encryption key |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function __construct($key) |
|
55 | + { |
|
56 | + // Set up the key |
|
57 | + parent::__construct(PHP_CRYPT::CIPHER_RIJNDAEL_192, $key); |
|
58 | 58 | |
59 | - // required block size in bits |
|
60 | - $this->blockSize(self::BYTES_BLOCK); |
|
59 | + // required block size in bits |
|
60 | + $this->blockSize(self::BYTES_BLOCK); |
|
61 | 61 | |
62 | - // expand the key now that we know the key size, and the bit size |
|
63 | - $this->expandKey(); |
|
64 | - } |
|
62 | + // expand the key now that we know the key size, and the bit size |
|
63 | + $this->expandKey(); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Destructor |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function __destruct() |
|
73 | - { |
|
74 | - parent::__destruct(); |
|
75 | - } |
|
67 | + /** |
|
68 | + * Destructor |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function __destruct() |
|
73 | + { |
|
74 | + parent::__destruct(); |
|
75 | + } |
|
76 | 76 | } |
@@ -36,222 +36,222 @@ |
||
36 | 36 | */ |
37 | 37 | abstract class Cipher extends Core |
38 | 38 | { |
39 | - /** @type integer ENCRYPT Indicates when we are in encryption mode */ |
|
40 | - const ENCRYPT = 1; |
|
39 | + /** @type integer ENCRYPT Indicates when we are in encryption mode */ |
|
40 | + const ENCRYPT = 1; |
|
41 | 41 | |
42 | - /** @type integer DECRYPT Indicates when we are in decryption mode */ |
|
43 | - const DECRYPT = 2; |
|
42 | + /** @type integer DECRYPT Indicates when we are in decryption mode */ |
|
43 | + const DECRYPT = 2; |
|
44 | 44 | |
45 | - /** @type integer BLOCK Indicates that a cipher is a block cipher */ |
|
46 | - const BLOCK = 1; |
|
45 | + /** @type integer BLOCK Indicates that a cipher is a block cipher */ |
|
46 | + const BLOCK = 1; |
|
47 | 47 | |
48 | - /** @type integer STREAM Indicates that a cipher is a stream cipher */ |
|
49 | - const STREAM = 2; |
|
48 | + /** @type integer STREAM Indicates that a cipher is a stream cipher */ |
|
49 | + const STREAM = 2; |
|
50 | 50 | |
51 | - /** @type string $cipher_name Stores the name of the cipher */ |
|
52 | - protected $cipher_name = ""; |
|
51 | + /** @type string $cipher_name Stores the name of the cipher */ |
|
52 | + protected $cipher_name = ""; |
|
53 | 53 | |
54 | - /** @type integer $block_size The block size of the cipher in bytes */ |
|
55 | - protected $block_size = 0; |
|
54 | + /** @type integer $block_size The block size of the cipher in bytes */ |
|
55 | + protected $block_size = 0; |
|
56 | 56 | |
57 | - /** |
|
58 | - * @type integer $operation Indicates if a cipher is Encrypting or Decrypting |
|
59 | - * this can be set to either Cipher::ENCRYPT or Cipher::DECRYPT |
|
60 | - */ |
|
61 | - protected $operation = self::ENCRYPT; // can be either Cipher::ENCRYPT | Cipher::DECRYPT; |
|
57 | + /** |
|
58 | + * @type integer $operation Indicates if a cipher is Encrypting or Decrypting |
|
59 | + * this can be set to either Cipher::ENCRYPT or Cipher::DECRYPT |
|
60 | + */ |
|
61 | + protected $operation = self::ENCRYPT; // can be either Cipher::ENCRYPT | Cipher::DECRYPT; |
|
62 | 62 | |
63 | - /** @type string $key Stores the key for the Cipher */ |
|
64 | - private $key = ""; |
|
63 | + /** @type string $key Stores the key for the Cipher */ |
|
64 | + private $key = ""; |
|
65 | 65 | |
66 | - /** @type integer $key_len Keep track of the key length, so we don't |
|
66 | + /** @type integer $key_len Keep track of the key length, so we don't |
|
67 | 67 | have to make repeated calls to strlen() to find the length */ |
68 | - private $key_len = 0; |
|
68 | + private $key_len = 0; |
|
69 | 69 | |
70 | 70 | |
71 | - /** |
|
72 | - * Constructor |
|
73 | - * |
|
74 | - * @param string $name one of the predefined ciphers |
|
75 | - * @param string $key The key used for encryption |
|
76 | - * @param int Optional, the required size of a key for the cipher |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - protected function __construct($name, $key = "", $required_key_sz = 0) |
|
80 | - { |
|
81 | - $this->name($name); |
|
82 | - $this->key($key, $required_key_sz); |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Destructor |
|
88 | - * |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - protected function __destruct() |
|
92 | - { |
|
71 | + /** |
|
72 | + * Constructor |
|
73 | + * |
|
74 | + * @param string $name one of the predefined ciphers |
|
75 | + * @param string $key The key used for encryption |
|
76 | + * @param int Optional, the required size of a key for the cipher |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + protected function __construct($name, $key = "", $required_key_sz = 0) |
|
80 | + { |
|
81 | + $this->name($name); |
|
82 | + $this->key($key, $required_key_sz); |
|
83 | + } |
|
84 | + |
|
93 | 85 | |
94 | - } |
|
86 | + /** |
|
87 | + * Destructor |
|
88 | + * |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + protected function __destruct() |
|
92 | + { |
|
95 | 93 | |
94 | + } |
|
96 | 95 | |
97 | 96 | |
98 | - /********************************************************************** |
|
97 | + |
|
98 | + /********************************************************************** |
|
99 | 99 | * ABSTRACT METHODS |
100 | 100 | * |
101 | 101 | * The abstract methods required by inheriting classes to implement |
102 | 102 | **********************************************************************/ |
103 | 103 | |
104 | - /** |
|
105 | - * The cipher's encryption function. Must be defined |
|
106 | - * by the class inheriting this Cipher object. This function |
|
107 | - * will most often be called from within the Mode object |
|
108 | - * |
|
109 | - * @param string $text The text to be encrypted |
|
110 | - * @return boolean Always returns false |
|
111 | - */ |
|
112 | - abstract public function encrypt(&$text); |
|
104 | + /** |
|
105 | + * The cipher's encryption function. Must be defined |
|
106 | + * by the class inheriting this Cipher object. This function |
|
107 | + * will most often be called from within the Mode object |
|
108 | + * |
|
109 | + * @param string $text The text to be encrypted |
|
110 | + * @return boolean Always returns false |
|
111 | + */ |
|
112 | + abstract public function encrypt(&$text); |
|
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * The cipher's decryption function. Must be defined |
|
117 | - * by the class inheriting this Cipher object. This function |
|
118 | - * will most often be called from within the Mode object |
|
119 | - * |
|
120 | - * @param string $text The text to decrypt |
|
121 | - * @return boolean Always returns false |
|
122 | - */ |
|
123 | - abstract public function decrypt(&$text); |
|
115 | + /** |
|
116 | + * The cipher's decryption function. Must be defined |
|
117 | + * by the class inheriting this Cipher object. This function |
|
118 | + * will most often be called from within the Mode object |
|
119 | + * |
|
120 | + * @param string $text The text to decrypt |
|
121 | + * @return boolean Always returns false |
|
122 | + */ |
|
123 | + abstract public function decrypt(&$text); |
|
124 | 124 | |
125 | 125 | |
126 | - /** |
|
127 | - * Indiciates whether the cipher is a block or stream cipher |
|
128 | - * |
|
129 | - * @return integer Returns either Cipher::BLOCK or Cipher::STREAM |
|
130 | - */ |
|
131 | - abstract public function type(); |
|
126 | + /** |
|
127 | + * Indiciates whether the cipher is a block or stream cipher |
|
128 | + * |
|
129 | + * @return integer Returns either Cipher::BLOCK or Cipher::STREAM |
|
130 | + */ |
|
131 | + abstract public function type(); |
|
132 | 132 | |
133 | 133 | |
134 | 134 | |
135 | 135 | |
136 | - /********************************************************************** |
|
136 | + /********************************************************************** |
|
137 | 137 | * PUBLIC METHODS |
138 | 138 | * |
139 | 139 | **********************************************************************/ |
140 | 140 | |
141 | - /** |
|
142 | - * Determine if we are Encrypting or Decrypting |
|
143 | - * Since some ciphers use the same algorithm to Encrypt or Decrypt but with only |
|
144 | - * slight differences, we need a way to check if we are Encrypting or Decrypting |
|
145 | - * An example is DES, which uses the same algorithm except that when Decrypting |
|
146 | - * the sub_keys are reversed |
|
147 | - * |
|
148 | - * @param integer $op Sets the operation to Cipher::ENCRYPT or Cipher::DECRYPT |
|
149 | - * @return integer The current operation, either Cipher::ENCRYPT or Cipher::DECRYPT |
|
150 | - */ |
|
151 | - public function operation($op = 0) |
|
152 | - { |
|
153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | - $this->operation = $op; |
|
155 | - |
|
156 | - return $this->operation; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Return the name of cipher that is currently being used |
|
162 | - * |
|
163 | - * @return string The cipher name |
|
164 | - */ |
|
165 | - public function name($name = "") |
|
166 | - { |
|
167 | - if($name != "") |
|
168 | - $this->cipher_name = $name; |
|
169 | - |
|
170 | - return $this->cipher_name; |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Size of the data in Bits that get used during encryption |
|
176 | - * |
|
177 | - * @param integer $bytes Number of bytes each block of data is required by the cipher |
|
178 | - * @return integer The number of bytes each block of data required by the cipher |
|
179 | - */ |
|
180 | - public function blockSize($bytes = 0) |
|
181 | - { |
|
182 | - if($bytes > 0) |
|
183 | - $this->block_size = $bytes; |
|
184 | - |
|
185 | - // in some cases a blockSize is not set, such as stream ciphers. |
|
186 | - // so just return 0 for the block size |
|
187 | - if(!isset($this->block_size)) |
|
188 | - return 0; |
|
189 | - |
|
190 | - return $this->block_size; |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * Returns the size (in bytes) required by the cipher. |
|
196 | - * |
|
197 | - * @return integer The number of bytes the cipher requires the key to be |
|
198 | - */ |
|
199 | - public function keySize() |
|
200 | - { |
|
201 | - return $this->key_len; |
|
202 | - } |
|
203 | - |
|
204 | - |
|
205 | - /** |
|
206 | - * Set the cipher key used for encryption/decryption. This function |
|
207 | - * may lengthen or shorten the key to meet the size requirements of |
|
208 | - * the cipher. |
|
209 | - * |
|
210 | - * If the $key parameter is not given, this function simply returns the |
|
211 | - * current key being used. |
|
212 | - * |
|
213 | - * @param string $key Optional, A key for the cipher |
|
214 | - * @param integer $req_sz The byte size required for the key |
|
215 | - * @return string They key, which may have been modified to fit size |
|
216 | - * requirements |
|
217 | - */ |
|
218 | - public function key($key = "", $req_sz = 0) |
|
219 | - { |
|
220 | - if($key != "" && $key != null) |
|
221 | - { |
|
222 | - // in the case where the key is changed changed after |
|
223 | - // creating a new Cipher object and the $req_sz was not |
|
224 | - // given, we need to make sure the new key meets the size |
|
225 | - // requirements. This can be determined from the $this->key_len |
|
226 | - // member set from the previous key |
|
227 | - if($this->key_len > 0 && $req_sz == 0) |
|
228 | - $req_sz = $this->key_len; |
|
229 | - else |
|
230 | - $this->key_len = strlen($key); |
|
231 | - |
|
232 | - if($req_sz > 0) |
|
233 | - { |
|
234 | - if($this->key_len > $req_sz) |
|
235 | - { |
|
236 | - // shorten the key length |
|
237 | - $key = substr($key, 0, $req_sz); |
|
238 | - $this->key_len = $req_sz; |
|
239 | - } |
|
240 | - else if($this->key_len < $req_sz) |
|
241 | - { |
|
242 | - // send a notice that the key was too small |
|
243 | - // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
|
244 | - $msg = strtoupper($this->name())." requires a $req_sz byte key, {$this->key_len} bytes received"; |
|
245 | - trigger_error($msg, E_USER_WARNING); |
|
141 | + /** |
|
142 | + * Determine if we are Encrypting or Decrypting |
|
143 | + * Since some ciphers use the same algorithm to Encrypt or Decrypt but with only |
|
144 | + * slight differences, we need a way to check if we are Encrypting or Decrypting |
|
145 | + * An example is DES, which uses the same algorithm except that when Decrypting |
|
146 | + * the sub_keys are reversed |
|
147 | + * |
|
148 | + * @param integer $op Sets the operation to Cipher::ENCRYPT or Cipher::DECRYPT |
|
149 | + * @return integer The current operation, either Cipher::ENCRYPT or Cipher::DECRYPT |
|
150 | + */ |
|
151 | + public function operation($op = 0) |
|
152 | + { |
|
153 | + if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | + $this->operation = $op; |
|
155 | + |
|
156 | + return $this->operation; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Return the name of cipher that is currently being used |
|
162 | + * |
|
163 | + * @return string The cipher name |
|
164 | + */ |
|
165 | + public function name($name = "") |
|
166 | + { |
|
167 | + if($name != "") |
|
168 | + $this->cipher_name = $name; |
|
169 | + |
|
170 | + return $this->cipher_name; |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Size of the data in Bits that get used during encryption |
|
176 | + * |
|
177 | + * @param integer $bytes Number of bytes each block of data is required by the cipher |
|
178 | + * @return integer The number of bytes each block of data required by the cipher |
|
179 | + */ |
|
180 | + public function blockSize($bytes = 0) |
|
181 | + { |
|
182 | + if($bytes > 0) |
|
183 | + $this->block_size = $bytes; |
|
184 | + |
|
185 | + // in some cases a blockSize is not set, such as stream ciphers. |
|
186 | + // so just return 0 for the block size |
|
187 | + if(!isset($this->block_size)) |
|
188 | + return 0; |
|
189 | + |
|
190 | + return $this->block_size; |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * Returns the size (in bytes) required by the cipher. |
|
196 | + * |
|
197 | + * @return integer The number of bytes the cipher requires the key to be |
|
198 | + */ |
|
199 | + public function keySize() |
|
200 | + { |
|
201 | + return $this->key_len; |
|
202 | + } |
|
203 | + |
|
204 | + |
|
205 | + /** |
|
206 | + * Set the cipher key used for encryption/decryption. This function |
|
207 | + * may lengthen or shorten the key to meet the size requirements of |
|
208 | + * the cipher. |
|
209 | + * |
|
210 | + * If the $key parameter is not given, this function simply returns the |
|
211 | + * current key being used. |
|
212 | + * |
|
213 | + * @param string $key Optional, A key for the cipher |
|
214 | + * @param integer $req_sz The byte size required for the key |
|
215 | + * @return string They key, which may have been modified to fit size |
|
216 | + * requirements |
|
217 | + */ |
|
218 | + public function key($key = "", $req_sz = 0) |
|
219 | + { |
|
220 | + if($key != "" && $key != null) |
|
221 | + { |
|
222 | + // in the case where the key is changed changed after |
|
223 | + // creating a new Cipher object and the $req_sz was not |
|
224 | + // given, we need to make sure the new key meets the size |
|
225 | + // requirements. This can be determined from the $this->key_len |
|
226 | + // member set from the previous key |
|
227 | + if($this->key_len > 0 && $req_sz == 0) |
|
228 | + $req_sz = $this->key_len; |
|
229 | + else |
|
230 | + $this->key_len = strlen($key); |
|
231 | + |
|
232 | + if($req_sz > 0) |
|
233 | + { |
|
234 | + if($this->key_len > $req_sz) |
|
235 | + { |
|
236 | + // shorten the key length |
|
237 | + $key = substr($key, 0, $req_sz); |
|
238 | + $this->key_len = $req_sz; |
|
239 | + } |
|
240 | + else if($this->key_len < $req_sz) |
|
241 | + { |
|
242 | + // send a notice that the key was too small |
|
243 | + // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
|
244 | + $msg = strtoupper($this->name())." requires a $req_sz byte key, {$this->key_len} bytes received"; |
|
245 | + trigger_error($msg, E_USER_WARNING); |
|
246 | 246 | |
247 | - return false; |
|
248 | - } |
|
249 | - } |
|
247 | + return false; |
|
248 | + } |
|
249 | + } |
|
250 | 250 | |
251 | - $this->key = $key; |
|
252 | - } |
|
251 | + $this->key = $key; |
|
252 | + } |
|
253 | 253 | |
254 | - return $this->key; |
|
255 | - } |
|
254 | + return $this->key; |
|
255 | + } |
|
256 | 256 | } |
257 | 257 | ?> |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function operation($op = 0) |
152 | 152 | { |
153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
153 | + if ($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | 154 | $this->operation = $op; |
155 | 155 | |
156 | 156 | return $this->operation; |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | public function name($name = "") |
166 | 166 | { |
167 | - if($name != "") |
|
167 | + if ($name != "") |
|
168 | 168 | $this->cipher_name = $name; |
169 | 169 | |
170 | 170 | return $this->cipher_name; |
@@ -179,12 +179,12 @@ discard block |
||
179 | 179 | */ |
180 | 180 | public function blockSize($bytes = 0) |
181 | 181 | { |
182 | - if($bytes > 0) |
|
182 | + if ($bytes > 0) |
|
183 | 183 | $this->block_size = $bytes; |
184 | 184 | |
185 | 185 | // in some cases a blockSize is not set, such as stream ciphers. |
186 | 186 | // so just return 0 for the block size |
187 | - if(!isset($this->block_size)) |
|
187 | + if (!isset($this->block_size)) |
|
188 | 188 | return 0; |
189 | 189 | |
190 | 190 | return $this->block_size; |
@@ -217,27 +217,27 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function key($key = "", $req_sz = 0) |
219 | 219 | { |
220 | - if($key != "" && $key != null) |
|
220 | + if ($key != "" && $key != null) |
|
221 | 221 | { |
222 | 222 | // in the case where the key is changed changed after |
223 | 223 | // creating a new Cipher object and the $req_sz was not |
224 | 224 | // given, we need to make sure the new key meets the size |
225 | 225 | // requirements. This can be determined from the $this->key_len |
226 | 226 | // member set from the previous key |
227 | - if($this->key_len > 0 && $req_sz == 0) |
|
227 | + if ($this->key_len > 0 && $req_sz == 0) |
|
228 | 228 | $req_sz = $this->key_len; |
229 | 229 | else |
230 | 230 | $this->key_len = strlen($key); |
231 | 231 | |
232 | - if($req_sz > 0) |
|
232 | + if ($req_sz > 0) |
|
233 | 233 | { |
234 | - if($this->key_len > $req_sz) |
|
234 | + if ($this->key_len > $req_sz) |
|
235 | 235 | { |
236 | 236 | // shorten the key length |
237 | 237 | $key = substr($key, 0, $req_sz); |
238 | 238 | $this->key_len = $req_sz; |
239 | 239 | } |
240 | - else if($this->key_len < $req_sz) |
|
240 | + else if ($this->key_len < $req_sz) |
|
241 | 241 | { |
242 | 242 | // send a notice that the key was too small |
243 | 243 | // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
@@ -150,8 +150,9 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function operation($op = 0) |
152 | 152 | { |
153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | - $this->operation = $op; |
|
153 | + if($op == self::ENCRYPT || $op == self::DECRYPT) { |
|
154 | + $this->operation = $op; |
|
155 | + } |
|
155 | 156 | |
156 | 157 | return $this->operation; |
157 | 158 | } |
@@ -164,8 +165,9 @@ discard block |
||
164 | 165 | */ |
165 | 166 | public function name($name = "") |
166 | 167 | { |
167 | - if($name != "") |
|
168 | - $this->cipher_name = $name; |
|
168 | + if($name != "") { |
|
169 | + $this->cipher_name = $name; |
|
170 | + } |
|
169 | 171 | |
170 | 172 | return $this->cipher_name; |
171 | 173 | } |
@@ -179,13 +181,15 @@ discard block |
||
179 | 181 | */ |
180 | 182 | public function blockSize($bytes = 0) |
181 | 183 | { |
182 | - if($bytes > 0) |
|
183 | - $this->block_size = $bytes; |
|
184 | + if($bytes > 0) { |
|
185 | + $this->block_size = $bytes; |
|
186 | + } |
|
184 | 187 | |
185 | 188 | // in some cases a blockSize is not set, such as stream ciphers. |
186 | 189 | // so just return 0 for the block size |
187 | - if(!isset($this->block_size)) |
|
188 | - return 0; |
|
190 | + if(!isset($this->block_size)) { |
|
191 | + return 0; |
|
192 | + } |
|
189 | 193 | |
190 | 194 | return $this->block_size; |
191 | 195 | } |
@@ -224,10 +228,11 @@ discard block |
||
224 | 228 | // given, we need to make sure the new key meets the size |
225 | 229 | // requirements. This can be determined from the $this->key_len |
226 | 230 | // member set from the previous key |
227 | - if($this->key_len > 0 && $req_sz == 0) |
|
228 | - $req_sz = $this->key_len; |
|
229 | - else |
|
230 | - $this->key_len = strlen($key); |
|
231 | + if($this->key_len > 0 && $req_sz == 0) { |
|
232 | + $req_sz = $this->key_len; |
|
233 | + } else { |
|
234 | + $this->key_len = strlen($key); |
|
235 | + } |
|
231 | 236 | |
232 | 237 | if($req_sz > 0) |
233 | 238 | { |
@@ -236,8 +241,7 @@ discard block |
||
236 | 241 | // shorten the key length |
237 | 242 | $key = substr($key, 0, $req_sz); |
238 | 243 | $this->key_len = $req_sz; |
239 | - } |
|
240 | - else if($this->key_len < $req_sz) |
|
244 | + } else if($this->key_len < $req_sz) |
|
241 | 245 | { |
242 | 246 | // send a notice that the key was too small |
243 | 247 | // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
@@ -36,167 +36,167 @@ |
||
36 | 36 | */ |
37 | 37 | class Mode_CTR extends Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * Sets the cipher object that will be used for encryption |
|
42 | - * |
|
43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - function __construct($cipher) |
|
47 | - { |
|
48 | - parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
|
49 | - |
|
50 | - // this works with only block Ciphers |
|
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Destructor |
|
58 | - * |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __destruct() |
|
62 | - { |
|
63 | - parent::__destruct(); |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Encrypts an encrypted string |
|
69 | - * |
|
70 | - * @param string $text the string to be encrypted |
|
71 | - * @return boolean Returns true |
|
72 | - */ |
|
73 | - public function encrypt(&$text) |
|
74 | - { |
|
75 | - $len = strlen($text); |
|
76 | - $blocksz = $this->cipher->blockSize(); |
|
77 | - |
|
78 | - $max = $len / $blocksz; |
|
79 | - for($i = 0; $i < $max; ++$i) |
|
80 | - { |
|
81 | - // get the current position in $text |
|
82 | - $pos = $i * $blocksz; |
|
83 | - |
|
84 | - // make sure we don't extend past the length of $text |
|
85 | - $byte_len = $blocksz; |
|
86 | - if(($pos + $byte_len) > $len) |
|
87 | - $byte_len -= ($pos + $byte_len) - $len; |
|
88 | - |
|
89 | - // encrypt the register |
|
90 | - $this->enc_register = $this->register; |
|
91 | - $this->cipher->encrypt($this->enc_register); |
|
92 | - |
|
93 | - // grab a block of plain text |
|
94 | - $block = substr($text, $pos, $byte_len); |
|
95 | - |
|
96 | - // xor the block |
|
97 | - for($j = 0; $j < $byte_len; ++$j) |
|
98 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
99 | - |
|
100 | - // replace the plain text block with the encrypted block |
|
101 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
102 | - |
|
103 | - // increment the counter |
|
104 | - $this->counter(); |
|
105 | - } |
|
106 | - |
|
107 | - return true; |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * Decrypt an encrypted string |
|
113 | - * |
|
114 | - * @param string $text The string to be decrypted |
|
115 | - * @return boolean Returns true |
|
116 | - */ |
|
117 | - public function decrypt(&$text) |
|
118 | - { |
|
119 | - $len = strlen($text); |
|
120 | - $blocksz = $this->cipher->blockSize(); |
|
121 | - |
|
122 | - $max = $len / $blocksz; |
|
123 | - for($i = 0; $i < $max; ++$i) |
|
124 | - { |
|
125 | - // get the current position in $text |
|
126 | - $pos = $i * $blocksz; |
|
127 | - |
|
128 | - // make sure we don't extend past the length of $text |
|
129 | - $byte_len = $blocksz; |
|
130 | - if(($pos + $byte_len) > $len) |
|
131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
132 | - |
|
133 | - // encrypt the register |
|
134 | - $this->enc_register = $this->register; |
|
135 | - $this->cipher->encrypt($this->enc_register); |
|
136 | - |
|
137 | - // grab a block of plain text |
|
138 | - $block = substr($text, $pos, $byte_len); |
|
139 | - |
|
140 | - // xor the block with the register (which contains the IV) |
|
141 | - for($j = 0; $j < $byte_len; ++$j) |
|
142 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
143 | - |
|
144 | - // replace the encrypted block with the plain text |
|
145 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
146 | - |
|
147 | - // increment the counter |
|
148 | - $this->counter(); |
|
149 | - } |
|
150 | - |
|
151 | - return true; |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * This mode requires an IV |
|
157 | - * |
|
158 | - * @return boolean True |
|
159 | - */ |
|
160 | - public function requiresIV() |
|
161 | - { |
|
162 | - return true; |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Increments the counter (which is initially the IV) by one byte starting at the |
|
168 | - * last byte. Once that byte has reached 0xff, it is then set to 0x00, and the |
|
169 | - * next byte is then incremented. On the following incrementation, the last byte |
|
170 | - * is incremented again. An example: |
|
171 | - * PASS 1: 2037e9ae63f73dfe |
|
172 | - * PASS 2: 2037e9ae63f73dff |
|
173 | - * PASS 3: 2037e9ae63f73e00 |
|
174 | - * PASS 4: 2037e9ae63f73e01 |
|
175 | - * ... |
|
176 | - * PASS N: 2100000000000001 |
|
177 | - * PASS N+1: 2100000000000002 |
|
178 | - * |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - private function counter() |
|
182 | - { |
|
183 | - $pos = $this->cipher->blockSize() - 1; |
|
184 | - |
|
185 | - // starting at the last byte, loop through each byte until |
|
186 | - // we find one that can be incremented |
|
187 | - for($i = $pos; $i >= 0; --$i) |
|
188 | - { |
|
189 | - // if we reached the last byte, set it to 0x00, then |
|
190 | - // loop one more time to increment the next byte |
|
191 | - if(ord($this->register[$i]) == 0xff) |
|
192 | - $this->register[$i] = chr(0x00); |
|
193 | - else |
|
194 | - { |
|
195 | - // now increment the byte by 1 |
|
196 | - $this->register[$i] = chr(ord($this->register[$i]) + 1); |
|
197 | - break; |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * Sets the cipher object that will be used for encryption |
|
42 | + * |
|
43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + function __construct($cipher) |
|
47 | + { |
|
48 | + parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
|
49 | + |
|
50 | + // this works with only block Ciphers |
|
51 | + if($cipher->type() != Cipher::BLOCK) |
|
52 | + trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Destructor |
|
58 | + * |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __destruct() |
|
62 | + { |
|
63 | + parent::__destruct(); |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Encrypts an encrypted string |
|
69 | + * |
|
70 | + * @param string $text the string to be encrypted |
|
71 | + * @return boolean Returns true |
|
72 | + */ |
|
73 | + public function encrypt(&$text) |
|
74 | + { |
|
75 | + $len = strlen($text); |
|
76 | + $blocksz = $this->cipher->blockSize(); |
|
77 | + |
|
78 | + $max = $len / $blocksz; |
|
79 | + for($i = 0; $i < $max; ++$i) |
|
80 | + { |
|
81 | + // get the current position in $text |
|
82 | + $pos = $i * $blocksz; |
|
83 | + |
|
84 | + // make sure we don't extend past the length of $text |
|
85 | + $byte_len = $blocksz; |
|
86 | + if(($pos + $byte_len) > $len) |
|
87 | + $byte_len -= ($pos + $byte_len) - $len; |
|
88 | + |
|
89 | + // encrypt the register |
|
90 | + $this->enc_register = $this->register; |
|
91 | + $this->cipher->encrypt($this->enc_register); |
|
92 | + |
|
93 | + // grab a block of plain text |
|
94 | + $block = substr($text, $pos, $byte_len); |
|
95 | + |
|
96 | + // xor the block |
|
97 | + for($j = 0; $j < $byte_len; ++$j) |
|
98 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
99 | + |
|
100 | + // replace the plain text block with the encrypted block |
|
101 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
102 | + |
|
103 | + // increment the counter |
|
104 | + $this->counter(); |
|
105 | + } |
|
106 | + |
|
107 | + return true; |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * Decrypt an encrypted string |
|
113 | + * |
|
114 | + * @param string $text The string to be decrypted |
|
115 | + * @return boolean Returns true |
|
116 | + */ |
|
117 | + public function decrypt(&$text) |
|
118 | + { |
|
119 | + $len = strlen($text); |
|
120 | + $blocksz = $this->cipher->blockSize(); |
|
121 | + |
|
122 | + $max = $len / $blocksz; |
|
123 | + for($i = 0; $i < $max; ++$i) |
|
124 | + { |
|
125 | + // get the current position in $text |
|
126 | + $pos = $i * $blocksz; |
|
127 | + |
|
128 | + // make sure we don't extend past the length of $text |
|
129 | + $byte_len = $blocksz; |
|
130 | + if(($pos + $byte_len) > $len) |
|
131 | + $byte_len -= ($pos + $byte_len) - $len; |
|
132 | + |
|
133 | + // encrypt the register |
|
134 | + $this->enc_register = $this->register; |
|
135 | + $this->cipher->encrypt($this->enc_register); |
|
136 | + |
|
137 | + // grab a block of plain text |
|
138 | + $block = substr($text, $pos, $byte_len); |
|
139 | + |
|
140 | + // xor the block with the register (which contains the IV) |
|
141 | + for($j = 0; $j < $byte_len; ++$j) |
|
142 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
143 | + |
|
144 | + // replace the encrypted block with the plain text |
|
145 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
146 | + |
|
147 | + // increment the counter |
|
148 | + $this->counter(); |
|
149 | + } |
|
150 | + |
|
151 | + return true; |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * This mode requires an IV |
|
157 | + * |
|
158 | + * @return boolean True |
|
159 | + */ |
|
160 | + public function requiresIV() |
|
161 | + { |
|
162 | + return true; |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Increments the counter (which is initially the IV) by one byte starting at the |
|
168 | + * last byte. Once that byte has reached 0xff, it is then set to 0x00, and the |
|
169 | + * next byte is then incremented. On the following incrementation, the last byte |
|
170 | + * is incremented again. An example: |
|
171 | + * PASS 1: 2037e9ae63f73dfe |
|
172 | + * PASS 2: 2037e9ae63f73dff |
|
173 | + * PASS 3: 2037e9ae63f73e00 |
|
174 | + * PASS 4: 2037e9ae63f73e01 |
|
175 | + * ... |
|
176 | + * PASS N: 2100000000000001 |
|
177 | + * PASS N+1: 2100000000000002 |
|
178 | + * |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + private function counter() |
|
182 | + { |
|
183 | + $pos = $this->cipher->blockSize() - 1; |
|
184 | + |
|
185 | + // starting at the last byte, loop through each byte until |
|
186 | + // we find one that can be incremented |
|
187 | + for($i = $pos; $i >= 0; --$i) |
|
188 | + { |
|
189 | + // if we reached the last byte, set it to 0x00, then |
|
190 | + // loop one more time to increment the next byte |
|
191 | + if(ord($this->register[$i]) == 0xff) |
|
192 | + $this->register[$i] = chr(0x00); |
|
193 | + else |
|
194 | + { |
|
195 | + // now increment the byte by 1 |
|
196 | + $this->register[$i] = chr(ord($this->register[$i]) + 1); |
|
197 | + break; |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | 201 | } |
202 | 202 | ?> |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
51 | + if ($cipher->type() != Cipher::BLOCK) |
|
52 | 52 | trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
53 | 53 | } |
54 | 54 | |
@@ -76,14 +76,14 @@ discard block |
||
76 | 76 | $blocksz = $this->cipher->blockSize(); |
77 | 77 | |
78 | 78 | $max = $len / $blocksz; |
79 | - for($i = 0; $i < $max; ++$i) |
|
79 | + for ($i = 0; $i < $max; ++$i) |
|
80 | 80 | { |
81 | 81 | // get the current position in $text |
82 | 82 | $pos = $i * $blocksz; |
83 | 83 | |
84 | 84 | // make sure we don't extend past the length of $text |
85 | 85 | $byte_len = $blocksz; |
86 | - if(($pos + $byte_len) > $len) |
|
86 | + if (($pos + $byte_len) > $len) |
|
87 | 87 | $byte_len -= ($pos + $byte_len) - $len; |
88 | 88 | |
89 | 89 | // encrypt the register |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | $block = substr($text, $pos, $byte_len); |
95 | 95 | |
96 | 96 | // xor the block |
97 | - for($j = 0; $j < $byte_len; ++$j) |
|
97 | + for ($j = 0; $j < $byte_len; ++$j) |
|
98 | 98 | $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
99 | 99 | |
100 | 100 | // replace the plain text block with the encrypted block |
@@ -120,14 +120,14 @@ discard block |
||
120 | 120 | $blocksz = $this->cipher->blockSize(); |
121 | 121 | |
122 | 122 | $max = $len / $blocksz; |
123 | - for($i = 0; $i < $max; ++$i) |
|
123 | + for ($i = 0; $i < $max; ++$i) |
|
124 | 124 | { |
125 | 125 | // get the current position in $text |
126 | 126 | $pos = $i * $blocksz; |
127 | 127 | |
128 | 128 | // make sure we don't extend past the length of $text |
129 | 129 | $byte_len = $blocksz; |
130 | - if(($pos + $byte_len) > $len) |
|
130 | + if (($pos + $byte_len) > $len) |
|
131 | 131 | $byte_len -= ($pos + $byte_len) - $len; |
132 | 132 | |
133 | 133 | // encrypt the register |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | $block = substr($text, $pos, $byte_len); |
139 | 139 | |
140 | 140 | // xor the block with the register (which contains the IV) |
141 | - for($j = 0; $j < $byte_len; ++$j) |
|
141 | + for ($j = 0; $j < $byte_len; ++$j) |
|
142 | 142 | $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
143 | 143 | |
144 | 144 | // replace the encrypted block with the plain text |
@@ -184,11 +184,11 @@ discard block |
||
184 | 184 | |
185 | 185 | // starting at the last byte, loop through each byte until |
186 | 186 | // we find one that can be incremented |
187 | - for($i = $pos; $i >= 0; --$i) |
|
187 | + for ($i = $pos; $i >= 0; --$i) |
|
188 | 188 | { |
189 | 189 | // if we reached the last byte, set it to 0x00, then |
190 | 190 | // loop one more time to increment the next byte |
191 | - if(ord($this->register[$i]) == 0xff) |
|
191 | + if (ord($this->register[$i]) == 0xff) |
|
192 | 192 | $this->register[$i] = chr(0x00); |
193 | 193 | else |
194 | 194 | { |
@@ -48,8 +48,9 @@ discard block |
||
48 | 48 | parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
51 | + if($cipher->type() != Cipher::BLOCK) { |
|
52 | + trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
53 | 54 | } |
54 | 55 | |
55 | 56 | |
@@ -83,8 +84,9 @@ discard block |
||
83 | 84 | |
84 | 85 | // make sure we don't extend past the length of $text |
85 | 86 | $byte_len = $blocksz; |
86 | - if(($pos + $byte_len) > $len) |
|
87 | - $byte_len -= ($pos + $byte_len) - $len; |
|
87 | + if(($pos + $byte_len) > $len) { |
|
88 | + $byte_len -= ($pos + $byte_len) - $len; |
|
89 | + } |
|
88 | 90 | |
89 | 91 | // encrypt the register |
90 | 92 | $this->enc_register = $this->register; |
@@ -94,8 +96,9 @@ discard block |
||
94 | 96 | $block = substr($text, $pos, $byte_len); |
95 | 97 | |
96 | 98 | // xor the block |
97 | - for($j = 0; $j < $byte_len; ++$j) |
|
98 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
99 | + for($j = 0; $j < $byte_len; ++$j) { |
|
100 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
101 | + } |
|
99 | 102 | |
100 | 103 | // replace the plain text block with the encrypted block |
101 | 104 | $text = substr_replace($text, $block, $pos, $byte_len); |
@@ -127,8 +130,9 @@ discard block |
||
127 | 130 | |
128 | 131 | // make sure we don't extend past the length of $text |
129 | 132 | $byte_len = $blocksz; |
130 | - if(($pos + $byte_len) > $len) |
|
131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
133 | + if(($pos + $byte_len) > $len) { |
|
134 | + $byte_len -= ($pos + $byte_len) - $len; |
|
135 | + } |
|
132 | 136 | |
133 | 137 | // encrypt the register |
134 | 138 | $this->enc_register = $this->register; |
@@ -138,8 +142,9 @@ discard block |
||
138 | 142 | $block = substr($text, $pos, $byte_len); |
139 | 143 | |
140 | 144 | // xor the block with the register (which contains the IV) |
141 | - for($j = 0; $j < $byte_len; ++$j) |
|
142 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
145 | + for($j = 0; $j < $byte_len; ++$j) { |
|
146 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
147 | + } |
|
143 | 148 | |
144 | 149 | // replace the encrypted block with the plain text |
145 | 150 | $text = substr_replace($text, $block, $pos, $byte_len); |
@@ -188,9 +193,9 @@ discard block |
||
188 | 193 | { |
189 | 194 | // if we reached the last byte, set it to 0x00, then |
190 | 195 | // loop one more time to increment the next byte |
191 | - if(ord($this->register[$i]) == 0xff) |
|
192 | - $this->register[$i] = chr(0x00); |
|
193 | - else |
|
196 | + if(ord($this->register[$i]) == 0xff) { |
|
197 | + $this->register[$i] = chr(0x00); |
|
198 | + } else |
|
194 | 199 | { |
195 | 200 | // now increment the byte by 1 |
196 | 201 | $this->register[$i] = chr(ord($this->register[$i]) + 1); |
@@ -37,128 +37,128 @@ |
||
37 | 37 | */ |
38 | 38 | class Mode_NOFB extends Mode |
39 | 39 | { |
40 | - /** |
|
41 | - * Constructor |
|
42 | - * Sets the cipher object that will be used for encryption |
|
43 | - * |
|
44 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function __construct($cipher) |
|
48 | - { |
|
49 | - parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
|
50 | - |
|
51 | - // this works with only block Ciphers |
|
52 | - if($cipher->type() != Cipher::BLOCK) |
|
53 | - trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Destructor |
|
59 | - * |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public function __destruct() |
|
63 | - { |
|
64 | - parent::__destruct(); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
70 | - * |
|
71 | - * @param string $text the string to be encrypted |
|
72 | - * @return boolean Returns true |
|
73 | - */ |
|
74 | - public function encrypt(&$text) |
|
75 | - { |
|
76 | - $len = strlen($text); |
|
77 | - $blocksz = $this->cipher->blockSize(); |
|
78 | - |
|
79 | - $max = $len / $blocksz; |
|
80 | - for($i = 0; $i < $max; ++$i) |
|
81 | - { |
|
82 | - // current position in the text |
|
83 | - $pos = $i * $blocksz; |
|
84 | - |
|
85 | - // make sure we don't extend past the length of $text |
|
86 | - $byte_len = $blocksz; |
|
87 | - if(($pos + $blocksz) > $len) |
|
88 | - $byte_len -= ($pos + $blocksz) - $len; |
|
89 | - |
|
90 | - // encrypt the register |
|
91 | - $this->enc_register = $this->register; |
|
92 | - $this->cipher->encrypt($this->enc_register); |
|
93 | - |
|
94 | - // now grab a block of text and a block of from the register, and XOR them |
|
95 | - $block = substr($text, $pos, $byte_len); |
|
96 | - for($j = 0; $j < $byte_len; ++$j) |
|
97 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
98 | - |
|
99 | - // replace the plain text block with encrypted text |
|
100 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
101 | - |
|
102 | - // shift the register left n-bytes, append n-bytes of encrypted register |
|
103 | - $this->register = substr($this->register, $byte_len); |
|
104 | - $this->register .= substr($this->enc_register, 0, $byte_len); |
|
105 | - } |
|
106 | - |
|
107 | - return true; |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
113 | - * |
|
114 | - * @param string $text the string to be decrypted |
|
115 | - * @return boolean Returns true |
|
116 | - */ |
|
117 | - public function decrypt(&$text) |
|
118 | - { |
|
119 | - $len = strlen($text); |
|
120 | - $blocksz = $this->cipher->blockSize(); |
|
121 | - |
|
122 | - $max = $len / $blocksz; |
|
123 | - for($i = 0; $i < $max; ++$i) |
|
124 | - { |
|
125 | - // current position within $text |
|
126 | - $pos = $i * $blocksz; |
|
127 | - |
|
128 | - // make sure we don't extend past the length of $text |
|
129 | - $byte_len = $blocksz; |
|
130 | - if(($pos + $byte_len) > $len) |
|
131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
132 | - |
|
133 | - // encrypt the register |
|
134 | - $this->enc_register = $this->register; |
|
135 | - $this->cipher->encrypt($this->enc_register); |
|
136 | - |
|
137 | - // shift the register left n-bytes, append n-bytes of encrypted register |
|
138 | - $this->register = substr($this->register, $byte_len); |
|
139 | - $this->register .= substr($this->enc_register, 0, $byte_len); |
|
140 | - |
|
141 | - // now grab a block of text and xor with the register |
|
142 | - $block = substr($text, $pos, $byte_len); |
|
143 | - for($j = 0; $j < $byte_len; ++$j) |
|
144 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
145 | - |
|
146 | - // replace the encrypted block with plain text |
|
147 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
148 | - } |
|
149 | - |
|
150 | - return true; |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * This mode requires an IV |
|
156 | - * |
|
157 | - * @return boolean true |
|
158 | - */ |
|
159 | - public function requiresIV() |
|
160 | - { |
|
161 | - return true; |
|
162 | - } |
|
40 | + /** |
|
41 | + * Constructor |
|
42 | + * Sets the cipher object that will be used for encryption |
|
43 | + * |
|
44 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function __construct($cipher) |
|
48 | + { |
|
49 | + parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
|
50 | + |
|
51 | + // this works with only block Ciphers |
|
52 | + if($cipher->type() != Cipher::BLOCK) |
|
53 | + trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Destructor |
|
59 | + * |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public function __destruct() |
|
63 | + { |
|
64 | + parent::__destruct(); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
70 | + * |
|
71 | + * @param string $text the string to be encrypted |
|
72 | + * @return boolean Returns true |
|
73 | + */ |
|
74 | + public function encrypt(&$text) |
|
75 | + { |
|
76 | + $len = strlen($text); |
|
77 | + $blocksz = $this->cipher->blockSize(); |
|
78 | + |
|
79 | + $max = $len / $blocksz; |
|
80 | + for($i = 0; $i < $max; ++$i) |
|
81 | + { |
|
82 | + // current position in the text |
|
83 | + $pos = $i * $blocksz; |
|
84 | + |
|
85 | + // make sure we don't extend past the length of $text |
|
86 | + $byte_len = $blocksz; |
|
87 | + if(($pos + $blocksz) > $len) |
|
88 | + $byte_len -= ($pos + $blocksz) - $len; |
|
89 | + |
|
90 | + // encrypt the register |
|
91 | + $this->enc_register = $this->register; |
|
92 | + $this->cipher->encrypt($this->enc_register); |
|
93 | + |
|
94 | + // now grab a block of text and a block of from the register, and XOR them |
|
95 | + $block = substr($text, $pos, $byte_len); |
|
96 | + for($j = 0; $j < $byte_len; ++$j) |
|
97 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
98 | + |
|
99 | + // replace the plain text block with encrypted text |
|
100 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
101 | + |
|
102 | + // shift the register left n-bytes, append n-bytes of encrypted register |
|
103 | + $this->register = substr($this->register, $byte_len); |
|
104 | + $this->register .= substr($this->enc_register, 0, $byte_len); |
|
105 | + } |
|
106 | + |
|
107 | + return true; |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
113 | + * |
|
114 | + * @param string $text the string to be decrypted |
|
115 | + * @return boolean Returns true |
|
116 | + */ |
|
117 | + public function decrypt(&$text) |
|
118 | + { |
|
119 | + $len = strlen($text); |
|
120 | + $blocksz = $this->cipher->blockSize(); |
|
121 | + |
|
122 | + $max = $len / $blocksz; |
|
123 | + for($i = 0; $i < $max; ++$i) |
|
124 | + { |
|
125 | + // current position within $text |
|
126 | + $pos = $i * $blocksz; |
|
127 | + |
|
128 | + // make sure we don't extend past the length of $text |
|
129 | + $byte_len = $blocksz; |
|
130 | + if(($pos + $byte_len) > $len) |
|
131 | + $byte_len -= ($pos + $byte_len) - $len; |
|
132 | + |
|
133 | + // encrypt the register |
|
134 | + $this->enc_register = $this->register; |
|
135 | + $this->cipher->encrypt($this->enc_register); |
|
136 | + |
|
137 | + // shift the register left n-bytes, append n-bytes of encrypted register |
|
138 | + $this->register = substr($this->register, $byte_len); |
|
139 | + $this->register .= substr($this->enc_register, 0, $byte_len); |
|
140 | + |
|
141 | + // now grab a block of text and xor with the register |
|
142 | + $block = substr($text, $pos, $byte_len); |
|
143 | + for($j = 0; $j < $byte_len; ++$j) |
|
144 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
145 | + |
|
146 | + // replace the encrypted block with plain text |
|
147 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
148 | + } |
|
149 | + |
|
150 | + return true; |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * This mode requires an IV |
|
156 | + * |
|
157 | + * @return boolean true |
|
158 | + */ |
|
159 | + public function requiresIV() |
|
160 | + { |
|
161 | + return true; |
|
162 | + } |
|
163 | 163 | } |
164 | 164 | ?> |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
50 | 50 | |
51 | 51 | // this works with only block Ciphers |
52 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | + if ($cipher->type() != Cipher::BLOCK) |
|
53 | 53 | trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
54 | 54 | } |
55 | 55 | |
@@ -77,14 +77,14 @@ discard block |
||
77 | 77 | $blocksz = $this->cipher->blockSize(); |
78 | 78 | |
79 | 79 | $max = $len / $blocksz; |
80 | - for($i = 0; $i < $max; ++$i) |
|
80 | + for ($i = 0; $i < $max; ++$i) |
|
81 | 81 | { |
82 | 82 | // current position in the text |
83 | 83 | $pos = $i * $blocksz; |
84 | 84 | |
85 | 85 | // make sure we don't extend past the length of $text |
86 | 86 | $byte_len = $blocksz; |
87 | - if(($pos + $blocksz) > $len) |
|
87 | + if (($pos + $blocksz) > $len) |
|
88 | 88 | $byte_len -= ($pos + $blocksz) - $len; |
89 | 89 | |
90 | 90 | // encrypt the register |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | |
94 | 94 | // now grab a block of text and a block of from the register, and XOR them |
95 | 95 | $block = substr($text, $pos, $byte_len); |
96 | - for($j = 0; $j < $byte_len; ++$j) |
|
96 | + for ($j = 0; $j < $byte_len; ++$j) |
|
97 | 97 | $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
98 | 98 | |
99 | 99 | // replace the plain text block with encrypted text |
@@ -120,14 +120,14 @@ discard block |
||
120 | 120 | $blocksz = $this->cipher->blockSize(); |
121 | 121 | |
122 | 122 | $max = $len / $blocksz; |
123 | - for($i = 0; $i < $max; ++$i) |
|
123 | + for ($i = 0; $i < $max; ++$i) |
|
124 | 124 | { |
125 | 125 | // current position within $text |
126 | 126 | $pos = $i * $blocksz; |
127 | 127 | |
128 | 128 | // make sure we don't extend past the length of $text |
129 | 129 | $byte_len = $blocksz; |
130 | - if(($pos + $byte_len) > $len) |
|
130 | + if (($pos + $byte_len) > $len) |
|
131 | 131 | $byte_len -= ($pos + $byte_len) - $len; |
132 | 132 | |
133 | 133 | // encrypt the register |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | |
141 | 141 | // now grab a block of text and xor with the register |
142 | 142 | $block = substr($text, $pos, $byte_len); |
143 | - for($j = 0; $j < $byte_len; ++$j) |
|
143 | + for ($j = 0; $j < $byte_len; ++$j) |
|
144 | 144 | $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
145 | 145 | |
146 | 146 | // replace the encrypted block with plain text |
@@ -49,8 +49,9 @@ discard block |
||
49 | 49 | parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
50 | 50 | |
51 | 51 | // this works with only block Ciphers |
52 | - if($cipher->type() != Cipher::BLOCK) |
|
53 | - trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
52 | + if($cipher->type() != Cipher::BLOCK) { |
|
53 | + trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
54 | + } |
|
54 | 55 | } |
55 | 56 | |
56 | 57 | |
@@ -84,8 +85,9 @@ discard block |
||
84 | 85 | |
85 | 86 | // make sure we don't extend past the length of $text |
86 | 87 | $byte_len = $blocksz; |
87 | - if(($pos + $blocksz) > $len) |
|
88 | - $byte_len -= ($pos + $blocksz) - $len; |
|
88 | + if(($pos + $blocksz) > $len) { |
|
89 | + $byte_len -= ($pos + $blocksz) - $len; |
|
90 | + } |
|
89 | 91 | |
90 | 92 | // encrypt the register |
91 | 93 | $this->enc_register = $this->register; |
@@ -93,8 +95,9 @@ discard block |
||
93 | 95 | |
94 | 96 | // now grab a block of text and a block of from the register, and XOR them |
95 | 97 | $block = substr($text, $pos, $byte_len); |
96 | - for($j = 0; $j < $byte_len; ++$j) |
|
97 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
98 | + for($j = 0; $j < $byte_len; ++$j) { |
|
99 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
100 | + } |
|
98 | 101 | |
99 | 102 | // replace the plain text block with encrypted text |
100 | 103 | $text = substr_replace($text, $block, $pos, $byte_len); |
@@ -127,8 +130,9 @@ discard block |
||
127 | 130 | |
128 | 131 | // make sure we don't extend past the length of $text |
129 | 132 | $byte_len = $blocksz; |
130 | - if(($pos + $byte_len) > $len) |
|
131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
133 | + if(($pos + $byte_len) > $len) { |
|
134 | + $byte_len -= ($pos + $byte_len) - $len; |
|
135 | + } |
|
132 | 136 | |
133 | 137 | // encrypt the register |
134 | 138 | $this->enc_register = $this->register; |
@@ -140,8 +144,9 @@ discard block |
||
140 | 144 | |
141 | 145 | // now grab a block of text and xor with the register |
142 | 146 | $block = substr($text, $pos, $byte_len); |
143 | - for($j = 0; $j < $byte_len; ++$j) |
|
144 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
147 | + for($j = 0; $j < $byte_len; ++$j) { |
|
148 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
149 | + } |
|
145 | 150 | |
146 | 151 | // replace the encrypted block with plain text |
147 | 152 | $text = substr_replace($text, $block, $pos, $byte_len); |
@@ -36,132 +36,132 @@ |
||
36 | 36 | */ |
37 | 37 | class Mode_CBC extends Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * Sets the cipher object that will be used for encryption |
|
42 | - * |
|
43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - function __construct($cipher) |
|
47 | - { |
|
48 | - parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
|
49 | - |
|
50 | - // this works with only block Ciphers |
|
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Destructor |
|
58 | - * |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __destruct() |
|
62 | - { |
|
63 | - parent::__destruct(); |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | - * to the constructor in CBC mode |
|
70 | - * The steps to encrypt using CBC are as follows |
|
71 | - * 1) Get a block of plain text data to use in the Cipher |
|
72 | - * 2) XOR the block with IV (if it's the first round) or the previous |
|
73 | - * round's encrypted result |
|
74 | - * 3) Encrypt the block using the cipher |
|
75 | - * 4) Save the encrypted block to use in the next round |
|
76 | - * |
|
77 | - * @param string $text the string to be encrypted in CBC mode |
|
78 | - * @return boolean Returns true |
|
79 | - */ |
|
80 | - public function encrypt(&$text) |
|
81 | - { |
|
82 | - $this->pad($text); |
|
83 | - $blocksz = $this->cipher->blockSize(); |
|
84 | - |
|
85 | - $max = strlen($text) / $blocksz; |
|
86 | - for($i = 0; $i < $max; ++$i) |
|
87 | - { |
|
88 | - // get the current position in $text |
|
89 | - $pos = $i * $blocksz; |
|
90 | - |
|
91 | - // grab a block of plain text |
|
92 | - $block = substr($text, $pos, $blocksz); |
|
93 | - |
|
94 | - // xor the block with the register |
|
95 | - for($j = 0; $j < $blocksz; ++$j) |
|
96 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
97 | - |
|
98 | - // encrypt the block, and save it back to the register |
|
99 | - $this->cipher->encrypt($block); |
|
100 | - $this->register = $block; |
|
101 | - |
|
102 | - // replace the plain text block with the cipher text |
|
103 | - $text = substr_replace($text, $this->register, $pos, $blocksz); |
|
104 | - } |
|
105 | - |
|
106 | - return true; |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
112 | - * to the constructor in CBC mode |
|
113 | - * The decryption algorithm requires the following steps |
|
114 | - * 1) Get the first block of encrypted text, save it |
|
115 | - * 2) Decrypt the block |
|
116 | - * 3) XOR the decrypted block (if it's the first pass use the IV, |
|
117 | - * else use the encrypted block from step 1 |
|
118 | - * 4) the result from the XOR will be a plain text block, save it |
|
119 | - * 5) assign the encrypted block from step 1 to use in the next round |
|
120 | - * |
|
121 | - * @param string $text the string to be decrypted in CBC mode |
|
122 | - * @return boolean Returns true |
|
123 | - */ |
|
124 | - public function decrypt(&$text) |
|
125 | - { |
|
126 | - $blocksz = $this->cipher->blockSize(); |
|
127 | - |
|
128 | - $max = strlen($text) / $blocksz; |
|
129 | - for($i = 0; $i < $max; ++$i) |
|
130 | - { |
|
131 | - // get the current position in $text |
|
132 | - $pos = $i * $blocksz; |
|
133 | - |
|
134 | - // grab a block of cipher text, and save it for use later |
|
135 | - $block = substr($text, $pos, $blocksz); |
|
136 | - $tmp_block = $block; |
|
137 | - |
|
138 | - // decrypt the block of cipher text |
|
139 | - $this->cipher->decrypt($block); |
|
140 | - |
|
141 | - // xor the block with the register |
|
142 | - for($j = 0; $j < $blocksz; ++$j) |
|
143 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
144 | - |
|
145 | - // replace the block of cipher text with plain text |
|
146 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
147 | - |
|
148 | - // save the cipher text block to the register |
|
149 | - $this->register = $tmp_block; |
|
150 | - } |
|
151 | - |
|
152 | - $this->strip($text); |
|
153 | - return true; |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * This mode requires an IV |
|
159 | - * |
|
160 | - * @return boolean Returns True |
|
161 | - */ |
|
162 | - public function requiresIV() |
|
163 | - { |
|
164 | - return true; |
|
165 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * Sets the cipher object that will be used for encryption |
|
42 | + * |
|
43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + function __construct($cipher) |
|
47 | + { |
|
48 | + parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
|
49 | + |
|
50 | + // this works with only block Ciphers |
|
51 | + if($cipher->type() != Cipher::BLOCK) |
|
52 | + trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Destructor |
|
58 | + * |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __destruct() |
|
62 | + { |
|
63 | + parent::__destruct(); |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | + * to the constructor in CBC mode |
|
70 | + * The steps to encrypt using CBC are as follows |
|
71 | + * 1) Get a block of plain text data to use in the Cipher |
|
72 | + * 2) XOR the block with IV (if it's the first round) or the previous |
|
73 | + * round's encrypted result |
|
74 | + * 3) Encrypt the block using the cipher |
|
75 | + * 4) Save the encrypted block to use in the next round |
|
76 | + * |
|
77 | + * @param string $text the string to be encrypted in CBC mode |
|
78 | + * @return boolean Returns true |
|
79 | + */ |
|
80 | + public function encrypt(&$text) |
|
81 | + { |
|
82 | + $this->pad($text); |
|
83 | + $blocksz = $this->cipher->blockSize(); |
|
84 | + |
|
85 | + $max = strlen($text) / $blocksz; |
|
86 | + for($i = 0; $i < $max; ++$i) |
|
87 | + { |
|
88 | + // get the current position in $text |
|
89 | + $pos = $i * $blocksz; |
|
90 | + |
|
91 | + // grab a block of plain text |
|
92 | + $block = substr($text, $pos, $blocksz); |
|
93 | + |
|
94 | + // xor the block with the register |
|
95 | + for($j = 0; $j < $blocksz; ++$j) |
|
96 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
97 | + |
|
98 | + // encrypt the block, and save it back to the register |
|
99 | + $this->cipher->encrypt($block); |
|
100 | + $this->register = $block; |
|
101 | + |
|
102 | + // replace the plain text block with the cipher text |
|
103 | + $text = substr_replace($text, $this->register, $pos, $blocksz); |
|
104 | + } |
|
105 | + |
|
106 | + return true; |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
112 | + * to the constructor in CBC mode |
|
113 | + * The decryption algorithm requires the following steps |
|
114 | + * 1) Get the first block of encrypted text, save it |
|
115 | + * 2) Decrypt the block |
|
116 | + * 3) XOR the decrypted block (if it's the first pass use the IV, |
|
117 | + * else use the encrypted block from step 1 |
|
118 | + * 4) the result from the XOR will be a plain text block, save it |
|
119 | + * 5) assign the encrypted block from step 1 to use in the next round |
|
120 | + * |
|
121 | + * @param string $text the string to be decrypted in CBC mode |
|
122 | + * @return boolean Returns true |
|
123 | + */ |
|
124 | + public function decrypt(&$text) |
|
125 | + { |
|
126 | + $blocksz = $this->cipher->blockSize(); |
|
127 | + |
|
128 | + $max = strlen($text) / $blocksz; |
|
129 | + for($i = 0; $i < $max; ++$i) |
|
130 | + { |
|
131 | + // get the current position in $text |
|
132 | + $pos = $i * $blocksz; |
|
133 | + |
|
134 | + // grab a block of cipher text, and save it for use later |
|
135 | + $block = substr($text, $pos, $blocksz); |
|
136 | + $tmp_block = $block; |
|
137 | + |
|
138 | + // decrypt the block of cipher text |
|
139 | + $this->cipher->decrypt($block); |
|
140 | + |
|
141 | + // xor the block with the register |
|
142 | + for($j = 0; $j < $blocksz; ++$j) |
|
143 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
144 | + |
|
145 | + // replace the block of cipher text with plain text |
|
146 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
147 | + |
|
148 | + // save the cipher text block to the register |
|
149 | + $this->register = $tmp_block; |
|
150 | + } |
|
151 | + |
|
152 | + $this->strip($text); |
|
153 | + return true; |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * This mode requires an IV |
|
159 | + * |
|
160 | + * @return boolean Returns True |
|
161 | + */ |
|
162 | + public function requiresIV() |
|
163 | + { |
|
164 | + return true; |
|
165 | + } |
|
166 | 166 | } |
167 | 167 | ?> |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
51 | + if ($cipher->type() != Cipher::BLOCK) |
|
52 | 52 | trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
53 | 53 | } |
54 | 54 | |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $blocksz = $this->cipher->blockSize(); |
84 | 84 | |
85 | 85 | $max = strlen($text) / $blocksz; |
86 | - for($i = 0; $i < $max; ++$i) |
|
86 | + for ($i = 0; $i < $max; ++$i) |
|
87 | 87 | { |
88 | 88 | // get the current position in $text |
89 | 89 | $pos = $i * $blocksz; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $block = substr($text, $pos, $blocksz); |
93 | 93 | |
94 | 94 | // xor the block with the register |
95 | - for($j = 0; $j < $blocksz; ++$j) |
|
95 | + for ($j = 0; $j < $blocksz; ++$j) |
|
96 | 96 | $block[$j] = $block[$j] ^ $this->register[$j]; |
97 | 97 | |
98 | 98 | // encrypt the block, and save it back to the register |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $blocksz = $this->cipher->blockSize(); |
127 | 127 | |
128 | 128 | $max = strlen($text) / $blocksz; |
129 | - for($i = 0; $i < $max; ++$i) |
|
129 | + for ($i = 0; $i < $max; ++$i) |
|
130 | 130 | { |
131 | 131 | // get the current position in $text |
132 | 132 | $pos = $i * $blocksz; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $this->cipher->decrypt($block); |
140 | 140 | |
141 | 141 | // xor the block with the register |
142 | - for($j = 0; $j < $blocksz; ++$j) |
|
142 | + for ($j = 0; $j < $blocksz; ++$j) |
|
143 | 143 | $block[$j] = $block[$j] ^ $this->register[$j]; |
144 | 144 | |
145 | 145 | // replace the block of cipher text with plain text |
@@ -48,8 +48,9 @@ discard block |
||
48 | 48 | parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
51 | + if($cipher->type() != Cipher::BLOCK) { |
|
52 | + trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
53 | 54 | } |
54 | 55 | |
55 | 56 | |
@@ -92,8 +93,9 @@ discard block |
||
92 | 93 | $block = substr($text, $pos, $blocksz); |
93 | 94 | |
94 | 95 | // xor the block with the register |
95 | - for($j = 0; $j < $blocksz; ++$j) |
|
96 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
96 | + for($j = 0; $j < $blocksz; ++$j) { |
|
97 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
98 | + } |
|
97 | 99 | |
98 | 100 | // encrypt the block, and save it back to the register |
99 | 101 | $this->cipher->encrypt($block); |
@@ -139,8 +141,9 @@ discard block |
||
139 | 141 | $this->cipher->decrypt($block); |
140 | 142 | |
141 | 143 | // xor the block with the register |
142 | - for($j = 0; $j < $blocksz; ++$j) |
|
143 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
144 | + for($j = 0; $j < $blocksz; ++$j) { |
|
145 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
146 | + } |
|
144 | 147 | |
145 | 148 | // replace the block of cipher text with plain text |
146 | 149 | $text = substr_replace($text, $block, $pos, $blocksz); |
@@ -38,32 +38,32 @@ |
||
38 | 38 | */ |
39 | 39 | class Mode_Stream extends Mode_Raw |
40 | 40 | { |
41 | - /** |
|
42 | - * Constructor |
|
43 | - * Sets the cipher object that will be used for encryption |
|
44 | - * |
|
45 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - public function __construct($cipher) |
|
49 | - { |
|
50 | - // call the secondary 'constructor' from the parent |
|
51 | - parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
|
41 | + /** |
|
42 | + * Constructor |
|
43 | + * Sets the cipher object that will be used for encryption |
|
44 | + * |
|
45 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + public function __construct($cipher) |
|
49 | + { |
|
50 | + // call the secondary 'constructor' from the parent |
|
51 | + parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
|
52 | 52 | |
53 | - // this works with only stream Ciphers |
|
54 | - if($cipher->type() != Cipher::STREAM) |
|
55 | - trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
56 | - } |
|
53 | + // this works with only stream Ciphers |
|
54 | + if($cipher->type() != Cipher::STREAM) |
|
55 | + trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * Destructor |
|
61 | - * |
|
62 | - * @return void |
|
63 | - */ |
|
64 | - public function __destruct() |
|
65 | - { |
|
66 | - parent::__destruct(); |
|
67 | - } |
|
59 | + /** |
|
60 | + * Destructor |
|
61 | + * |
|
62 | + * @return void |
|
63 | + */ |
|
64 | + public function __destruct() |
|
65 | + { |
|
66 | + parent::__destruct(); |
|
67 | + } |
|
68 | 68 | } |
69 | 69 | ?> |
@@ -51,7 +51,7 @@ |
||
51 | 51 | parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
52 | 52 | |
53 | 53 | // this works with only stream Ciphers |
54 | - if($cipher->type() != Cipher::STREAM) |
|
54 | + if ($cipher->type() != Cipher::STREAM) |
|
55 | 55 | trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
56 | 56 | } |
57 | 57 |
@@ -51,8 +51,9 @@ |
||
51 | 51 | parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
52 | 52 | |
53 | 53 | // this works with only stream Ciphers |
54 | - if($cipher->type() != Cipher::STREAM) |
|
55 | - trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
54 | + if($cipher->type() != Cipher::STREAM) { |
|
55 | + trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
56 | + } |
|
56 | 57 | } |
57 | 58 | |
58 | 59 |
@@ -36,91 +36,91 @@ |
||
36 | 36 | */ |
37 | 37 | class Mode_OFB extends Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * Sets the cipher object that will be used for encryption |
|
42 | - * |
|
43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function __construct($cipher) |
|
47 | - { |
|
48 | - parent::__construct(PHP_Crypt::MODE_OFB, $cipher); |
|
49 | - |
|
50 | - // this works with only block Ciphers |
|
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("OFB mode requires a block cipher", E_USER_WARNING); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Destructor |
|
58 | - * |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __destruct() |
|
62 | - { |
|
63 | - parent::__destruct(); |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | - * |
|
70 | - * @param string $text the string to be encrypted |
|
71 | - * @return boolean Returns true |
|
72 | - */ |
|
73 | - public function encrypt(&$text) |
|
74 | - { |
|
75 | - $max = strlen($text); |
|
76 | - for($i = 0; $i < $max; ++$i) |
|
77 | - { |
|
78 | - $this->enc_register = $this->register; |
|
79 | - $this->cipher->encrypt($this->enc_register); |
|
80 | - $text[$i] = $text[$i] ^ $this->enc_register[0]; |
|
81 | - |
|
82 | - // shift the register left |
|
83 | - $this->register = substr($this->register, 1); |
|
84 | - $this->register .= $this->enc_register[0]; |
|
85 | - } |
|
86 | - |
|
87 | - return true; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
93 | - * |
|
94 | - * @param string $text the string to be decrypted |
|
95 | - * @return boolean Returns true |
|
96 | - */ |
|
97 | - public function decrypt(&$text) |
|
98 | - { |
|
99 | - $max = strlen($text); |
|
100 | - for($i = 0; $i < $max; ++$i) |
|
101 | - { |
|
102 | - $this->enc_register = $this->register; |
|
103 | - $this->cipher->encrypt($this->enc_register); |
|
104 | - |
|
105 | - // shift the register left |
|
106 | - $this->register = substr($this->register, 1); |
|
107 | - $this->register .= $this->enc_register[0]; |
|
108 | - |
|
109 | - $text[$i] = $text[$i] ^ $this->enc_register[0]; |
|
110 | - } |
|
111 | - |
|
112 | - return true; |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * This mode requires an IV |
|
118 | - * |
|
119 | - * @return boolean true |
|
120 | - */ |
|
121 | - public function requiresIV() |
|
122 | - { |
|
123 | - return true; |
|
124 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * Sets the cipher object that will be used for encryption |
|
42 | + * |
|
43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function __construct($cipher) |
|
47 | + { |
|
48 | + parent::__construct(PHP_Crypt::MODE_OFB, $cipher); |
|
49 | + |
|
50 | + // this works with only block Ciphers |
|
51 | + if($cipher->type() != Cipher::BLOCK) |
|
52 | + trigger_error("OFB mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Destructor |
|
58 | + * |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __destruct() |
|
62 | + { |
|
63 | + parent::__destruct(); |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | + * |
|
70 | + * @param string $text the string to be encrypted |
|
71 | + * @return boolean Returns true |
|
72 | + */ |
|
73 | + public function encrypt(&$text) |
|
74 | + { |
|
75 | + $max = strlen($text); |
|
76 | + for($i = 0; $i < $max; ++$i) |
|
77 | + { |
|
78 | + $this->enc_register = $this->register; |
|
79 | + $this->cipher->encrypt($this->enc_register); |
|
80 | + $text[$i] = $text[$i] ^ $this->enc_register[0]; |
|
81 | + |
|
82 | + // shift the register left |
|
83 | + $this->register = substr($this->register, 1); |
|
84 | + $this->register .= $this->enc_register[0]; |
|
85 | + } |
|
86 | + |
|
87 | + return true; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
93 | + * |
|
94 | + * @param string $text the string to be decrypted |
|
95 | + * @return boolean Returns true |
|
96 | + */ |
|
97 | + public function decrypt(&$text) |
|
98 | + { |
|
99 | + $max = strlen($text); |
|
100 | + for($i = 0; $i < $max; ++$i) |
|
101 | + { |
|
102 | + $this->enc_register = $this->register; |
|
103 | + $this->cipher->encrypt($this->enc_register); |
|
104 | + |
|
105 | + // shift the register left |
|
106 | + $this->register = substr($this->register, 1); |
|
107 | + $this->register .= $this->enc_register[0]; |
|
108 | + |
|
109 | + $text[$i] = $text[$i] ^ $this->enc_register[0]; |
|
110 | + } |
|
111 | + |
|
112 | + return true; |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * This mode requires an IV |
|
118 | + * |
|
119 | + * @return boolean true |
|
120 | + */ |
|
121 | + public function requiresIV() |
|
122 | + { |
|
123 | + return true; |
|
124 | + } |
|
125 | 125 | } |
126 | 126 | ?> |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | parent::__construct(PHP_Crypt::MODE_OFB, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
51 | + if ($cipher->type() != Cipher::BLOCK) |
|
52 | 52 | trigger_error("OFB mode requires a block cipher", E_USER_WARNING); |
53 | 53 | } |
54 | 54 | |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | public function encrypt(&$text) |
74 | 74 | { |
75 | 75 | $max = strlen($text); |
76 | - for($i = 0; $i < $max; ++$i) |
|
76 | + for ($i = 0; $i < $max; ++$i) |
|
77 | 77 | { |
78 | 78 | $this->enc_register = $this->register; |
79 | 79 | $this->cipher->encrypt($this->enc_register); |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | public function decrypt(&$text) |
98 | 98 | { |
99 | 99 | $max = strlen($text); |
100 | - for($i = 0; $i < $max; ++$i) |
|
100 | + for ($i = 0; $i < $max; ++$i) |
|
101 | 101 | { |
102 | 102 | $this->enc_register = $this->register; |
103 | 103 | $this->cipher->encrypt($this->enc_register); |
@@ -48,8 +48,9 @@ |
||
48 | 48 | parent::__construct(PHP_Crypt::MODE_OFB, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("OFB mode requires a block cipher", E_USER_WARNING); |
|
51 | + if($cipher->type() != Cipher::BLOCK) { |
|
52 | + trigger_error("OFB mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
53 | 54 | } |
54 | 55 | |
55 | 56 |