Documentation

TripleDES extends DES
in package

Pure-PHP implementation of Triple DES.

Tags
author

Jim Wigginton terrafrost@php.net

access

public

Table of Contents

DECRYPT  = 1
Contains $keys[self::DECRYPT]
ENCRYPT  = 0
Contains $keys[self::ENCRYPT]
ENGINE_INTERNAL  = 1
Base value for the internal implementation $engine switch
ENGINE_MCRYPT  = 2
Base value for the mcrypt implementation $engine switch
ENGINE_OPENSSL  = 3
Base value for the mcrypt implementation $engine switch
MODE_3CBC  = -2
Encrypt / decrypt using inner chaining
MODE_CBC  = 2
Encrypt / decrypt using the Code Book Chaining mode.
MODE_CBC3  = phpseclibCryptBase::MODE_CBC
Encrypt / decrypt using outer chaining
MODE_CFB  = 3
Encrypt / decrypt using the Cipher Feedback mode.
MODE_CFB8  = 38
Encrypt / decrypt using the Cipher Feedback mode (8bit)
MODE_CTR  = -1
Encrypt / decrypt using the Counter mode.
MODE_ECB  = 1
Encrypt / decrypt using the Electronic Code Book mode.
MODE_OFB  = 4
Encrypt / decrypt using the Output Feedback mode.
MODE_STREAM  = 5
Encrypt / decrypt using streaming mode.
$block_size  : int
The Block Length of the block cipher
$cfb_init_len  : int
Optimizing value while CFB-encrypting
$changed  : bool
Does internal cipher state need to be (re)initialized?
$cipher_name_mcrypt  : string
The mcrypt specific name of the cipher
$cipher_name_openssl  : string
The openssl specific name of the cipher
$cipher_name_openssl_ecb  : string
The openssl specific name of the cipher in ECB mode
$continuousBuffer  : bool
Continuous Buffer status
$debuffer  : array<string|int, mixed>
Decryption buffer for CTR, OFB and CFB modes
$dechanged  : bool
Does the demcrypt resource need to be (re)initialized?
$decryptIV  : string
A "sliding" Initialization Vector
$demcrypt  : resource
mcrypt resource for decryption
$des  : array<string|int, mixed>
The \phpseclib\Crypt\DES objects
$des_rounds  : int
Switch for DES/3DES encryption
$ecb  : resource
mcrypt resource for CFB mode
$enbuffer  : array<string|int, mixed>
Encryption buffer for CTR, OFB and CFB modes
$enchanged  : bool
Does the enmcrypt resource need to be (re)initialized?
$encryptIV  : string
A "sliding" Initialization Vector
$engine  : int
Holds which crypt engine internaly should be use, which will be determined automatically on __construct()
$enmcrypt  : resource
mcrypt resource for encryption
$explicit_key_length  : bool
Has the key length explicitly been set or should it be derived from the key, itself?
$inline_crypt  : callable
The name of the performance-optimized callback function
$invipmap  : array<string|int, mixed>
Inverse IP mapping helper table.
$ipmap  : array<string|int, mixed>
IP mapping helper table.
$iv  : string
The Initialization Vector
$key  : string
The Key
$key_length  : int
Key Length (in bytes)
$key_length_max  : string
max possible size of $key
$keys  : array<string|int, mixed>
The Key Schedule
$mode  : int
The Encryption Mode
$mode_3cbc  : bool
Internal flag whether using self::MODE_3CBC or not
$openssl_emulate_ctr  : bool
If OpenSSL can be used in ECB but not in CTR we can emulate CTR
$openssl_mode_names  : array<string|int, mixed>
The OpenSSL names of the cipher / modes
$openssl_options  : mixed
Determines what options are passed to openssl_encrypt/decrypt
$paddable  : bool
Is the mode one that is paddable?
$padding  : bool
Padding status
$password_default_salt  : string
The default salt used by setPassword()
$preferredEngine  : int
Holds the preferred crypt engine
$sbox1  : array<string|int, mixed>
Pre-permuted S-box1
$sbox2  : array<string|int, mixed>
Pre-permuted S-box2
$sbox3  : array<string|int, mixed>
Pre-permuted S-box3
$sbox4  : array<string|int, mixed>
Pre-permuted S-box4
$sbox5  : array<string|int, mixed>
Pre-permuted S-box5
$sbox6  : array<string|int, mixed>
Pre-permuted S-box6
$sbox7  : array<string|int, mixed>
Pre-permuted S-box7
$sbox8  : array<string|int, mixed>
Pre-permuted S-box8
$shuffle  : array<string|int, mixed>
Shuffle table.
$skip_key_adjustment  : bool
Don't truncate / null pad key
$use_inline_crypt  : mixed
Holds whether performance-optimized $inline_crypt() can/should be used.
$WHIRLPOOL_AVAILABLE  : bool
Whirlpool available flag
__construct()  : mixed
Default Constructor.
_createInlineCryptFunction()  : string
Creates the performance-optimized function for en/decrypt()
_decryptBlock()  : string
Decrypts a block
_encryptBlock()  : string
Encrypts a block
_getLambdaFunctions()  : array<string|int, mixed>
Holds the lambda_functions table (classwide)
_hashInlineCryptFunction()  : string
Generates a digest from $bytes
_increment_str()  : mixed
Increment the current string
_openssl_ctr_process()  : string
OpenSSL CTR Processor
_openssl_ofb_process()  : string
OpenSSL OFB Processor
_openssl_translate_mode()  : int
phpseclib <-> OpenSSL Mode Mapper
_pad()  : string
Pads a string
_processBlock()  : string
Encrypts or decrypts a 64-bit block
_setEngine()  : mixed
Sets the engine as appropriate
_setupInlineCrypt()  : mixed
Setup the performance-optimized function for de/encrypt()
_setupKey()  : mixed
Creates the key schedule
_string_pop()  : string
String Pop
_string_shift()  : string
String Shift
_unpad()  : string
Unpads a string.
decrypt()  : string
Decrypts a message.
disableContinuousBuffer()  : mixed
Treat consecutive packets as if they are a discontinuous buffer.
disablePadding()  : mixed
Do not pad packets.
enableContinuousBuffer()  : mixed
Treat consecutive "packets" as if they are a continuous buffer.
enablePadding()  : mixed
Pad "packets".
encrypt()  : string
Encrypts a message.
getBlockLength()  : int
Returns the current block length in bits
getEngine()  : mixed
Returns the engine currently being utilized
getKeyLength()  : int
Returns the current key length in bits
isValidEngine()  : bool
Test for engine validity
safe_intval()  : int
Convert float to int
safe_intval_inline()  : string
eval()'able string for in-line float to int
setIV()  : mixed
Sets the initialization vector. (optional)
setKey()  : mixed
Sets the key.
setKeyLength()  : mixed
Sets the key length.
setPreferredEngine()  : int
Sets the internal crypt engine

