示例#1
1
 /**
  * Constructor
  *
  * @param  string      $key        Secret encryption key.
  *                                 It's unsafe to store encryption key in memory, so no getter for key exists.
  * @param  string      $cipher     Cipher algorithm (one of the MCRYPT_ciphername constants)
  * @param  string      $mode       Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants)
  * @param  string|bool $initVector Initial vector to fill algorithm blocks.
  *                                 TRUE generates a random initial vector.
  *                                 FALSE fills initial vector with zero bytes to not use it.
  * @throws Magento_Exception
  */
 public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE_ECB, $initVector = false)
 {
     $this->_cipher = $cipher;
     $this->_mode = $mode;
     $this->_handle = mcrypt_module_open($cipher, '', $mode, '');
     try {
         $maxKeySize = mcrypt_enc_get_key_size($this->_handle);
         if (strlen($key) > $maxKeySize) {
             throw new Magento_Exception('Key must not exceed ' . $maxKeySize . ' bytes.');
         }
         $initVectorSize = mcrypt_enc_get_iv_size($this->_handle);
         if (true === $initVector) {
             /* Generate a random vector from human-readable characters */
             $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
             $initVector = '';
             for ($i = 0; $i < $initVectorSize; $i++) {
                 $initVector .= $abc[rand(0, strlen($abc) - 1)];
             }
         } else {
             if (false === $initVector) {
                 /* Set vector to zero bytes to not use it */
                 $initVector = str_repeat("", $initVectorSize);
             } else {
                 if (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
                     throw new Magento_Exception('Init vector must be a string of ' . $initVectorSize . ' bytes.');
                 }
             }
         }
         $this->_initVector = $initVector;
     } catch (Exception $e) {
         mcrypt_module_close($this->_handle);
         throw $e;
     }
     mcrypt_generic_init($this->_handle, $key, $initVector);
 }
示例#2
0
 /**
  * 实例化类库
  *
  * @param string $secret_key 加密的安全码
  *
  * @return void
  */
 public function __construct($secret_key)
 {
     $this->td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
     $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_DEV_RANDOM);
     $this->ks = mcrypt_enc_get_key_size($this->td);
     $this->key = substr(md5($secret_key), 0, $this->ks);
 }
示例#3
0
文件: Crypto.php 项目: hayate/hayate
 private function __construct()
 {
     if (!function_exists('mcrypt_module_open')) {
         throw new Hayate_Exception(sprintf(_('%s: mcrypt extension is missing.'), __CLASS__));
     }
     switch (self::ALGO) {
         case 'aes':
             $algo = MCRYPT_RIJNDAEL_256;
             break;
         case 'tripledes':
             $algo = MCRYPT_TRIPLEDES;
             break;
         case 'blowfish':
             $algo = MCRYPT_BLOWFISH;
             break;
         default:
             throw new Hayate_Exception(sprintf(_('%s is not supported, please use "aes", "tripledes" or "blowfish"'), self::ALGO));
     }
     // initialize mcrypt
     $this->mcrypt = mcrypt_module_open($algo, '', MCRYPT_MODE_CBC, '');
     // calculate IV size
     $this->ivsize = mcrypt_enc_get_iv_size($this->mcrypt);
     // calculate key max key length
     $this->maxKeysize = mcrypt_enc_get_key_size($this->mcrypt);
     $config = Hayate_Config::getInstance();
     if ($config->get('core.secret_key', false)) {
         $this->setKey($config->core->secret_key);
     }
 }
