/**
  * Generate a UUID v4
  *
  * @return  string
  */
 private function uuid_v4()
 {
     $randval = new RandomValue();
     $data = $randval->generate(16);
     $data[6] = chr(ord($data[6]) & 0xf | 0x40);
     // set version to 0100
     $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
     // set bits 6-7 to 10
     return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
 }
Exemple #2
0
 public function encrypt($plainText, $key, $iv = null)
 {
     $iv_size = $this->getBlockSize();
     $key = $this->resizeKey($key, $iv_size);
     $iv = $this->resizeKey($iv, $iv_size);
     if (empty($iv)) {
         $randVal = new RandomValue();
         $iv = $randVal->generate($iv_size);
     }
     $cipherText = mcrypt_encrypt($this->cipherType, $key, $plainText, $this->cipherMode, $iv);
     $cipherText = $iv . $cipherText;
     return $cipherText;
 }
Exemple #3
0
 public function encrypt($plainText, $key, $iv = null)
 {
     $iv_size = $this->getBlockSize();
     $key = $this->resizeKey($key, $iv_size);
     $iv = $this->resizeKey($iv, $iv_size);
     if (empty($iv)) {
         $randVal = new RandomValue();
         $iv = $randVal->generate($iv_size);
     }
     $plainText .= $this->getZeroPadding($plainText, $iv_size);
     $cipherText = openssl_encrypt($plainText, $this->method, $key, $this->openSSLOptions, $iv);
     $cipherText = $iv . $cipherText;
     return $cipherText;
 }
Exemple #4
0
 /**
  * AES encryption in CBC mode. This is the standard mode (the CTR methods
  * actually use Rijndael-128 in CTR mode, which - technically - isn't AES).
  * The data length is tucked as a 32-bit unsigned integer (little endian)
  * after the ciphertext. It supports AES-128 only.
  *
  * @since  3.0.1
  * @author Nicholas K. Dionysopoulos
  *
  * @param   string  $plaintext  The data to encrypt
  * @param   string  $password   Encryption password
  * @param   bool    $legacy     Enable legacy mode (insecure initialization vector)
  *
  * @return  string  The ciphertext
  */
 public function AESEncryptCBC($plaintext, $password, $legacy = false)
 {
     $adapter = $this->getAdapter();
     if (!$adapter->isSupported()) {
         return false;
     }
     // Get the expanded key from the password
     $key = $this->expandKey($password);
     // Create an IV
     $rand = new RandomValue();
     $iv = $rand->generate(16);
     if ($legacy) {
         $iv = $this->createTheWrongIV($password);
     }
     // The ciphertext is the encrypted string...
     $ciphertext = $adapter->encrypt($plaintext, $key, $iv);
     // ...minus the IV which was placed in front
     $ciphertext = substr($ciphertext, 16);
     // ...plus the IV at the end...
     if (!$legacy) {
         $ciphertext .= 'JPIV' . $iv;
     }
     // ...plus the plaintext length.
     $ciphertext .= pack('V', strlen($plaintext));
     return $ciphertext;
 }