Constants

DECRYPT

Contains $keys[self::DECRYPT]

public mixed DECRYPT = 1

ENCRYPT

Contains $keys[self::ENCRYPT]

public mixed ENCRYPT = 0

ENGINE_INTERNAL

Base value for the internal implementation $engine switch

public mixed ENGINE_INTERNAL = 1

ENGINE_MCRYPT

Base value for the mcrypt implementation $engine switch

public mixed ENGINE_MCRYPT = 2

ENGINE_OPENSSL

Base value for the mcrypt implementation $engine switch

public mixed ENGINE_OPENSSL = 3

MODE_3CBC

Encrypt / decrypt using inner chaining

public mixed MODE_3CBC = -2

Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (self::MODE_CBC3).

MODE_CBC3

Encrypt / decrypt using outer chaining

public mixed MODE_CBC3 = phpseclibCryptBase::MODE_CBC

Outer chaining is used by SSH-2 and when the mode is set to \phpseclib\Crypt\Base::MODE_CBC.

MODE_CFB8

Encrypt / decrypt using the Cipher Feedback mode (8bit)

public mixed MODE_CFB8 = 38

MODE_STREAM

Encrypt / decrypt using streaming mode.

public mixed MODE_STREAM = 5

Properties

$block_size

The Block Length of the block cipher

public int $block_size = 16
Tags
access

private

$cfb_init_len

Optimizing value while CFB-encrypting

public int $cfb_init_len = 750
Tags
see
Base::cfb_init_len
access

private

$changed

Does internal cipher state need to be (re)initialized?

public bool $changed = true
Tags
see
self::setKey()
see
self::setIV()
see
self::disableContinuousBuffer()
access

private

$cipher_name_mcrypt

The mcrypt specific name of the cipher

public string $cipher_name_mcrypt = 'tripledes'
Tags
see
DES::cipher_name_mcrypt
see
Base::cipher_name_mcrypt
access

private

$cipher_name_openssl_ecb

The openssl specific name of the cipher in ECB mode

public string $cipher_name_openssl_ecb

If OpenSSL does not support the mode we're trying to use (CTR) it can still be emulated with ECB mode.

Tags
link
http://www.php.net/openssl-get-cipher-methods
access

private

$continuousBuffer

Continuous Buffer status

public bool $continuousBuffer = false
Tags
see
self::enableContinuousBuffer()
access

private

$debuffer

Decryption buffer for CTR, OFB and CFB modes

public array<string|int, mixed> $debuffer
Tags
see
self::decrypt()
see
self::_clearBuffers()
access

private

$dechanged

Does the demcrypt resource need to be (re)initialized?

public bool $dechanged = true
Tags
see
Twofish::setKey()
see
Twofish::setIV()
access

private

$decryptIV

A "sliding" Initialization Vector

public string $decryptIV
Tags
see
self::enableContinuousBuffer()
see
self::_clearBuffers()
access

private

$demcrypt

mcrypt resource for decryption

public resource $demcrypt

The mcrypt resource can be recreated every time something needs to be created or it can be created just once. Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.

Tags
see
self::decrypt()
access

private

$des