示例#4
0
 public function decrypt($encrypted, $is_id = false)
 {
     static $_map = array();
     if ($is_id) {
         $len = strlen($encrypted);
         $tmp = '';
         for ($i = 0; $i < $len; $i = $i + 2) {
             $tmp = $tmp . chr(hexdec($encrypted[$i] . $encrypted[$i + 1]));
         }
         $encrypted = $tmp;
     } else {
         $encrypted = base64_decode($encrypted);
     }
     $hashkey = md5($encrypted . $this->key);
     if (isset($_map[$hashkey])) {
         return $_map[$hashkey];
     }
     $key = str_pad($this->key, 24, '0');
     $td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', '');
     $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
     $ks = mcrypt_enc_get_key_size($td);
     @mcrypt_generic_init($td, $key, $iv);
     $decrypted = mdecrypt_generic($td, $encrypted);
     mcrypt_generic_deinit($td);
     mcrypt_module_close($td);
     $y = $this->pkcs5_unpad($decrypted);
     if ($is_id) {
         $y = base_convert($y, 36, 10);
     }
     $_map[$hashkey] = $y;
     return $y;
 }
示例#5
0
文件: Posnet.php 项目: hisef/vpos4php
 /**
  * _getComplete
  *
  * method is prepare complete xml data call from callback url page.
  *
  * @param Dahius_VirtualPos_Request $request
  * @return string 
  */
 protected function _getComplete($request)
 {
     $response = new Dahius_VirtualPos_Response();
     $response->createdOn = time();
     $response->createdBy = $this->_name;
     // Check 3D Values
     $merchantPack = $request->threeDResponse["MerchantPacket"];
     $bankPack = $request->threeDResponse["BankPacket"];
     $sign = $request->threeDResponse["Sign"];
     $hash = strtoupper(md5($merchantPack . $bankPack . $this->_parameters->getPath("merchant_key")));
     if (strcmp($hash, $sign) != 0) {
         $response->code = -4;
         $response->message = "Package Not Matched";
         return $response;
     }
     // Get MD Status...
     $block = mcrypt_get_block_size(MCRYPT_TripleDES, MCRYPT_MODE_CBC);
     $tdes = mcrypt_module_open(MCRYPT_TripleDES, '', MCRYPT_MODE_CBC, '');
     $key_size = mcrypt_enc_get_key_size($tdes);
     $merchant_info = $this->_deCrypt($merchantPack, $this->_parameters->getPath("merchant_key"), $block, $tdes, $key_size);
     mcrypt_generic_deinit($tdes);
     mcrypt_module_close($tdes);
     list($mid, $tid, $amount, $instant, $xid, $tp, $tpo, $webURL, $ip, $port, $txStatus, $mdStatus, $errMsg, $transactionTime, $currency) = explode(";", $merchant_info);
     if (!in_array($mdStatus, $this->_parameters->getPath("valid_md_status"))) {
         $response->code = -3;
         $response->message = "mdStatus({$request->threeDResponse["mdStatus"]}) Not Valid";
         return $response;
     }
     $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?>\n                <posnetRequest>\n                    <mid>{$this->_parameters->getPath("mid")}</mid>\n                    <tid>{$this->_parameters->getPath("tid")}</tid>\n                    <username>{$this->_parameters->getPath("username")}</username>\n                    <password>{$this->_parameters->getPath("password")}</password>\n                    <oosTran>\n                        <bank>{$bankPack}</bank>\n                        <wpAmount>0</wpAmount>\n                    </oosTran>\n                </posnetRequest>";
     return "xmldata={$xml}";
 }
