includes/rijndael.php
changeset 602 ecbc8d202058
parent 595 b051eb79b158
child 613 c08670a77871
equal deleted inserted replaced
601:9c1f07ae26d5 602:ecbc8d202058
  1221   function set_encrypt()
  1221   function set_encrypt()
  1222   {
  1222   {
  1223     $i = 0;
  1223     $i = 0;
  1224     $rk = 0;
  1224     $rk = 0;
  1225     
  1225     
  1226     if ( $this->key_state == 'encrypt' )
  1226     if ( $this->key_state === 'encrypt' )
       
  1227     {
  1227       return 0;
  1228       return 0;
       
  1229     }
  1228     
  1230     
  1229     $this->key_state = 'encrypt';
  1231     $this->key_state = 'encrypt';
  1230     
  1232     
  1231     $userkey =& $this->key;
  1233     $userkey =& $this->key;
  1232     $bits =& $this->bits;
  1234     $bits =& $this->bits;
  1338     $i = 0;
  1340     $i = 0;
  1339     $j = 0;
  1341     $j = 0;
  1340     $rk = 0;
  1342     $rk = 0;
  1341     $bits =& $this->bits;
  1343     $bits =& $this->bits;
  1342     
  1344     
  1343     if ( $this->key_state == 'decrypt' )
  1345     if ( $this->key_state === 'decrypt' )
       
  1346     {
  1344       return 0;
  1347       return 0;
       
  1348     }
  1345     
  1349     
  1346     $this->key_state = 'decrypt';
  1350     $this->key_state = 'decrypt';
  1347 
  1351     
  1348     // first, start with an encryption schedule 
  1352     // first, start with an encryption schedule 
  1349     $status = $this->set_encrypt($bits);
  1353     $status = $this->set_encrypt();
       
  1354     
       
  1355     // set the state again because set_encrypt() will change it
       
  1356     $this->key_state = 'decrypt';
  1350     
  1357     
  1351     if ($status < 0) {
  1358     if ($status < 0) {
  1352       librijndael2::trace("AES_set_decrypt_key: AES_set_encrypt_key error");
  1359       librijndael2::trace("AES_set_decrypt_key: AES_set_encrypt_key error");
  1353       return;
  1360       return;
  1354     }
  1361     }
  1456    * Pads a string with nul bytes until it reaches a multiple of 16 bytes.
  1463    * Pads a string with nul bytes until it reaches a multiple of 16 bytes.
  1457    * @param string
  1464    * @param string
  1458    * @return string
  1465    * @return string
  1459    */
  1466    */
  1460   
  1467   
  1461   function pad_string($string)
  1468   protected function pad_string($string)
  1462   {
  1469   {
  1463     while ( strlen($string) % 32 > 0 )
  1470     while ( strlen($string) % 32 > 0 )
  1464     {
  1471     {
  1465       $string .= "\000";
  1472       $string .= "00";
  1466     }
  1473     }
  1467     return $string;
  1474     return $string;
  1468   }
  1475   }
  1469   
  1476   
  1470   /**
  1477   /**
  1534     
  1541     
  1535     $result = '';
  1542     $result = '';
  1536     $plaintext = str_split($this->pad_string($plaintext), 32);
  1543     $plaintext = str_split($this->pad_string($plaintext), 32);
  1537     foreach ( $plaintext as $block )
  1544     foreach ( $plaintext as $block )
  1538     {
  1545     {
  1539       $result .= $aes->AES_cbc_encrypt($block, $okey, $ivec, 'AES_ENCRYPT');
  1546       $block = $aes->AES_cbc_encrypt($block, $okey, $ivec, 'AES_ENCRYPT');
       
  1547       $result .= $block;
  1540     }
  1548     }
  1541     
  1549     
  1542     switch ( $return_format )
  1550     switch ( $return_format )
  1543     {
  1551     {
  1544       case ENC_BINARY:
  1552       case ENC_BINARY:
  1600     }
  1608     }
  1601     
  1609     
  1602     // perform decryption
  1610     // perform decryption
  1603     $result = '';
  1611     $result = '';
  1604     $cryptext_orig = $cryptext;
  1612     $cryptext_orig = $cryptext;
  1605     $cryptext = str_split($cryptext, 32);
  1613     $cryptext = enano_str_split($cryptext, 32);
  1606     foreach ( $cryptext as $block )
  1614     foreach ( $cryptext as $block )
  1607     {
  1615     {
  1608       $result .= $aes->AES_cbc_encrypt($block, $okey, $ivec, 'AES_DECRYPT');
  1616       $block = $aes->AES_cbc_encrypt($block, $okey, $ivec, 'AES_DECRYPT');
       
  1617       $result .= $block;
  1609     }
  1618     }
  1610     
  1619     
  1611     // decode result and trim nul bytes
  1620     // decode result and trim nul bytes
  1612     $result = librijndael2::hex2string($result);
  1621     $result = librijndael2::hex2string($result);
  1613     $result = rtrim($result, "\000");
  1622     $result = rtrim($result, "\000");
  1637       $instance = new $class();
  1646       $instance = new $class();
  1638     }
  1647     }
  1639     return $instance;
  1648     return $instance;
  1640   }
  1649   }
  1641   
  1650   
       
  1651   #
       
  1652   # Utility functions
       
  1653   #
       
  1654   
       
  1655   /**
       
  1656    * Generates a random key suitable for encryption
       
  1657    * @param int $len the length of the key, in bytes
       
  1658    * @return string a BINARY key
       
  1659    */
       
  1660   
       
  1661   function randkey($len = 32)
       
  1662   {
       
  1663     $key = '';
       
  1664     for($i=0;$i<$len;$i++)
       
  1665     {
       
  1666       $key .= chr(mt_rand(0, 255));
       
  1667     }
       
  1668     if ( @file_exists('/dev/urandom') && @is_readable('/dev/urandom') )
       
  1669     {
       
  1670       // Let's use something a little more secure
       
  1671       $ur = @fopen('/dev/urandom', 'r');
       
  1672       if ( !$ur )
       
  1673         return $key;
       
  1674       $ukey = @fread($ur, $len);
       
  1675       fclose($ur);
       
  1676       if ( strlen($ukey) != $len )
       
  1677         return $key;
       
  1678       return $ukey;
       
  1679     }
       
  1680     return $key;
       
  1681   }
       
  1682   
       
  1683   function gen_readymade_key()
       
  1684   {
       
  1685     $key = librijndael2::string2hex($this->randkey(AES_BITS / 8));
       
  1686     return $key;
       
  1687   }
  1642 }
  1688 }
  1643 
  1689 
  1644 function aes_decrypt_cache_store($encrypted, $decrypted, $key)
  1690 function aes_decrypt_cache_store($encrypted, $decrypted, $key)
  1645 {
  1691 {
  1646   $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
  1692   $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';