Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 846 Rev 874
Line 24... Line 24...
24
 *
24
 *
25
 *    echo $des->decrypt($des->encrypt($plaintext));
25
 *    echo $des->decrypt($des->encrypt($plaintext));
26
 * ?>
26
 * ?>
27
 * </code>
27
 * </code>
28
 *
28
 *
-
 
29
 * @category  Crypt
-
 
30
 * @package   TripleDES
29
 * @author    Jim Wigginton <terrafrost@php.net>
31
 * @author    Jim Wigginton <terrafrost@php.net>
30
 * @copyright 2007 Jim Wigginton
32
 * @copyright 2007 Jim Wigginton
31
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
33
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
32
 * @link      http://phpseclib.sourceforge.net
34
 * @link      http://phpseclib.sourceforge.net
33
 */
35
 */
Line 35... Line 37...
35
namespace phpseclib3\Crypt;
37
namespace phpseclib3\Crypt;
36
 
38
 
37
/**
39
/**
38
 * Pure-PHP implementation of Triple DES.
40
 * Pure-PHP implementation of Triple DES.
39
 *
41
 *
-
 
42
 * @package TripleDES
40
 * @author  Jim Wigginton <terrafrost@php.net>
43
 * @author  Jim Wigginton <terrafrost@php.net>
-
 
44
 * @access  public
41
 */
45
 */