示例#6
0
 function ssl_encode($data, $key = '')
 {
     // Use the Encrypt.php function get_key to encode the data.
     $key = $this->get_key($key);
     // Set a random salt
     $salt = substr(md5(mt_rand(), true), 8);
     $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
     $pad = $block - strlen($data) % $block;
     $data = $data . str_repeat(chr($pad), $pad);
     // Setup encryption parameters
     $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
     $key_len = mcrypt_enc_get_key_size($td);
     $iv_len = mcrypt_enc_get_iv_size($td);
     $total_len = $key_len + $iv_len;
     $salted = '';
     $dx = '';
     // Salt the key and iv
     while (strlen($salted) < $total_len) {
         $dx = md5($dx . $key . $salt, true);
         $salted .= $dx;
     }
     $key = substr($salted, 0, $key_len);
     $iv = substr($salted, $key_len, $iv_len);
     mcrypt_generic_init($td, $key, $iv);
     $encrypted_data = mcrypt_generic($td, $data);
     mcrypt_generic_deinit($td);
     mcrypt_module_close($td);
     return chunk_split(base64_encode('Salted__' . $salt . $encrypted_data), 32, "\n");
 }
 /**
  * 对密文进行解密
  * @param  string $encrypt 密文
  * @return string          明文
  */
 public function decrypt($encrypt)
 {
     //BASE64解码
     $encrypt = base64_decode($encrypt);
     //打开加密算法模块
     $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
     //初始化加密算法模块
     mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16));
     //执行解密
     $decrypt = mdecrypt_generic($td, $encrypt);
     //去除PKCS7补位
     $decrypt = self::PKCS7Decode($decrypt, mcrypt_enc_get_key_size($td));
     //关闭加密算法模块
     mcrypt_generic_deinit($td);
     mcrypt_module_close($td);
     if (strlen($decrypt) < 16) {
         throw new \Exception("非法密文字符串!");
     }
     //去除随机字符串
     $decrypt = substr($decrypt, 16);
     //获取网络字节序
     $size = unpack("N", substr($decrypt, 0, 4));
     $size = $size[1];
     //APP_ID
     $appid = substr($decrypt, $size + 4);
     //验证APP_ID
     if ($appid !== $this->appId) {
         throw new \Exception("非法APP_ID!");
     }
     //明文内容
     $text = substr($decrypt, 4, $size);
     return $text;
 }
示例#8
0
 /**
  * Decryption of data
  *
  * @param string      $data	Data to be decrypted
  * @param bool|string $key	Key, if not specified - system key will be used
  *
  * @return bool|mixed
  */
 function decrypt($data, $key = false)
 {
     if (!$this->encrypt_support) {
         return $data;
     }
     if (!is_resource($this->td)) {
         $this->td = mcrypt_module_open(MCRYPT_BLOWFISH, '', 'cbc', '');
         $this->key = mb_substr($this->key, 0, mcrypt_enc_get_key_size($this->td));
         $this->iv = mb_substr(md5($this->iv), 0, mcrypt_enc_get_iv_size($this->td));
     }
     if ($key === false) {
         $key = $this->key;
     } else {
         $key = mb_substr(md5($this->key) . md5($key), 0, mcrypt_enc_get_key_size($this->td));
     }
     mcrypt_generic_init($this->td, $key, $this->iv);
     errors_off();
     $decrypted = @unserialize(mdecrypt_generic($this->td, $data));
     errors_on();
     mcrypt_generic_deinit($this->td);
     if (is_array($decrypted) && $decrypted['key'] == $key) {
         return $decrypted['data'];
     } else {
         return false;
     }
 }
示例#9
0
 public function __construct($key, $algorithm, $mode = 'ecb', $iv = false)
 {
     /* In non-ECB mode, an initialization vector is required. */
     if ($mode != 'ecb' && $iv === false) {
         return false;
     }
     /* Try to open the encryption module. */
     $this->_td = mcrypt_module_open($algorithm, '', $mode, '');
     if ($this->_td === false) {
         return false;
     }
     /* Use UNIX random number generator if available. */
     if (strstr(PHP_OS, 'WIN') !== false) {
         $randomSeed = MCRYPT_RAND;
     } else {
         $randomSeed = MCRYPT_DEV_RANDOM;
     }
     /* If an initialization vector was not specified, create one;
      * otherwise ensure that the specified IV is the proper size.
      */
     if ($iv === false) {
         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->_td), $randomSeed);
     } else {
         $iv = substr($iv, 0, mcrypt_enc_get_iv_size($this->_td));
     }
     /* Trim the key to the maximum allowed key size. */
     $key = substr($key, 0, mcrypt_enc_get_key_size($this->_td));
     /* Initialize the MCrypt library. */
     mcrypt_generic_init($this->_td, $key, $iv);
 }