The \phpseclib\Crypt\DES objects

public array<string|int, mixed> $des

Used only if $mode_3cbc === true

Tags
access

private

$des_rounds

Switch for DES/3DES encryption

public int $des_rounds = 1

Used only if $engine == self::ENGINE_INTERNAL

Tags
see
self::_setupKey()
see
self::_processBlock()
access

private

$ecb

mcrypt resource for CFB mode

public resource $ecb

mcrypt's CFB mode, in (and only in) buffered context, is broken, so phpseclib implements the CFB mode by it self, even when the mcrypt php extension is available.

In order to do the CFB-mode work (fast) phpseclib use a separate ECB-mode mcrypt resource.

Tags
link
http://phpseclib.sourceforge.net/cfb-demo.phps
see
self::encrypt()
see
self::decrypt()
see
self::_setupMcrypt()
access

private

$enbuffer

Encryption buffer for CTR, OFB and CFB modes

public array<string|int, mixed> $enbuffer
Tags
see
self::encrypt()
see
self::_clearBuffers()
access

private

$enchanged

Does the enmcrypt resource need to be (re)initialized?

public bool $enchanged = true
Tags
see
Twofish::setKey()
see
Twofish::setIV()
access

private

$encryptIV

A "sliding" Initialization Vector

public string $encryptIV
Tags
see
self::enableContinuousBuffer()
see
self::_clearBuffers()
access

private

$engine

Holds which crypt engine internaly should be use, which will be determined automatically on __construct()

public int $engine

Currently available $engines are:

  • self::ENGINE_OPENSSL (very fast, php-extension: openssl, extension_loaded('openssl') required)
  • self::ENGINE_MCRYPT (fast, php-extension: mcrypt, extension_loaded('mcrypt') required)
  • self::ENGINE_INTERNAL (slower, pure php-engine, no php-extension required)
Tags
see
self::_setEngine()
see
self::encrypt()
see
self::decrypt()
access

private

$enmcrypt

mcrypt resource for encryption

public resource $enmcrypt

The mcrypt resource can be recreated every time something needs to be created or it can be created just once. Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.

Tags
see
self::encrypt()
access

private

$explicit_key_length

Has the key length explicitly been set or should it be derived from the key, itself?

public bool $explicit_key_length = false
Tags
see
self::setKeyLength()
access

private

$inline_crypt

The name of the performance-optimized callback function

public callable $inline_crypt

Used by encrypt() / decrypt() only if $engine == self::ENGINE_INTERNAL

Tags
see
self::encrypt()
see
self::decrypt()
see
self::_setupInlineCrypt()
see
self::$use_inline_crypt
access

private

$invipmap

Inverse IP mapping helper table.

public array<string|int, mixed> $invipmap = array(0x0, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x8, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x4, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0xc, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x2, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0xa, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x6, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 0xe, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x1, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 0x9, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x5, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0xd, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x3, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0xb, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x7, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0xf, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff)

Indexing this table with a byte value reverses the bit order.

Tags
access

private

$ipmap

IP mapping helper table.

public array<string|int, mixed> $ipmap = array(0x0, 0x10, 0x1, 0x11, 0x20, 0x30, 0x21, 0x31, 0x2, 0x12, 0x3, 0x13, 0x22, 0x32, 0x23, 0x33, 0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71, 0x42, 0x52, 0x43, 0x53, 0x62, 0x72, 0x63, 0x73, 0x4, 0x14, 0x5, 0x15, 0x24, 0x34, 0x25, 0x35, 0x6, 0x16, 0x7, 0x17, 0x26, 0x36, 0x27, 0x37, 0x44, 0x54, 0x45, 0x55, 0x64, 0x74, 0x65, 0x75, 0x46, 0x56, 0x47, 0x57, 0x66, 0x76, 0x67, 0x77, 0x80, 0x90, 0x81, 0x91, 0xa0, 0xb0, 0xa1, 0xb1, 0x82, 0x92, 0x83, 0x93, 0xa2, 0xb2, 0xa3, 0xb3, 0xc0, 0xd0, 0xc1, 0xd1, 0xe0, 0xf0, 0xe1, 0xf1, 0xc2, 0xd2, 0xc3, 0xd3, 0xe2, 0xf2, 0xe3, 0xf3, 0x84, 0x94, 0x85, 0x95, 0xa4, 0xb4, 0xa5, 0xb5, 0x86, 0x96, 0x87, 0x97, 0xa6, 0xb6, 0xa7, 0xb7, 0xc4, 0xd4, 0xc5, 0xd5, 0xe4, 0xf4, 0xe5, 0xf5, 0xc6, 0xd6, 0xc7, 0xd7, 0xe6, 0xf6, 0xe7, 0xf7, 0x8, 0x18, 0x9, 0x19, 0x28, 0x38, 0x29, 0x39, 0xa, 0x1a, 0xb, 0x1b, 0x2a, 0x3a, 0x2b, 0x3b, 0x48, 0x58, 0x49, 0x59, 0x68, 0x78, 0x69, 0x79, 0x4a, 0x5a, 0x4b, 0x5b, 0x6a, 0x7a, 0x6b, 0x7b, 0xc, 0x1c, 0xd, 0x1d, 0x2c, 0x3c, 0x2d, 0x3d, 0xe, 0x1e, 0xf, 0x1f, 0x2e, 0x3e, 0x2f, 0x3f, 0x4c, 0x5c, 0x4d, 0x5d, 0x6c, 0x7c, 0x6d, 0x7d, 0x4e, 0x5e, 0x4f, 0x5f, 0x6e, 0x7e, 0x6f, 0x7f, 0x88, 0x98, 0x89, 0x99, 0xa8, 0xb8, 0xa9, 0xb9, 0x8a, 0x9a, 0x8b, 0x9b, 0xaa, 0xba, 0xab, 0xbb, 0xc8, 0xd8, 0xc9, 0xd9, 0xe8, 0xf8, 0xe9, 0xf9, 0xca, 0xda, 0xcb, 0xdb, 0xea, 0xfa, 0xeb, 0xfb, 0x8c, 0x9c, 0x8d, 0x9d, 0xac, 0xbc, 0xad, 0xbd, 0x8e, 0x9e, 0x8f, 0x9f, 0xae, 0xbe, 0xaf, 0xbf, 0xcc, 0xdc, 0xcd, 0xdd, 0xec, 0xfc, 0xed, 0xfd, 0xce, 0xde, 0xcf, 0xdf, 0xee, 0xfe, 0xef, 0xff)

