[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/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: 460 lines (14 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

TripleDES:: (11 methods):
  __construct()
  isValidEngine()
  setIV()
  setKeyLength()
  setKey()
  encrypt()
  decrypt()
  enableContinuousBuffer()
  disableContinuousBuffer()
  _setupKey()
  setPreferredEngine()


Class: TripleDES  - X-Ref

Pure-PHP implementation of Triple DES.

__construct($mode = Base::MODE_CBC)   X-Ref
Default Constructor.

Determines whether or not the mcrypt extension should be used.

$mode could be:

- \phpseclib\Crypt\Base::MODE_ECB

- \phpseclib\Crypt\Base::MODE_CBC

- \phpseclib\Crypt\Base::MODE_CTR

- \phpseclib\Crypt\Base::MODE_CFB

- \phpseclib\Crypt\Base::MODE_OFB

- \phpseclib\Crypt\TripleDES::MODE_3CBC

If not explicitly set, \phpseclib\Crypt\Base::MODE_CBC will be used.

param: int $mode
see: \phpseclib\Crypt\DES::__construct()
see: \phpseclib\Crypt\Base::__construct()

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

This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine()

param: int $engine
return: bool
see: \phpseclib\Crypt\Base::__construct()

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

SetIV is not required when \phpseclib\Crypt\Base::MODE_ECB is being used.  If not explicitly set, it'll be assumed
to be all zero's.

param: string $iv
see: \phpseclib\Crypt\Base::setIV()

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

Valid key lengths are 64, 128 and 192

param: int $length
see: \phpseclib\Crypt\Base:setKeyLength()

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

Keys can be of any length.  Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
192-bit (eg. strlen($key) == 24) keys.  This function pads and truncates $key as appropriate.

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

If the key is not explicitly set, it'll be assumed to be all null bytes.

param: string $key
see: \phpseclib\Crypt\DES::setKey()
see: \phpseclib\Crypt\Base::setKey()

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

param: string $plaintext
return: string $cipertext
see: \phpseclib\Crypt\Base::encrypt()

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

param: string $ciphertext
return: string $plaintext
see: \phpseclib\Crypt\Base::decrypt()

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:

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

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

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

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 \phpseclib\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: \phpseclib\Crypt\Base::enableContinuousBuffer()
see: self::disableContinuousBuffer()

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

The default behavior.

see: \phpseclib\Crypt\Base::disableContinuousBuffer()
see: self::enableContinuousBuffer()

_setupKey()   X-Ref
Creates the key schedule

see: \phpseclib\Crypt\DES::_setupKey()
see: \phpseclib\Crypt\Base::_setupKey()

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

param: int $engine
return: int
see: \phpseclib\Crypt\Base::__construct()
see: \phpseclib\Crypt\Base::setPreferredEngine()