phpseclib\Crypt\RC4::setKeyLength PHP Method

setKeyLength() public method

Keys can be between 1 and 256 bytes long.
public setKeyLength ( integer $length )
$length integer
    function setKeyLength($length)
    {
        if ($length < 8 || $length > 2048) {
            throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported');
        }
        $this->key_length = $length >> 3;
        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;
 }