phpseclib\Crypt\TripleDES::setKeyLength PHP Method

setKeyLength() public method

Valid key lengths are 128 and 192 bits. If you want to use a 64-bit key use DES.php
public setKeyLength ( integer $length )
$length integer
    function setKeyLength($length)
    {
        switch ($length) {
            case 128:
            case 192:
                break;
            default:
                throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes 128 or 192 bits are supported');
        }
        parent::setKeyLength($length);
    }

Usage Example

Example #1
0
 /**
  * Returns a SymmetricKey object based on a PBES1 $algo
  *
  * @access public
  * @param string $algo
  */
 static function getPBES1EncryptionObject($algo)
 {
     $algo = preg_match('#^pbeWith(?:MD2|MD5|SHA1|SHA)And(.*?)-CBC$#', $algo, $matches) ? $matches[1] : substr($algo, 13);
     // strlen('pbeWithSHAAnd') == 13
     switch ($algo) {
         case 'DES':
             $cipher = new DES(BlockCipher::MODE_CBC);
             break;
         case 'RC2':
             $cipher = new RC2(BlockCipher::MODE_CBC);
             break;
         case '3-KeyTripleDES':
             $cipher = new TripleDES(BlockCipher::MODE_CBC);
             break;
         case '2-KeyTripleDES':
             $cipher = new TripleDES(BlockCipher::MODE_CBC);
             $cipher->setKeyLength(128);
             break;
         case '128BitRC2':
             $cipher = new RC2(BlockCipher::MODE_CBC);
             $cipher->setKeyLength(128);
             break;
         case '40BitRC2':
             $cipher = new RC2(BlockCipher::MODE_CBC);
             $cipher->setKeyLength(40);
             break;
         case '128BitRC4':
             $cipher = new RC4();
             $cipher->setKeyLength(128);
             break;
         case '40BitRC4':
             $cipher = new RC4();
             $cipher->setKeyLength(40);
             break;
         default:
             throw new UnsupportedAlgorithmException("{$algo} is not a supported algorithm");
     }
     return $cipher;
 }