示例#10
0
 function phpFreaksCrypto($key = 'a843l?nv89rjfd}O(jdnsleken0', $iv = false, $algorithm = 'tripledes', $mode = 'ecb')
 {
     if (extension_loaded('mcrypt') === FALSE) {
         //$prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
         //dl($prefix . 'mcrypt.' . PHP_SHLIB_SUFFIX) or die('The Mcrypt module could not be loaded.');
         die('The Mcrypt module is not loaded and is required.');
     }
     if ($mode != 'ecb' && $iv === false) {
         /*
           the iv must remain the same from encryption to decryption and is usually
           passed into the encrypted string in some form, but not always.
         */
         die('In order to use encryption modes other then ecb, you must specify a unique and consistent initialization vector.');
     }
     // set mcrypt mode and cipher
     $this->td = mcrypt_module_open($algorithm, '', $mode, '');
     // Unix has better pseudo random number generator then mcrypt, so if it is available lets use it!
     //$random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;
     $random_seed = MCRYPT_RAND;
     // if initialization vector set in constructor use it else, generate from random seed
     $iv = $iv === false ? mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), $random_seed) : substr($iv, 0, mcrypt_enc_get_iv_size($this->td));
     // get the expected key size based on mode and cipher
     $expected_key_size = mcrypt_enc_get_key_size($this->td);
     // we dont need to know the real key, we just need to be able to confirm a hashed version
     $key = substr(md5($key), 0, $expected_key_size);
     // initialize mcrypt library with mode/cipher, encryption key, and random initialization vector
     mcrypt_generic_init($this->td, $key, $iv);
 }
示例#11
0
function enkripsi_plain2($algoritma, $mode, $secretkey, $fileplain)
{
    /* Membuka Modul untuk memilih Algoritma & Mode Operasi */
    $td = mcrypt_module_open($algoritma, '', $mode, '');
    /* Inisialisasi IV dan Menentukan panjang kunci yang digunakan*/
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $ks = mcrypt_enc_get_key_size($td);
    /* Menghasilkan Kunci */
    $key = $secretkey;
    //echo "kuncinya : ". $key. "<br>";
    /* Inisialisasi */
    mcrypt_generic_init($td, $key, $iv);
    /* Enkripsi Data, dimana hasil enkripsi harus di encode dengan base64.\
       Hal ini dikarenakan web browser tidak dapat membaca karakter-karakter\
       ASCII dalam bentuk simbol-simbol */
    $buffer = $fileplain;
    $encrypted = mcrypt_generic($td, $buffer);
    $encrypted1 = base64_encode($iv) . ";" . base64_encode($encrypted);
    $encrypted2 = base64_encode($encrypted1);
    $filecipher = $encrypted2;
    /* Menghentikan proses enkripsi dan menutup modul */
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $filecipher;
}
示例#12
0
 public static function decrypt($string, $key = null, $salt = null, $iv = null)
 {
     $config = ConfigManager::getConfig('Crypto', 'AES256')->AuxConfig;
     if ($key === null) {
         $key = $config->key;
     }
     if ($salt === null) {
         $salt = $config->salt;
     }
     if ($iv === null) {
         $iv = $config->iv;
     }
     $td = mcrypt_module_open('rijndael-128', '', MCRYPT_MODE_CBC, '');
     $ks = mcrypt_enc_get_key_size($td);
     $bs = mcrypt_enc_get_block_size($td);
     $iv = substr(hash("sha256", $iv), 0, $bs);
     // Create key
     $key = Crypto::pbkdf2("sha512", $key, $salt, $config->pbkdfRounds, $ks);
     // Initialize encryption module for decryption
     mcrypt_generic_init($td, $key, $iv);
     $decryptedString = "";
     // Decrypt encrypted string
     try {
         if (ctype_xdigit($string)) {
             $decryptedString = trim(mdecrypt_generic($td, pack("H*", $string)));
         }
     } catch (ErrorException $e) {
     }
     // Terminate decryption handle and close module
     mcrypt_generic_deinit($td);
     mcrypt_module_close($td);
     // Show string
     return $decryptedString;
 }