42
class TripleDES extends DES
46
class TripleDES extends DES
43
{
47
{
44
    /**
48
    /**
45
     * Encrypt / decrypt using inner chaining
49
     * Encrypt / decrypt using inner chaining
Line 58... Line 62...
58
    /**
62
    /**
59
     * Key Length (in bytes)
63
     * Key Length (in bytes)
60
     *
64
     *
61
     * @see \phpseclib3\Crypt\TripleDES::setKeyLength()
65
     * @see \phpseclib3\Crypt\TripleDES::setKeyLength()
62
     * @var int
66
     * @var int
-
 
67
     * @access private
63
     */
68
     */
64
    protected $key_length = 24;
69
    protected $key_length = 24;
65
 
70
 
66
    /**
71
    /**
67
     * The mcrypt specific name of the cipher
72
     * The mcrypt specific name of the cipher
68
     *
73
     *
69
     * @see \phpseclib3\Crypt\DES::cipher_name_mcrypt
74
     * @see \phpseclib3\Crypt\DES::cipher_name_mcrypt
70
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
75
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
71
     * @var string
76
     * @var string
-
 
77
     * @access private
72
     */
78
     */
73
    protected $cipher_name_mcrypt = 'tripledes';
79
    protected $cipher_name_mcrypt = 'tripledes';
74
 
80
 
75
    /**
81
    /**
76
     * Optimizing value while CFB-encrypting
82
     * Optimizing value while CFB-encrypting
77
     *
83
     *
78
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
84
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
79
     * @var int
85
     * @var int
-
 
86
     * @access private
80
     */
87
     */
81
    protected $cfb_init_len = 750;
88
    protected $cfb_init_len = 750;
82
 
89
 
83
    /**
90
    /**
84
     * max possible size of $key
91
     * max possible size of $key
85
     *
92
     *
86
     * @see self::setKey()
93
     * @see self::setKey()
87
     * @see \phpseclib3\Crypt\DES::setKey()
94
     * @see \phpseclib3\Crypt\DES::setKey()
88
     * @var string
95
     * @var string
-
 
96
     * @access private
89
     */
97
     */
90
    protected $key_length_max = 24;
98
    protected $key_length_max = 24;
91
 
99
 
92
    /**
100
    /**
93
     * Internal flag whether using self::MODE_3CBC or not
101
     * Internal flag whether using self::MODE_3CBC or not
94
     *
102
     *
95
     * @var bool
103
     * @var bool
-
 
104
     * @access private
96
     */
105
     */
97
    private $mode_3cbc;
106
    private $mode_3cbc;
98
 
107
 
99
    /**
108
    /**
100
     * The \phpseclib3\Crypt\DES objects
109
     * The \phpseclib3\Crypt\DES objects
101
     *
110
     *
102
     * Used only if $mode_3cbc === true
111
     * Used only if $mode_3cbc === true
103
     *
112
     *
104
     * @var array
113
     * @var array
-
 
114
     * @access private
105
     */
115
     */
106
    private $des;
116
    private $des;
107
 
117
 
108
    /**
118
    /**
109
     * Default Constructor.
119
     * Default Constructor.
Line 127... Line 137...
127
     * - cbc3 (same as cbc)
137
     * - cbc3 (same as cbc)
128
     *
138
     *
129
     * @see \phpseclib3\Crypt\DES::__construct()
139
     * @see \phpseclib3\Crypt\DES::__construct()
130
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
140
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
131
     * @param string $mode
141
     * @param string $mode
-
 
142
     * @access public
132
     */
143
     */
133
    public function __construct($mode)
144
    public function __construct($mode)
134
    {
145
    {
135
        switch (strtolower($mode)) {
146
        switch (strtolower($mode)) {
136
            // In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC
147
            // In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC
Line 169... Line 180...
169
     *
180
     *
170
     * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
181
     * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
171
     *
182
     *
172
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
183
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
173
     * @param int $engine
184
     * @param int $engine
-
 
185
     * @access protected
174
     * @return bool
186
     * @return bool
175
     */
187
     */
176
    protected function isValidEngineHelper($engine)
188
    protected function isValidEngineHelper($engine)
177
    {
189
    {
178
        if ($engine == self::ENGINE_OPENSSL) {
190
        if ($engine == self::ENGINE_OPENSSL) {
Line 188... Line 200...
188
     * Sets the initialization vector.
200
     * Sets the initialization vector.
189
     *
201
     *
190
     * SetIV is not required when \phpseclib3\Crypt\Common\SymmetricKey::MODE_ECB is being used.
202
     * SetIV is not required when \phpseclib3\Crypt\Common\SymmetricKey::MODE_ECB is being used.
191
     *
203
     *
192
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setIV()
204
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setIV()
-
 
205
     * @access public
193
     * @param string $iv
206
     * @param string $iv
194
     */
207
     */
195
    public function setIV($iv)
208
    public function setIV($iv)
196
    {
209
    {
197
        parent::setIV($iv);
210
        parent::setIV($iv);
Line 208... Line 221...
208
     * Valid key lengths are 128 and 192 bits.
221
     * Valid key lengths are 128 and 192 bits.
209
     *
222
     *
210
     * If you want to use a 64-bit key use DES.php
223
     * If you want to use a 64-bit key use DES.php
211
     *
224
     *
212
     * @see \phpseclib3\Crypt\Common\SymmetricKey:setKeyLength()
225
     * @see \phpseclib3\Crypt\Common\SymmetricKey:setKeyLength()
-
 
226
     * @access public
213
     * @throws \LengthException if the key length is invalid
227
     * @throws \LengthException if the key length is invalid
214
     * @param int $length
228
     * @param int $length
215
     */
229
     */
216
    public function setKeyLength($length)
230
    public function setKeyLength($length)
217
    {
231
    {
Line 231... Line 245...
231
     *
245
     *
232
     * Triple DES can use 128-bit (eg. strlen($key) == 16) or 192-bit (eg. strlen($key) == 24) keys.
246
     * Triple DES can use 128-bit (eg. strlen($key) == 16) or 192-bit (eg. strlen($key) == 24) keys.
233
     *
247
     *
234
     * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
248
     * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
235
     *
249
     *
-
 
250
     * @access public
236
     * @see \phpseclib3\Crypt\DES::setKey()
251
     * @see \phpseclib3\Crypt\DES::setKey()
237
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey()
252
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey()
238
     * @throws \LengthException if the key length is invalid
253
     * @throws \LengthException if the key length is invalid
239
     * @param string $key
254
     * @param string $key
240
     */
255
     */
Line 269... Line 284...
269
 
284
 
270
    /**
285
    /**
271
     * Encrypts a message.
286
     * Encrypts a message.
272
     *
287
     *
273
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
288
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
-
 
289
     * @access public
274
     * @param string $plaintext
290
     * @param string $plaintext
275
     * @return string $cipertext
291
     * @return string $cipertext
276
     */
292
     */
277
    public function encrypt($plaintext)
293
    public function encrypt($plaintext)
278
    {
294
    {
Line 295... Line 311...
295
 
311
 
296
    /**
312
    /**
297
     * Decrypts a message.
313
     * Decrypts a message.
298
     *
314
     *
299
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
315
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
-
 
316
     * @access public
300
     * @param string $ciphertext
317
     * @param string $ciphertext
301
     * @return string $plaintext
318
     * @return string $plaintext
302
     */
319
     */
303
    public function decrypt($ciphertext)
320
    public function decrypt($ciphertext)
304
    {
321
    {
Line 351... Line 368...
351
     * continuous buffers not be used.  They do offer better security and are, in fact, sometimes required (SSH uses them),
368
     * continuous buffers not be used.  They do offer better security and are, in fact, sometimes required (SSH uses them),
352
     * however, they are also less intuitive and more likely to cause you problems.
369
     * however, they are also less intuitive and more likely to cause you problems.
353
     *
370
     *
354
     * @see \phpseclib3\Crypt\Common\SymmetricKey::enableContinuousBuffer()
371
     * @see \phpseclib3\Crypt\Common\SymmetricKey::enableContinuousBuffer()
355
     * @see self::disableContinuousBuffer()
372
     * @see self::disableContinuousBuffer()
-
 
373
     * @access public
356
     */
374
     */
357
    public function enableContinuousBuffer()
375
    public function enableContinuousBuffer()
358
    {
376
    {
359
        parent::enableContinuousBuffer();
377
        parent::enableContinuousBuffer();
360
        if ($this->mode_3cbc) {
378
        if ($this->mode_3cbc) {
Line 369... Line 387...
369
     *
387
     *
370
     * The default behavior.
388
     * The default behavior.
371
     *
389
     *
372
     * @see \phpseclib3\Crypt\Common\SymmetricKey::disableContinuousBuffer()
390
     * @see \phpseclib3\Crypt\Common\SymmetricKey::disableContinuousBuffer()
373
     * @see self::enableContinuousBuffer()
391
     * @see self::enableContinuousBuffer()
-
 
392
     * @access public
374
     */
393
     */
375
    public function disableContinuousBuffer()
394
    public function disableContinuousBuffer()
376
    {
395
    {
377
        parent::disableContinuousBuffer();
396
        parent::disableContinuousBuffer();
378
        if ($this->mode_3cbc) {
397
        if ($this->mode_3cbc) {
Line 385... Line 404...
385
    /**
404
    /**
386
     * Creates the key schedule
405
     * Creates the key schedule
387
     *
406
     *
388
     * @see \phpseclib3\Crypt\DES::setupKey()
407
     * @see \phpseclib3\Crypt\DES::setupKey()
389
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey()
408
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey()
-
 
409
     * @access private
390
     */
410
     */
391
    protected function setupKey()
411
    protected function setupKey()
392
    {
412
    {
393
        switch (true) {
413
        switch (true) {
394
            // if $key <= 64bits we configure our internal pure-php cipher engine
414
            // if $key <= 64bits we configure our internal pure-php cipher engine
Line 420... Line 440...
420
     * Sets the internal crypt engine
440
     * Sets the internal crypt engine
421
     *
441
     *
422
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
442
     * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
423
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setPreferredEngine()
443
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setPreferredEngine()
424
     * @param int $engine
444
     * @param int $engine
-
 
445
     * @access public
425
     */
446
     */
426
    public function setPreferredEngine($engine)
447
    public function setPreferredEngine($engine)
427
    {
448
    {
428
        if ($this->mode_3cbc) {
449
        if ($this->mode_3cbc) {
429
            $this->des[0]->setPreferredEngine($engine);
450
            $this->des[0]->setPreferredEngine($engine);