Indexing this table with each source byte performs the initial bit permutation.

Tags
access

private

$iv

The Initialization Vector

public string $iv
Tags
see
self::setIV()
access

private

$key

The Key

public string $key = ""
Tags
see
self::setKey()
access

private

$key_length_max

max possible size of $key

public string $key_length_max = 24
Tags
see
self::setKey()
see
DES::setKey()
access

private

$keys

The Key Schedule

public array<string|int, mixed> $keys
Tags
see
self::_setupKey()
access

private

$mode

The Encryption Mode

public int $mode
Tags
see
self::__construct()
access

private

$mode_3cbc

Internal flag whether using self::MODE_3CBC or not

public bool $mode_3cbc
Tags
access

private

$openssl_emulate_ctr

If OpenSSL can be used in ECB but not in CTR we can emulate CTR

public bool $openssl_emulate_ctr = false
Tags
see
self::_openssl_ctr_process()
access

private

$openssl_mode_names

The OpenSSL names of the cipher / modes

public array<string|int, mixed> $openssl_mode_names = array(self::MODE_ECB => 'des-ecb', self::MODE_CBC => 'des-cbc', self::MODE_CFB => 'des-cfb', self::MODE_OFB => 'des-ofb')
Tags
see
Base::openssl_mode_names
access

private

$openssl_options

Determines what options are passed to openssl_encrypt/decrypt

public mixed $openssl_options
Tags
see
self::isValidEngine()
access

private

$paddable

Is the mode one that is paddable?

public bool $paddable = false
Tags
see
self::__construct()
access

private

$padding

Padding status

public bool $padding = true
Tags
see
self::enablePadding()
access

private

$password_default_salt

The default salt used by setPassword()

public string $password_default_salt = 'phpseclib'
Tags
see
Base::password_default_salt
see
Base::setPassword()
access

private

$preferredEngine

Holds the preferred crypt engine

public int $preferredEngine
Tags
see
self::_setEngine()
see
self::setPreferredEngine()
access

private

$sbox1

Pre-permuted S-box1

public array<string|int, mixed> $sbox1 = array(0x808200, 0x0, 0x8000, 0x808202, 0x808002, 0x8202, 0x2, 0x8000, 0x200, 0x808200, 0x808202, 0x200, 0x800202, 0x808002, 0x800000, 0x2, 0x202, 0x800200, 0x800200, 0x8200, 0x8200, 0x808000, 0x808000, 0x800202, 0x8002, 0x800002, 0x800002, 0x8002, 0x0, 0x202, 0x8202, 0x800000, 0x8000, 0x808202, 0x2, 0x808000, 0x808200, 0x800000, 0x800000, 0x200, 0x808002, 0x8000, 0x8200, 0x800002, 0x200, 0x2, 0x800202, 0x8202, 0x808202, 0x8002, 0x808000, 0x800202, 0x800002, 0x202, 0x8202, 0x808200, 0x202, 0x800200, 0x800200, 0x0, 0x8002, 0x8200, 0x0, 0x808002)

Each box ($sbox1-$sbox8) has been vectorized, then each value pre-permuted using the P table: concatenation can then be replaced by exclusive ORs.

Tags
access

private

$sbox2

Pre-permuted S-box2