示例#13
0
 /**
  * Initialize mcrypt module
  *
  * @param string $key cipher private key
  * @return Varien_Crypt_Mcrypt
  */
 public function init($key)
 {
     if (!$this->getCipher()) {
         $this->setCipher(MCRYPT_BLOWFISH);
     }
     if (!$this->getMode()) {
         $this->setMode(MCRYPT_MODE_ECB);
     }
     $this->setHandler(mcrypt_module_open($this->getCipher(), '', $this->getMode(), ''));
     if (!$this->getInitVector()) {
         if (MCRYPT_MODE_CBC == $this->getMode()) {
             $this->setInitVector(substr(md5(mcrypt_create_iv(mcrypt_enc_get_iv_size($this->getHandler()), MCRYPT_RAND)), -mcrypt_enc_get_iv_size($this->getHandler())));
         } else {
             $this->setInitVector(mcrypt_create_iv(mcrypt_enc_get_iv_size($this->getHandler()), MCRYPT_RAND));
         }
     }
     $maxKeySize = mcrypt_enc_get_key_size($this->getHandler());
     if (strlen($key) > $maxKeySize) {
         // strlen() intentionally, to count bytes, rather than characters
         $this->setHandler(null);
         throw new Varien_Exception('Maximum key size must be smaller ' . $maxKeySize);
     }
     mcrypt_generic_init($this->getHandler(), $key, $this->getInitVector());
     return $this;
 }
示例#14
0
 function __construct()
 {
     $this->resource = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
     $ks = mcrypt_enc_get_key_size($this->resource);
     $this->key = substr(md5($this->key), 0, $ks);
     $ivsize = mcrypt_enc_get_iv_size($this->resource);
     $this->iv = substr(md5($this->key), 0, $ivsize);
 }
 public function __construct($algorithm, $mode)
 {
     if (!($this->crResource = mcrypt_module_open($algorithm, null, $mode, null))) {
         throw new WrongStateException('Mcrypt Module did not open.');
     }
     $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->crResource), MCRYPT_DEV_URANDOM);
     $this->keySize = mcrypt_enc_get_key_size($this->crResource);
 }
 private function checkKey()
 {
     if (strlen($this->key) == 0) {
         throw new \InvalidArgumentException('The key length must be > 0');
     } elseif (strlen($this->key) > ($keyMaxLength = mcrypt_enc_get_key_size($this->module))) {
         throw new \InvalidArgumentException('The key length must be <= ' . $keyMaxLength . ' for the choosen algorithm (' . $this->algorithm . ')');
     }
 }
示例#17
0
 /**
  * @param string  $key     Human readable key
  * @param string  $cipher  Cipher/algorithm
  * @param string  $mode    Crypt mode
  */
 public function __construct($key = 'top secret key', $cipher = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_ECB)
 {
     $this->module = mcrypt_module_open($cipher, '', $mode, '');
     $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->module), MCRYPT_DEV_RANDOM);
     $keySize = mcrypt_enc_get_key_size($this->module);
     // todo improve to hex
     $this->key = substr(md5($key), 0, $keySize);
 }
示例#18
0
 function __construct()
 {
     $this->td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
     $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_DEV_RANDOM);
     $this->ks = mcrypt_enc_get_key_size($this->td);
     //$this->key = substr(md5('very secret key'), 0, $this->ks);
     $this->key = substr(sha1('very secret key'), 0, $this->ks);
 }
