コード例 #1
0
ファイル: Twofish.php プロジェクト: bodun/jorani
 /**
  * Sets the key.
  *
  * Keys can be of any length. Twofish, itself, requires the use of a key that's 128, 192 or 256-bits long.
  * If the key is less than 256-bits we round the length up to the closest valid key length,
  * padding $key with null bytes. If the key is more than 256-bits, we trim the excess bits.
  *
  * If the key is not explicitly set, it'll be assumed a 128 bits key to be all null bytes.
  *
  * @access public
  * @see \phpseclib\Crypt\Base::setKey()
  * @param String $key
  */
 function setKey($key)
 {
     $keylength = strlen($key);
     switch (true) {
         case $keylength <= 16:
             $key = str_pad($key, 16, "");
             break;
         case $keylength <= 24:
             $key = str_pad($key, 24, "");
             break;
         case $keylength < 32:
             $key = str_pad($key, 32, "");
             break;
         case $keylength > 32:
             $key = substr($key, 0, 32);
     }
     parent::setKey($key);
 }
コード例 #2
0
ファイル: RC2.php プロジェクト: bengitiger/phpseclib
 /**
  * Sets the key.
  *
  * Keys can be of any length. RC2, itself, uses 1 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\Base::setKey()
  * @access public
  * @param string $key
  * @param int $t1 optional Effective key length in bits.
  */
 function setKey($key, $t1 = 0)
 {
     $this->orig_key = $key;
     if ($t1 <= 0) {
         $t1 = $this->default_key_length;
     } elseif ($t1 > 1024) {
         $t1 = 1024;
     }
     $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));
 }
コード例 #3
0
ファイル: RC4.php プロジェクト: vernonlacerda/jorani
 /**
  * Sets the key.
  *
  * Keys can be between 1 and 256 bytes long.  If they are longer then 256 bytes, the first 256 bytes will
  * be used.  If no key is explicitly set, it'll be assumed to be a single null byte.
  *
  * @access public
  * @see \phpseclib\Crypt\Base::setKey()
  * @param String $key
  */
 function setKey($key)
 {
     parent::setKey(substr($key, 0, 256));
 }
コード例 #4
0
ファイル: Rijndael.php プロジェクト: zeus911/phpseclib
 /**
 * Default Constructor.
 *
 * 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
 *
 * If not explictly set, \phpseclib\Crypt\Base::MODE_CBC will be used.
 *
 * @see \phpseclib\Crypt\Base::Crypt_Base()
 * @param optional Integer $mode
 * @access public
 
     /**
 * Sets the key.
 *
 * Keys can be of any length.  Rijndael, itself, requires the use of a key that's between 128-bits and 256-bits long and
 * whose length is a multiple of 32.  If the key is less than 256-bits and the key length isn't set, we round the length
 * up to the closest valid key length, padding $key with null bytes.  If the key is more than 256-bits, we trim the
 * excess bits.
 *
 * If the key is not explicitly set, it'll be assumed to be all null bytes.
 *
 * Note: 160/224-bit keys must explicitly set by setKeyLength(), otherwise they will be round/pad up to 192/256 bits.
 *
 * @see \phpseclib\Crypt\Base:setKey()
 * @see setKeyLength()
 * @access public
 * @param String $key
 */
 function setKey($key)
 {
     if (!$this->explicit_key_length) {
         $length = strlen($key);
         switch (true) {
             case $length <= 16:
                 $this->key_size = 16;
                 break;
             case $length <= 20:
                 $this->key_size = 20;
                 break;
             case $length <= 24:
                 $this->key_size = 24;
                 break;
             case $length <= 28:
                 $this->key_size = 28;
                 break;
             default:
                 $this->key_size = 32;
         }
     }
     parent::setKey($key);
 }
コード例 #5
0
ファイル: Blowfish.php プロジェクト: hhgr/hhgolf
 /**
  * Sets the key.
  *
  * Keys can be of any length.  Blowfish, itself, requires the use of a key between 32 and max. 448-bits long.
  * If the key is less than 32-bits we NOT fill the key to 32bit but let the key as it is to be compatible
  * with mcrypt because mcrypt act this way with blowfish key's < 32 bits.
  *
  * If the key is more than 448-bits, we trim the excess bits.
  *
  * If the key is not explicitly set, or empty, it'll be assumed a 128 bits key to be all null bytes.
  *
  * @access public
  * @see \phpseclib\Crypt\Base::setKey()
  * @param String $key
  */
 function setKey($key)
 {
     $keylength = strlen($key);
     if (!$keylength) {
         $key = "";
     } elseif ($keylength > 56) {
         $key = substr($key, 0, 56);
     }
     parent::setKey($key);
 }
コード例 #6
0
ファイル: DES.php プロジェクト: kenichiii/phpseclib
 /**
  * Sets the key.
  *
  * Keys can be of any length.  DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
  * only use the first eight, if $key has more then eight characters in it, and pad $key with the
  * null byte if it is less then eight characters long.
  *
  * 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 zero's.
  *
  * @see \phpseclib\Crypt\Base::setKey()
  * @access public
  * @param string $key
  */
 function setKey($key)
 {
     // We check/cut here only up to max length of the key.
     // Key padding to the proper length will be done in _setupKey()
     if (strlen($key) > $this->key_size_max) {
         $key = substr($key, 0, $this->key_size_max);
     }
     // Sets the key
     parent::setKey($key);
 }