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

Defines 1 class

TripleDES:: (11 methods):
  __construct()
  isValidEngineHelper()
  setIV()
  setKeyLength()
  setKey()
  encrypt()
  decrypt()
  enableContinuousBuffer()
  disableContinuousBuffer()
  setupKey()
  setPreferredEngine()


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)

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

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
param: int $engine
see: \phpseclib3\Crypt\Common\SymmetricKey::__construct()

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

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

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

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

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

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.

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

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

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

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

return: string $plaintext
param: string $ciphertext
see: \phpseclib3\Crypt\Common\SymmetricKey::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 \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

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