[ Index ]

PHP Cross Reference of DokuWiki




/vendor/phpseclib/phpseclib/phpseclib/Crypt/ -> TripleDES.php (summary)

Pure-PHP implementation of Triple DES. Uses mcrypt, if available, and an internal implementation, otherwise.  Operates in the EDE3 mode (encrypt-decrypt-encrypt).

Author: Jim Wigginton
Copyright: 2007 Jim Wigginton
License: http://www.opensource.org/licenses/mit-license.html MIT License
Link: http://phpseclib.sourceforge.net
File Size: 436 lines (14 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

TripleDES:: (11 methods):

Class: TripleDES  - X-Ref

Pure-PHP implementation of Triple DES.

__construct($mode)   X-Ref
Default Constructor.

Determines whether or not the mcrypt or OpenSSL extensions should be used.

$mode could be:

- ecb

- cbc

- ctr

- cfb

- ofb

- 3cbc

- cbc3 (same as cbc)

see: \phpseclib3\Crypt\DES::__construct()
see: \phpseclib3\Crypt\Common\SymmetricKey::__construct()
param: string $mode

isValidEngineHelper($engine)   X-Ref
Test for engine validity

This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()

return: bool
see: \phpseclib3\Crypt\Common\SymmetricKey::__construct()
param: int $engine

setIV($iv)   X-Ref
Sets the initialization vector.

SetIV is not required when \phpseclib3\Crypt\Common\SymmetricKey::MODE_ECB is being used.

see: \phpseclib3\Crypt\Common\SymmetricKey::setIV()
param: string $iv

setKeyLength($length)   X-Ref
Sets the key length.

Valid key lengths are 128 and 192 bits.

If you want to use a 64-bit key use DES.php

see: \phpseclib3\Crypt\Common\SymmetricKey:setKeyLength()
param: int $length

setKey($key)   X-Ref
Sets the key.

Triple DES can use 128-bit (eg. strlen($key) == 16) or 192-bit (eg. strlen($key) == 24) keys.

DES also requires that every eighth bit be a parity bit, however, we'll ignore that.

see: \phpseclib3\Crypt\DES::setKey()
see: \phpseclib3\Crypt\Common\SymmetricKey::setKey()
param: string $key

encrypt($plaintext)   X-Ref
Encrypts a message.

return: string $cipertext
see: \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
param: string $plaintext

decrypt($ciphertext)   X-Ref
Decrypts a message.

return: string $plaintext
see: \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
param: string $ciphertext

enableContinuousBuffer()   X-Ref
Treat consecutive "packets" as if they are a continuous buffer.

Say you have a 16-byte plaintext $plaintext.  Using the default behavior, the two following code snippets
will yield different outputs:

echo $des->encrypt(substr($plaintext, 0, 8));
echo $des->encrypt(substr($plaintext, 8, 8));
echo $des->encrypt($plaintext);

The solution is to enable the continuous buffer.  Although this will resolve the above discrepancy, it creates
another, as demonstrated with the following:

$des->encrypt(substr($plaintext, 0, 8));
echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));

With the continuous buffer disabled, these would yield the same output.  With it enabled, they yield different
outputs.  The reason is due to the fact that the initialization vector's change after every encryption /
decryption round when the continuous buffer is enabled.  When it's disabled, they remain constant.

Put another way, when the continuous buffer is enabled, the state of the \phpseclib3\Crypt\DES() object changes after each
encryption / decryption round, whereas otherwise, it'd remain constant.  For this reason, it's recommended that
continuous buffers not be used.  They do offer better security and are, in fact, sometimes required (SSH uses them),
however, they are also less intuitive and more likely to cause you problems.

see: \phpseclib3\Crypt\Common\SymmetricKey::enableContinuousBuffer()
see: self::disableContinuousBuffer()

disableContinuousBuffer()   X-Ref
Treat consecutive packets as if they are a discontinuous buffer.

The default behavior.

see: \phpseclib3\Crypt\Common\SymmetricKey::disableContinuousBuffer()
see: self::enableContinuousBuffer()

setupKey()   X-Ref
Creates the key schedule

see: \phpseclib3\Crypt\DES::setupKey()
see: \phpseclib3\Crypt\Common\SymmetricKey::setupKey()

setPreferredEngine($engine)   X-Ref
Sets the internal crypt engine

see: \phpseclib3\Crypt\Common\SymmetricKey::__construct()
see: \phpseclib3\Crypt\Common\SymmetricKey::setPreferredEngine()
param: int $engine