Exemple #1
0
 /**
  * Decrypt 3DES-encrypted string
  *
  * @param string $cipher encrypted text
  * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key'
  * @param boolean $base64 whether or not input is base64-encoded
  *
  * @return string decrypted text
  */
 public function decrypt($cipher, $key = 'des_key', $base64 = true)
 {
     if (!$cipher) {
         return '';
     }
     $cipher = $base64 ? base64_decode($cipher) : $cipher;
     $ckey = $this->config->get_crypto_key($key);
     if (function_exists('openssl_decrypt')) {
         $method = 'DES-EDE3-CBC';
         $opts = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
         $iv_size = openssl_cipher_iv_length($method);
         $iv = substr($cipher, 0, $iv_size);
         // session corruption? (#1485970)
         if (strlen($iv) < $iv_size) {
             return '';
         }
         $cipher = substr($cipher, $iv_size);
         $clear = openssl_decrypt($cipher, $method, $ckey, $opts, $iv);
     } else {
         if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) {
             $iv_size = mcrypt_enc_get_iv_size($td);
             $iv = substr($cipher, 0, $iv_size);
             // session corruption? (#1485970)
             if (strlen($iv) < $iv_size) {
                 return '';
             }
             $cipher = substr($cipher, $iv_size);
             mcrypt_generic_init($td, $ckey, $iv);
             $clear = mdecrypt_generic($td, $cipher);
             mcrypt_generic_deinit($td);
             mcrypt_module_close($td);
         } else {
             @(include_once 'des.inc');
             if (function_exists('des')) {
                 $des_iv_size = 8;
                 $iv = substr($cipher, 0, $des_iv_size);
                 $cipher = substr($cipher, $des_iv_size);
                 $clear = des($ckey, $cipher, 0, 1, $iv);
             } else {
                 self::raise_error(array('code' => 500, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Could not perform decryption; make sure OpenSSL or Mcrypt or lib/des.inc is available"), true, true);
             }
         }
     }
     /*-
      * Trim PHP's padding and the canary byte; see note in
      * rcube::encrypt() and http://php.net/mcrypt_generic#68082
      */
     $clear = substr(rtrim($clear, ""), 0, -1);
     return $clear;
 }
Exemple #2
0
 /**
  * Decrypt a string
  *
  * @param string  $cipher Encrypted text
  * @param string  $key    Encryption key to retrieve from the configuration, defaults to 'des_key'
  * @param boolean $base64 Whether or not input is base64-encoded
  *
  * @return string Decrypted text
  */
 public function decrypt($cipher, $key = 'des_key', $base64 = true)
 {
     if (!$cipher) {
         return '';
     }
     $cipher = $base64 ? base64_decode($cipher) : $cipher;
     $ckey = $this->config->get_crypto_key($key);
     $method = $this->config->get_crypto_method();
     $opts = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
     $iv_size = openssl_cipher_iv_length($method);
     $iv = substr($cipher, 0, $iv_size);
     // session corruption? (#1485970)
     if (strlen($iv) < $iv_size) {
         return '';
     }
     $cipher = substr($cipher, $iv_size);
     $clear = openssl_decrypt($cipher, $method, $ckey, $opts, $iv);
     return $clear;
 }
Exemple #3
0
 /**
  * Decrypt 3DES-encrypted string
  *
  * @param string  $cipher Encrypted text
  * @param string  $key    Encryption key to retrieve from the configuration, defaults to 'des_key'
  * @param boolean $base64 Whether or not input is base64-encoded
  *
  * @return string decrypted text
  */
 public function decrypt($cipher, $key = 'des_key', $base64 = true)
 {
     if (!$cipher) {
         return '';
     }
     $cipher = $base64 ? base64_decode($cipher) : $cipher;
     $ckey = $this->config->get_crypto_key($key);
     $method = 'DES-EDE3-CBC';
     $opts = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
     $iv_size = openssl_cipher_iv_length($method);
     $iv = substr($cipher, 0, $iv_size);
     // session corruption? (#1485970)
     if (strlen($iv) < $iv_size) {
         return '';
     }
     $cipher = substr($cipher, $iv_size);
     $clear = openssl_decrypt($cipher, $method, $ckey, $opts, $iv);
     // Trim PHP's padding and the canary byte; see note in
     // rcube::encrypt() and http://php.net/mcrypt_generic#68082
     $clear = substr(rtrim($clear, ""), 0, -1);
     return $clear;
 }
Exemple #4
0
 /**
  * Decrypt 3DES-encrypted string
  *
  * @param string $cipher encrypted text
  * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key'
  * @param boolean $base64 whether or not input is base64-encoded
  *
  * @return string decrypted text
  */
 public function decrypt($cipher, $key = 'des_key', $base64 = true)
 {
     if (!$cipher) {
         return '';
     }
     $cipher = $base64 ? base64_decode($cipher) : $cipher;
     if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) {
         $iv_size = mcrypt_enc_get_iv_size($td);
         $iv = substr($cipher, 0, $iv_size);
         // session corruption? (#1485970)
         if (strlen($iv) < $iv_size) {
             return '';
         }
         $cipher = substr($cipher, $iv_size);
         mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv);
         $clear = mdecrypt_generic($td, $cipher);
         mcrypt_generic_deinit($td);
         mcrypt_module_close($td);
     } else {
         @(include_once 'lib/des.inc');
         if (function_exists('des')) {
             $des_iv_size = 8;
             $iv = substr($cipher, 0, $des_iv_size);
             $cipher = substr($cipher, $des_iv_size);
             $clear = des($this->config->get_crypto_key($key), $cipher, 0, 1, $iv);
         } else {
             raise_error(array('code' => 500, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Could not perform decryption; make sure Mcrypt is installed or lib/des.inc is available"), true, true);
         }
     }
     /*-
      * Trim PHP's padding and the canary byte; see note in
      * rcmail::encrypt() and http://php.net/mcrypt_generic#68082
      */
     $clear = substr(rtrim($clear, ""), 0, -1);
     return $clear;
 }