public array<string|int, mixed> $sbox2 = array(0x40084010, 0x40004000, 0x4000, 0x84010, 0x80000, 0x10, 0x40080010, 0x40004010, 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x80000, 0x10, 0x40080010, 0x84000, 0x80010, 0x40004010, 0x0, 0x40000000, 0x4000, 0x84010, 0x40080000, 0x80010, 0x40000010, 0x0, 0x84000, 0x4010, 0x40084000, 0x40080000, 0x4010, 0x0, 0x84010, 0x40080010, 0x80000, 0x40004010, 0x40080000, 0x40084000, 0x4000, 0x40080000, 0x40004000, 0x10, 0x40084010, 0x84010, 0x10, 0x4000, 0x40000000, 0x4010, 0x40084000, 0x80000, 0x40000010, 0x80010, 0x40004010, 0x40000010, 0x80010, 0x84000, 0x0, 0x40004000, 0x4010, 0x40000000, 0x40080010, 0x40084010, 0x84000)
Tags
access

private

$sbox3

Pre-permuted S-box3

public array<string|int, mixed> $sbox3 = array(0x104, 0x4010100, 0x0, 0x4010004, 0x4000100, 0x0, 0x10104, 0x4000100, 0x10004, 0x4000004, 0x4000004, 0x10000, 0x4010104, 0x10004, 0x4010000, 0x104, 0x4000000, 0x4, 0x4010100, 0x100, 0x10100, 0x4010000, 0x4010004, 0x10104, 0x4000104, 0x10100, 0x10000, 0x4000104, 0x4, 0x4010104, 0x100, 0x4000000, 0x4010100, 0x4000000, 0x10004, 0x104, 0x10000, 0x4010100, 0x4000100, 0x0, 0x100, 0x10004, 0x4010104, 0x4000100, 0x4000004, 0x100, 0x0, 0x4010004, 0x4000104, 0x10000, 0x4000000, 0x4010104, 0x4, 0x10104, 0x10100, 0x4000004, 0x4010000, 0x4000104, 0x104, 0x4010000, 0x10104, 0x4, 0x4010004, 0x10100)
Tags
access

private

$sbox4

Pre-permuted S-box4

public array<string|int, mixed> $sbox4 = array(0x80401000, 0x80001040, 0x80001040, 0x40, 0x401040, 0x80400040, 0x80400000, 0x80001000, 0x0, 0x401000, 0x401000, 0x80401040, 0x80000040, 0x0, 0x400040, 0x80400000, 0x80000000, 0x1000, 0x400000, 0x80401000, 0x40, 0x400000, 0x80001000, 0x1040, 0x80400040, 0x80000000, 0x1040, 0x400040, 0x1000, 0x401040, 0x80401040, 0x80000040, 0x400040, 0x80400000, 0x401000, 0x80401040, 0x80000040, 0x0, 0x0, 0x401000, 0x1040, 0x400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x40, 0x80401040, 0x80000040, 0x80000000, 0x1000, 0x80400000, 0x80001000, 0x401040, 0x80400040, 0x80001000, 0x1040, 0x400000, 0x80401000, 0x40, 0x400000, 0x1000, 0x401040)
Tags
access

private

$sbox5

Pre-permuted S-box5

public array<string|int, mixed> $sbox5 = array(0x80, 0x1040080, 0x1040000, 0x21000080, 0x40000, 0x80, 0x20000000, 0x1040000, 0x20040080, 0x40000, 0x1000080, 0x20040080, 0x21000080, 0x21040000, 0x40080, 0x20000000, 0x1000000, 0x20040000, 0x20040000, 0x0, 0x20000080, 0x21040080, 0x21040080, 0x1000080, 0x21040000, 0x20000080, 0x0, 0x21000000, 0x1040080, 0x1000000, 0x21000000, 0x40080, 0x40000, 0x21000080, 0x80, 0x1000000, 0x20000000, 0x1040000, 0x21000080, 0x20040080, 0x1000080, 0x20000000, 0x21040000, 0x1040080, 0x20040080, 0x80, 0x1000000, 0x21040000, 0x21040080, 0x40080, 0x21000000, 0x21040080, 0x1040000, 0x0, 0x20040000, 0x21000000, 0x40080, 0x1000080, 0x20000080, 0x40000, 0x0, 0x20040000, 0x1040080, 0x20000080)
Tags
access

private

$sbox6

Pre-permuted S-box6

public array<string|int, mixed> $sbox6 = array(0x10000008, 0x10200000, 0x2000, 0x10202008, 0x10200000, 0x8, 0x10202008, 0x200000, 0x10002000, 0x202008, 0x200000, 0x10000008, 0x200008, 0x10002000, 0x10000000, 0x2008, 0x0, 0x200008, 0x10002008, 0x2000, 0x202000, 0x10002008, 0x8, 0x10200008, 0x10200008, 0x0, 0x202008, 0x10202000, 0x2008, 0x202000, 0x10202000, 0x10000000, 0x10002000, 0x8, 0x10200008, 0x202000, 0x10202008, 0x200000, 0x2008, 0x10000008, 0x200000, 0x10002000, 0x10000000, 0x2008, 0x10000008, 0x10202008, 0x202000, 0x10200000, 0x202008, 0x10202000, 0x0, 0x10200008, 0x8, 0x2000, 0x10200000, 0x202008, 0x2000, 0x200008, 0x10002008, 0x0, 0x10202000, 0x10000000, 0x200008, 0x10002008)
Tags
access