示例#19
0
文件: Crypto.php 项目: z0x/phpSec
 /**
  * Encrypt data returning a JSON encoded array safe for storage in a database
  * or file. The array has the following structure before it is encoded:
  * array(
  *   'cdata' => 'Encrypted data, Base 64 encoded',
  *   'iv'    => 'Base64 encoded IV',
  *   'algo'  => 'Algorythm used',
  *   'mode'  => 'Mode used',
  *   'mac'   => 'Message Authentication Code'
  * )
  *
  * @param mixed $data
  *   Data to encrypt.
  *
  * @param string $key
  *   Key to encrypt data with.
  *
  * @return string
  *   Serialized array containing the encrypted data along with some meta data.
  */
 public function encrypt($data, $key)
 {
     /* Make sure both algorithm and mode are either block or non-block. */
     $isBlockCipher = mcrypt_module_is_block_algorithm($this->_algo);
     $isBlockMode = mcrypt_module_is_block_algorithm_mode($this->_mode);
     if ($isBlockCipher !== $isBlockMode) {
         throw new \phpSec\Exception\InvalidAlgorithmParameterException('You can not mix block and non-block ciphers and modes');
         return false;
     }
     $td = mcrypt_module_open($this->_algo, '', $this->_mode, '');
     /* Check key size. */
     $keySize = strlen($key);
     $keySizes = mcrypt_enc_get_supported_key_sizes($td);
     if (count($keySizes) > 0) {
         /* Encryption method requires a specific key size. */
         if (!in_array($keySize, $keySizes)) {
             throw new \phpSec\Exception\InvalidKeySpecException('Key is out of range. Should be one of: ' . implode(', ', $keySizes));
             return false;
         }
     } else {
         /* No specific size is needed. */
         if ($keySize == 0 || $keySize > mcrypt_enc_get_key_size($td)) {
             throw new \phpSec\Exception\InvalidKeySpecException('Key is out of range. Should be between  1 and ' . mcrypt_enc_get_key_size($td) . ' bytes.');
             return false;
         }
     }
     /* Using PBKDF with constant salts dedicated to each purpose 
      * can securely derivce two keys from one */
     $key1 = $this->pbkdf2($key, "encrypt", 1, $keySize);
     $key2 = $this->pbkdf2($key, "HMAC", 1, $keySize);
     /* Create IV. */
     $rnd = $this->psl['crypt/rand'];
     $iv = $rnd->bytes(mcrypt_enc_get_iv_size($td));
     /* Init mcrypt. */
     mcrypt_generic_init($td, $key1, $iv);
     /* Prepeare the array with data. */
     $serializedData = serialize($data);
     /* Enable padding of data if block cipher moode. */
     if (mcrypt_module_is_block_algorithm_mode($this->_mode) === true) {
         $this->_padding = true;
     }
     /* Add padding if enabled. */
     if ($this->_padding === true) {
         $block = mcrypt_enc_get_block_size($td);
         $serializedData = $this->pad($block, $serializedData);
         $encrypted['padding'] = 'PKCS7';
     }
     $encrypted['algo'] = $this->_algo;
     /* Algorithm used to encrypt. */
     $encrypted['mode'] = $this->_mode;
     /* Algorithm mode. */
     $encrypted['iv'] = base64_encode($iv);
     /* Initialization vector, just a bunch of randomness. */
     $encrypted['cdata'] = base64_encode(mcrypt_generic($td, $serializedData));
     /* The encrypted data. */
     $encrypted['mac'] = base64_encode($this->pbkdf2($encrypted['cdata'], $key2, 1, 32));
     return json_encode($encrypted);
 }
示例#20
0
 private function start()
 {
     $this->resource = mcrypt_module_open($this->algorithm, '', $this->mode, '');
     $this->key_size = mcrypt_enc_get_key_size($this->resource);
     $this->key = substr($this->raw_key, 0, $this->key_size);
     $this->iv_size = mcrypt_enc_get_iv_size($this->resource);
     $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
     return $this->initialize();
 }
示例#21
0
 public function __construct($key)
 {
     $td = mcrypt_module_open('des', '', 'ecb', '');
     $key = substr($key, 0, mcrypt_enc_get_key_size($td));
     $iv_size = mcrypt_enc_get_iv_size($td);
     $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
     mcrypt_generic_init($td, $key, $iv);
     $this->td = $td;
 }
示例#22
0
 /**
  * 构造函数
  *
  */
 private function __construct()
 {
     //生成对象
     $this->_object_mode = mcrypt_module_open($this->_algorithm, '', $this->_encryp_mode, '');
     //密钥长度
     $this->_key_size = mcrypt_enc_get_key_size($this->_object_mode);
     //加密密钥
     $this->_key_algorithm = substr(md5($this->_secret_key), 0, $this->_key_size);
 }
