예제 #1
0
 /**
  * Sets the key.
  *
  * Rijndael supports five different key lengths
  *
  * @see setKeyLength()
  * @access public
  * @param string $key
  * @throws \LengthException if the key length isn't supported
  */
 function setKey($key)
 {
     switch (strlen($key)) {
         case 16:
         case 24:
         case 32:
             break;
         default:
             throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported');
     }
     parent::setKey($key);
 }
예제 #2
0
 /**
  * Sets the key.
  *
  * Keys can be of any length. RC2, itself, uses 8 to 1024 bit keys (eg.
  * strlen($key) <= 128), however, we only use the first 128 bytes if $key
  * has more then 128 bytes in it, and set $key to a single null byte if
  * it is empty.
  *
  * If the key is not explicitly set, it'll be assumed to be a single
  * null byte.
  *
  * @see \phpseclib\Crypt\Common\SymmetricKey::setKey()
  * @access public
  * @param string $key
  * @param int $t1 optional Effective key length in bits.
  * @throws \LengthException if the key length isn't supported
  */
 function setKey($key, $t1 = false)
 {
     $this->orig_key = $key;
     if ($t1 === false) {
         $t1 = $this->default_key_length;
     }
     if ($t1 < 1 || $t1 > 1024) {
         throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 1024 bits, inclusive, are supported');
     }
     $this->current_key_length = $t1;
     // Key byte count should be 1..128.
     $key = strlen($key) ? substr($key, 0, 128) : "";
     $t = strlen($key);
     // The mcrypt RC2 implementation only supports effective key length
     // of 1024 bits. It is however possible to handle effective key
     // lengths in range 1..1024 by expanding the key and applying
     // inverse pitable mapping to the first byte before submitting it
     // to mcrypt.
     // Key expansion.
     $l = array_values(unpack('C*', $key));
     $t8 = $t1 + 7 >> 3;
     $tm = 0xff >> 8 * $t8 - $t1;
     // Expand key.
     $pitable = $this->pitable;
     for ($i = $t; $i < 128; $i++) {
         $l[$i] = $pitable[$l[$i - 1] + $l[$i - $t]];
     }
     $i = 128 - $t8;
     $l[$i] = $pitable[$l[$i] & $tm];
     while ($i--) {
         $l[$i] = $pitable[$l[$i + 1] ^ $l[$i + $t8]];
     }
     // Prepare the key for mcrypt.
     $l[0] = $this->invpitable[$l[0]];
     array_unshift($l, 'C*');
     parent::setKey(call_user_func_array('pack', $l));
 }