private

$sbox7

Pre-permuted S-box7

public array<string|int, mixed> $sbox7 = array(0x100000, 0x2100001, 0x2000401, 0x0, 0x400, 0x2000401, 0x100401, 0x2100400, 0x2100401, 0x100000, 0x0, 0x2000001, 0x1, 0x2000000, 0x2100001, 0x401, 0x2000400, 0x100401, 0x100001, 0x2000400, 0x2000001, 0x2100000, 0x2100400, 0x100001, 0x2100000, 0x400, 0x401, 0x2100401, 0x100400, 0x1, 0x2000000, 0x100400, 0x2000000, 0x100400, 0x100000, 0x2000401, 0x2000401, 0x2100001, 0x2100001, 0x1, 0x100001, 0x2000000, 0x2000400, 0x100000, 0x2100400, 0x401, 0x100401, 0x2100400, 0x401, 0x2000001, 0x2100401, 0x2100000, 0x100400, 0x0, 0x1, 0x2100401, 0x0, 0x100401, 0x2100000, 0x400, 0x2000001, 0x2000400, 0x400, 0x100001)
Tags
access

private

$sbox8

Pre-permuted S-box8

public array<string|int, mixed> $sbox8 = array(0x8000820, 0x800, 0x20000, 0x8020820, 0x8000000, 0x8000820, 0x20, 0x8000000, 0x20020, 0x8020000, 0x8020820, 0x20800, 0x8020800, 0x20820, 0x800, 0x20, 0x8020000, 0x8000020, 0x8000800, 0x820, 0x20800, 0x20020, 0x8020020, 0x8020800, 0x820, 0x0, 0x0, 0x8020020, 0x8000020, 0x8000800, 0x20820, 0x20000, 0x20820, 0x20000, 0x8020800, 0x800, 0x20, 0x8020020, 0x800, 0x20820, 0x8000800, 0x20, 0x8000020, 0x8020000, 0x8020020, 0x8000000, 0x20000, 0x8000820, 0x0, 0x8020820, 0x20020, 0x8000020, 0x8020000, 0x8000800, 0x8000820, 0x0, 0x8020820, 0x20800, 0x20800, 0x820, 0x820, 0x20020, 0x8000000, 0x8020800)
Tags
access

private

$shuffle

Shuffle table.

public array<string|int, mixed> $shuffle = array("", "�", "�", "��", "�", "��", "��", "���", "�", "��", "��", "���", "��", "���", "���", "����", "�", "��", "��", "���", "��", "���", "���", "����", "��", "���", "���", "����", "���", "����", "����", "�����", "�", "��", "��", "���", "��", "���", "���", "����", "��", "���", "���", "����", "���", "����", "����", "�����", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "�", "��", "��", "���", "��", "���", "���", "����", "��", "���", "���", "����", "���", "����", "����", "�����", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "���", "����", "����", "�����", "����", "�����", "�����", "������", "����", "�����", "�����", "������", "�����", "������", "������", "�������", "�", "��", "��", "���", "��", "���", "���", "����", "��", "���", "���", "����", "���", "����", "����", "�����", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "���", "����", "����", "�����", "����", "�����", "�����", "������", "����", "�����", "�����", "������", "�����", "������", "������", "�������", "��", "���", "���", "����", "���", "����", "����", "�����", "���", "����", "����", "�����", "����", "�����", "�����", "������", "���", "����", "����", "�����", "����", "�����", "�����", "������", "����", "�����", "�����", "������", "�����", "������", "������", "�������", "���", "����", "����", "�����", "����", "�����", "�����", "������", "����", "�����", "�����", "������", "�����", "������", "������", "�������", "����", "�����", "�����", "������", "�����", "������", "������", "�������", "�����", "������", "������", "�������", "������", "�������", "�������", "��������")

For each byte value index, the entry holds an 8-byte string with each byte containing all bits in the same state as the corresponding bit in the index value.

Tags
see
self::_processBlock()
see
self::_setupKey()
access

private

$skip_key_adjustment

Don't truncate / null pad key

public bool $skip_key_adjustment = false
Tags
see
self::_clearBuffers()
access

private

$use_inline_crypt

Holds whether performance-optimized $inline_crypt() can/should be used.

public mixed $use_inline_crypt = true
Tags
see
self::encrypt()
see
self::decrypt()
see
self::inline_crypt
access

private

Methods

__construct()

Default Constructor.

public __construct([int $mode = Base::MODE_CBC ]) : mixed

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.

Parameters
$mode : int = Base::MODE_CBC
Tags
see
DES::__construct()
see
Base::__construct()
access

public

Return values
mixed

_createInlineCryptFunction()

Creates the performance-optimized function for en/decrypt()

public _createInlineCryptFunction(array<string|int, mixed> $cipher_code) : string

Internally for phpseclib developers:

_createInlineCryptFunction():

  • merge the $cipher_code [setup'ed by _setupInlineCrypt()] with the current [$this->]mode of operation code

  • create the $inline function, which called by encrypt() / decrypt() as its replacement to speed up the en/decryption operations.

  • return the name of the created $inline callback function

  • used to speed up en/decryption

The main reason why can speed up things [up to 50%] this way are:

  • using variables more effective then regular. (ie no use of expensive arrays but integers $k_0, $k_1 ... or even, for example, the pure $key[] values hardcoded)

  • avoiding 1000's of function calls of ie _encryptBlock() but inlining the crypt operations. in the mode of operation for() loop.

  • full loop unroll the (sometimes key-dependent) rounds avoiding this way ++$i counters and runtime-if's etc...

The basic code architectur of the generated $inline en/decrypt() lambda function, in pseudo php, is:

+----------------------------------------------------------------------------------------------+ | callback $inline = create_function: | | lambda_function_0001_crypt_ECB($action, $text) | | { | | INSERT PHP CODE OF: | | $cipher_code['init_crypt']; // general init code. | | // ie: $sbox'es declarations used for | | // encrypt and decrypt'ing. | | | | switch ($action) { | | case 'encrypt': | | INSERT PHP CODE OF: | | $cipher_code['init_encrypt']; // encrypt sepcific init code. | | ie: specified $key or $box | | declarations for encrypt'ing. | | | | foreach ($ciphertext) { | | $in = $block_size of $ciphertext; | | | | INSERT PHP CODE OF: | | $cipher_code['encrypt_block']; // encrypt's (string) $in, which is always: | | // strlen($in) == $this->block_size | | // here comes the cipher algorithm in action | | // for encryption. | | // $cipher_code['encrypt_block'] has to | | // encrypt the content of the $in variable | | | | $plaintext .= $in; | | } | | return $plaintext; | | | | case 'decrypt': | | INSERT PHP CODE OF: | | $cipher_code['init_decrypt']; // decrypt sepcific init code | | ie: specified $key or $box | | declarations for decrypt'ing. | | foreach ($plaintext) { | | $in = $block_size of $plaintext; | | | | INSERT PHP CODE OF: | | $cipher_code['decrypt_block']; // decrypt's (string) $in, which is always | | // strlen($in) == $this->block_size | | // here comes the cipher algorithm in action | | // for decryption. | | // $cipher_code['decrypt_block'] has to | | // decrypt the content of the $in variable | | $ciphertext .= $in; | | } | | return $ciphertext; | | } | | } | +----------------------------------------------------------------------------------------------+

See also the \phpseclib\Crypt*::_setupInlineCrypt()'s for productive inline $cipher_code's how they works.

Structure of: $cipher_code = array( 'init_crypt' => (string) '', // optional 'init_encrypt' => (string) '', // optional 'init_decrypt' => (string) '', // optional 'encrypt_block' => (string) '', // required 'decrypt_block' => (string) '' // required );

Parameters
$cipher_code : array<string|int, mixed>
Tags
see
self::_setupInlineCrypt()
see
self::encrypt()
see
self::decrypt()
access

private

Return values
string

(the name of the created callback function)

_decryptBlock()

Decrypts a block

public abstract _decryptBlock(string $in) : string

Note: Must be extended by the child \phpseclib\Crypt* class

Parameters
$in : string
Tags
access

private

Return values
string

_encryptBlock()

Encrypts a block

public abstract _encryptBlock(string $in) : string

Note: Must be extended by the child \phpseclib\Crypt* class

Parameters
$in : string
Tags
access

private

Return values
string

_getLambdaFunctions()

Holds the lambda_functions table (classwide)

public _getLambdaFunctions() : array<string|int, mixed>

Each name of the lambda function, created from _setupInlineCrypt() && _createInlineCryptFunction() is stored, classwide (!), here for reusing.

The string-based index of $function is a classwide unique value representing, at least, the $mode of operation (or more... depends of the optimizing level) for which $mode the lambda function was created.

Tags
access

private

Return values
array<string|int, mixed>

&$functions

_hashInlineCryptFunction()

Generates a digest from $bytes

public _hashInlineCryptFunction( $bytes) : string
Parameters
$bytes :
Tags
see
self::_setupInlineCrypt()
access

private

Return values
string

_increment_str()

Increment the current string

public _increment_str(string &$var) : mixed
Parameters
$var : string
Tags
see
self::decrypt()
see
self::encrypt()
access

private

Return values
mixed

_openssl_ctr_process()

OpenSSL CTR Processor

public _openssl_ctr_process(string $plaintext, string &$encryptIV, array<string|int, mixed> &$buffer) : string

PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream for CTR is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() and Base::decrypt(). Also, OpenSSL doesn't implement CTR for all of it's symmetric ciphers so this function will emulate CTR with ECB when necessary.

Parameters
$plaintext : string
$encryptIV : string
$buffer : array<string|int, mixed>
Tags
see
self::encrypt()
see
self::decrypt()
access

private

Return values
string

_openssl_ofb_process()

OpenSSL OFB Processor

public _openssl_ofb_process(string $plaintext, string &$encryptIV, array<string|int, mixed> &$buffer) : string

PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream for OFB is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() and Base::decrypt().

Parameters
$plaintext : string
$encryptIV : string
$buffer : array<string|int, mixed>
Tags
see
self::encrypt()
see
self::decrypt()
access

private

Return values
string

_openssl_translate_mode()

phpseclib <-> OpenSSL Mode Mapper

public _openssl_translate_mode() : int

May need to be overwritten by classes extending this one in some cases

Tags
access

private

Return values
int

_pad()

Pads a string

public _pad(string $text) : string

Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to chr($this->block_size - (strlen($text) % $this->block_size)

If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless and padding will, hence forth, be enabled.

Parameters
$text : string
Tags
see
self::_unpad()
access

private

Return values
string

_processBlock()

Encrypts or decrypts a 64-bit block

public _processBlock(string $block, int $mode) : string

$mode should be either self::ENCRYPT or self::DECRYPT. See Feistel.png to get a general idea of what this function does.

Parameters
$block : string
$mode : int
Tags
see
self::_encryptBlock()
see
self::_decryptBlock()
access

private

Return values
string

_setEngine()

Sets the engine as appropriate

public _setEngine() : mixed
Tags
see
self::__construct()
access

private

Return values
mixed

_setupInlineCrypt()

Setup the performance-optimized function for de/encrypt()

public _setupInlineCrypt() : mixed
Tags
see
Base::_setupInlineCrypt()
access

private

Return values
mixed

_string_pop()

String Pop

public _string_pop(string &$string[, int $index = 1 ]) : string

Inspired by array_pop

Parameters
$string : string
$index : int = 1
Tags
access

private

Return values
string

_string_shift()

String Shift

public _string_shift(string &$string[, int $index = 1 ]) : string

Inspired by array_shift

Parameters
$string : string
$index : int = 1
Tags
access

private

Return values
string

_unpad()

Unpads a string.

public _unpad(string $text) : string

If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong and false will be returned.

Parameters
$text : string
Tags
see
self::_pad()
access

private

Return values
string

decrypt()

Decrypts a message.

public decrypt(string $ciphertext) : string
Parameters
$ciphertext : string
Tags
see
Base::decrypt()
access

public

Return values
string

$plaintext

disableContinuousBuffer()

Treat consecutive packets as if they are a discontinuous buffer.

public disableContinuousBuffer() : mixed

The default behavior.

Tags
see
Base::disableContinuousBuffer()
see
self::enableContinuousBuffer()
access

public

Return values
mixed

disablePadding()

Do not pad packets.

public disablePadding() : mixed
Tags
see
self::enablePadding()
access

public

Return values
mixed

enableContinuousBuffer()

Treat consecutive "packets" as if they are a continuous buffer.

public enableContinuousBuffer() : mixed

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 \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.

Tags
see
Base::enableContinuousBuffer()
see
self::disableContinuousBuffer()
access

public

Return values
mixed

enablePadding()

Pad "packets".

public enablePadding() : mixed

Block ciphers working by encrypting between their specified [$this->]block_size at a time If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to pad the input so that it is of the proper length.

Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is transmitted separately)

Tags
see
self::disablePadding()
access

public

Return values
mixed

encrypt()

Encrypts a message.

public encrypt(string $plaintext) : string
Parameters
$plaintext : string
Tags
see
Base::encrypt()
access

public

Return values
string

$cipertext

getBlockLength()

Returns the current block length in bits

public getBlockLength() : int
Tags
access

public

Return values
int

getEngine()

Returns the engine currently being utilized

public getEngine() : mixed
Tags
see
self::_setEngine()
access

public

Return values
mixed

getKeyLength()

Returns the current key length in bits

public getKeyLength() : int
Tags
access

public

Return values
int

isValidEngine()

Test for engine validity

public isValidEngine(int $engine) : bool

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

Parameters
$engine : int
Tags
see
Base::__construct()
access

public

Return values
bool

safe_intval()

Convert float to int

public safe_intval(string $x) : int

On ARM CPUs converting floats to ints doesn't always work

Parameters
$x : string
Tags
access

private

Return values
int

safe_intval_inline()

eval()'able string for in-line float to int

public safe_intval_inline() : string
Tags
access

private

Return values
string

setIV()

Sets the initialization vector. (optional)

public setIV(string $iv) : mixed

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.

Parameters
$iv : string
Tags
see
Base::setIV()
access

public

Return values
mixed

setKey()

Sets the key.

public setKey(string $key) : mixed

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.

Parameters
$key : string
Tags
access

public

see
DES::setKey()
see
Base::setKey()
Return values
mixed

setKeyLength()

Sets the key length.

public setKeyLength(int $length) : mixed

Valid key lengths are 64, 128 and 192

Parameters
$length : int
Tags
see

\phpseclib\Crypt\Base:setKeyLength()

access

public

Return values
mixed

Search results