示例#23
0
 /**
  * Instantiates a new rijndael-based encryptor with the specified cipher,
  * secret and IV.
  *
  * @param string $cipher one of the MCRYPT_* constants
  * @param string $secret to be used as the key for encryption/decryption
  * @param string $iv a random, but optionally public, value
  */
 public function __construct($cipher, $secret, $iv)
 {
     // Initialize the encryptor module.
     $this->module = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
     // Pad the secret key or IV when needed, or cut them short.
     $this->secret = self::constrainStringSize($secret, mcrypt_enc_get_key_size($this->module));
     $this->iv = self::constrainStringSize($iv, mcrypt_enc_get_iv_size($this->module));
     $this->cipher = $cipher;
 }
示例#24
0
 public function generate_key()
 {
     global $conf, $util;
     $td = mcrypt_module_open($conf->encrypt_algo, '', $conf->encrypt_mode, '');
     $ks = mcrypt_enc_get_key_size($td);
     $key = $util->random_string($ks);
     mcrypt_module_close($td);
     return $key;
 }
示例#25
0
 function generateSharingKey()
 {
     $this->keyBasic = md5(uniqid(rand(), true));
     $maxSizeKey = mcrypt_enc_get_key_size($this->cypherOpen("", ""));
     $this->keyBasic = substr($this->keyBasic, 0, $maxSizeKey);
     $size = strlen($this->keyBasic);
     $this->serverKey = substr($this->keyBasic, 0, $size / 2);
     $this->clientKey = substr($this->keyBasic, $size / 2, $size);
     $this->setKey($this->serverKey . $this->clientKey);
 }
示例#26
0
 public function __construct()
 {
     $td = mcrypt_module_open('des', '', 'ecb', '');
     $key = substr($this->keys, 0, mcrypt_enc_get_key_size($td));
     $iv_size = mcrypt_enc_get_iv_size($td);
     $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
     $this->key = $key;
     $this->iv = $iv;
     $this->td = $td;
 }
function decrypt($encrypted_text, $key)
{
    $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND);
    $key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
    mcrypt_generic_init($cipher, $key, $iv);
    $decrypted = mdecrypt_generic($cipher, base64_decode(urldecode($encrypted_text)));
    mcrypt_generic_deinit($cipher);
    return $decrypted;
}
示例#28
0
 /**
  * init encryption module
  */
 private function init()
 {
     $this->module = mcrypt_module_open($this->algorithm, '', MCRYPT_MODE_ECB, '');
     // Create the IV and determine the keysize length
     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->module), MCRYPT_RAND);
     $ks = mcrypt_enc_get_key_size($this->module);
     /* Create key */
     $key = substr(md5($this->secretKey), 0, $ks);
     /* Intialize */
     mcrypt_generic_init($this->module, $key, $iv);
 }
示例#29
0
 /**
  * Generates a random salt.
  *
  * @return string
  */
 protected function generateSalt()
 {
     $length = mcrypt_enc_get_key_size(mcrypt_module_open($this->cipher, '', $this->mode, ''));
     $validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
     $salt = '';
     $count = strlen($validChars) - 1;
     while ($length--) {
         $salt .= $validChars[mt_rand(0, $count)];
     }
     return $salt;
 }
示例#30
0
 public static function encrypt($data)
 {
     $size = mcrypt_get_block_size('des', 'ecb');
     $data = self::pkcs5_pad($data, $size);
     $mcrypt_module = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'ecb', '');
     $mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt_module), MCRYPT_RAND);
     $key_size = mcrypt_enc_get_key_size($mcrypt_module);
     mcrypt_generic_init($mcrypt_module, self::$key, $mcrypt_iv);
     $encrypted = base64_encode(mcrypt_generic($mcrypt_module, $data));
     mcrypt_module_close($mcrypt_module);
     return $encrypted;
 }