Esempio n. 1
0
 /**
  * @param $message
  * @param null $name
  * @return array
  */
 public function encrypt($message, $name = null)
 {
     if (isset($manualPadding)) {
         $this->cipher->disablePadding();
         $blockSize = 32;
         $paddedSubject = $message;
         $padSize = $blockSize - strlen($paddedSubject) % $blockSize;
         $paddedSubject = $paddedSubject . str_repeat(chr($padSize), $padSize);
         $missingCharacters = strlen($paddedSubject) % 16;
         if ($missingCharacters > 0) {
             $padding = str_repeat('0', 16 - $missingCharacters - 1) . '-';
             $paddedSubject = $padding . $paddedSubject;
             $paddingLength = strlen($padding);
         } else {
             $paddingLength = 0;
         }
     } else {
         $paddingLength = 0;
         $paddedSubject = $message;
     }
     $encryptedRecords = $this->cipher->encrypt($paddedSubject);
     $encryptedName = '';
     if (!is_null($name)) {
         $encryptedName = $this->cipher->encrypt($name);
     }
     return ['padding_length' => $paddingLength, 'encrypted_message' => base64_encode($encryptedRecords), 'encrypted_name' => base64_encode($encryptedName)];
 }
Esempio n. 2
0
 /**
  * Encrypt a string.
  *
  * @param string $text The content for the encryption.
  *
  * @return string The encrypted string
  */
 public function Encrypt($text)
 {
     // Init the iv.
     $iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
     $this->aes->setIV($iv);
     // Crypt.
     return $iv . "|@|" . $this->aes->encrypt($text);
 }
Esempio n. 3
0
 public static function encrypt($data, $k, $iv)
 {
     $aes = new AES(MCRYPT_MODE_CBC);
     $aes->setKey($k);
     $aes->setIV($iv);
     return $aes->encrypt($data);
 }
Esempio n. 4
0
File: AES.php Progetto: mywin/mywin
 public static function encrypt($data)
 {
     $aes = new AES(AES::MODE_ECB);
     $aes->setKey(self::$key);
     $data = $aes->encrypt($data);
     $data = bin2hex($data);
     return $data;
 }
 /**
  * Process the launchkey option to prepare for storage in the database.  The method will encrypt the data and set
  * the current version so that the option may be programmatically updated in place in the future.
  *
  * @since 1.0.0
  *
  * @param array $input
  *
  * @return array
  */
 public function pre_update_option_filter(array $input)
 {
     $output = $input;
     $output['version'] = static::VERSION;
     if (!empty($input[LaunchKey_WP_Options::OPTION_SECRET_KEY])) {
         $key = md5($input[LaunchKey_WP_Options::OPTION_SECRET_KEY]);
         if (empty($this->cache[$key])) {
             /**
              * Use the rocket key as the IV. If null, use the static value.
              * @link https://docs.launchkey.com/glossary.html#term-iv
              */
             $iv = empty($input[LaunchKey_WP_Options::OPTION_ROCKET_KEY]) ? static::STATIC_IV : $input[LaunchKey_WP_Options::OPTION_ROCKET_KEY];
             $this->crypt_aes->setIV($iv);
             /**
              * Encrypt and Base64 encode the encrypted value and set it as the output value
              * @link https://docs.launchkey.com/glossary.html#term-base64
              */
             $this->cache[$key] = base64_encode($this->crypt_aes->encrypt($input[LaunchKey_WP_Options::OPTION_SECRET_KEY]));
         }
         $output[LaunchKey_WP_Options::OPTION_SECRET_KEY] = $this->cache[$key];
     } else {
         $output[LaunchKey_WP_Options::OPTION_SECRET_KEY] = null;
     }
     if (!empty($input[LaunchKey_WP_Options::OPTION_PRIVATE_KEY])) {
         $key = md5($input[LaunchKey_WP_Options::OPTION_PRIVATE_KEY]);
         if (empty($this->cache[$key])) {
             /**
              * Use the decrypted secret key as the IV. If null, use the static value.
              * @link https://docs.launchkey.com/glossary.html#term-iv
              */
             $iv = empty($input[LaunchKey_WP_Options::OPTION_SECRET_KEY]) ? static::STATIC_IV : $input[LaunchKey_WP_Options::OPTION_SECRET_KEY];
             $this->crypt_aes->setIV($iv);
             /**
              * Encrypt and Base64 encode the encrypted value and set it as the output value
              * @link https://docs.launchkey.com/glossary.html#term-base64
              */
             $this->cache[$key] = base64_encode($this->crypt_aes->encrypt($input[LaunchKey_WP_Options::OPTION_PRIVATE_KEY]));
         }
         $output[LaunchKey_WP_Options::OPTION_PRIVATE_KEY] = $this->cache[$key];
     } else {
         $output[LaunchKey_WP_Options::OPTION_PRIVATE_KEY] = null;
     }
     return $output;
 }
Esempio n. 6
0
 /**
  * Encrypts a value and adds an HMAC (Encrypt-Then-MAC)
  * @param string $plaintext
  * @param string $password Password to encrypt, if not specified the secret from config.php will be taken
  * @return string Authenticated ciphertext
  */
 public function encrypt($plaintext, $password = '')
 {
     if ($password === '') {
         $password = $this->config->getSystemValue('secret');
     }
     $this->cipher->setPassword($password);
     $iv = $this->random->generate($this->ivLength);
     $this->cipher->setIV($iv);
     $ciphertext = bin2hex($this->cipher->encrypt($plaintext));
     $hmac = bin2hex($this->calculateHMAC($ciphertext . $iv, $password));
     return $ciphertext . '|' . $iv . '|' . $hmac;
 }
 /**
  * @return mixed
  */
 public function encrypt($text, $key, $options = array())
 {
     $processed_text = '';
     $disable_base64 = array_key_exists('base64', $options) && $options['base64'] == FALSE;
     $aes = new AES();
     $aes->setKey($key);
     $processed_text = $aes->encrypt($text);
     // If base64 encoding is not disabled.
     if (!$disable_base64) {
         $processed_text = base64_encode($processed_text);
     }
     return trim($processed_text);
 }
 /**
  * Encryption using openssl's AES or phpseclib's AES
  * (phpseclib uses mcrypt when it is available)
  *
  * @param string $data   original data
  * @param string $secret the secret
  *
  * @return string the encrypted result
  */
 public function cookieEncrypt($data, $secret)
 {
     if (self::useOpenSSL()) {
         return openssl_encrypt($data, 'AES-128-CBC', $secret, 0, $this->_cookie_iv);
     } else {
         $cipher = new Crypt\AES(Crypt\Base::MODE_CBC);
         $cipher->setIV($this->_cookie_iv);
         $cipher->setKey($secret);
         return base64_encode($cipher->encrypt($data));
     }
 }
Esempio n. 9
0
 /**
  * Convert a private key to the appropriate format.
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @param \phpseclib\Math\BigInteger $d
  * @param array $primes
  * @param array $exponents
  * @param array $coefficients
  * @param string $password optional
  * @return string
  */
 static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, $primes, $exponents, $coefficients, $password = '')
 {
     if (count($primes) != 2) {
         return false;
     }
     $raw = array('modulus' => $n->toBytes(true), 'publicExponent' => $e->toBytes(true), 'privateExponent' => $d->toBytes(true), 'prime1' => $primes[1]->toBytes(true), 'prime2' => $primes[2]->toBytes(true), 'exponent1' => $exponents[1]->toBytes(true), 'exponent2' => $exponents[2]->toBytes(true), 'coefficient' => $coefficients[2]->toBytes(true));
     $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
     $encryption = !empty($password) || is_string($password) ? 'aes256-cbc' : 'none';
     $key .= $encryption;
     $key .= "\r\nComment: " . self::$comment . "\r\n";
     $public = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus']);
     $source = pack('Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($encryption), $encryption, strlen(self::$comment), self::$comment, strlen($public), $public);
     $public = Base64::encode($public);
     $key .= "Public-Lines: " . (strlen($public) + 63 >> 6) . "\r\n";
     $key .= chunk_split($public, 64);
     $private = pack('Na*Na*Na*Na*', strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'], strlen($raw['coefficient']), $raw['coefficient']);
     if (empty($password) && !is_string($password)) {
         $source .= pack('Na*', strlen($private), $private);
         $hashkey = 'putty-private-key-file-mac-key';
     } else {
         $private .= Random::string(16 - (strlen($private) & 15));
         $source .= pack('Na*', strlen($private), $private);
         $crypto = new AES();
         $crypto->setKey(static::generateSymmetricKey($password, 32));
         $crypto->setIV(str_repeat("", $crypto->getBlockLength() >> 3));
         $crypto->disablePadding();
         $private = $crypto->encrypt($private);
         $hashkey = 'putty-private-key-file-mac-key' . $password;
     }
     $private = Base64::encode($private);
     $key .= 'Private-Lines: ' . (strlen($private) + 63 >> 6) . "\r\n";
     $key .= chunk_split($private, 64);
     $hash = new Hash('sha1');
     $hash->setKey(sha1($hashkey, true));
     $key .= 'Private-MAC: ' . Hex::encode($hash->hash($source)) . "\r\n";
     return $key;
 }
Esempio n. 10
0
 /**
  * Convert a private key to the appropriate format.
  *
  * @access private
  * @see setPrivateKeyFormat()
  * @param String $RSAPrivateKey        	
  * @return String
  */
 function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients)
 {
     $num_primes = count($primes);
     $raw = array('version' => $num_primes == 2 ? chr(0) : chr(1), 'modulus' => $n->toBytes(true), 'publicExponent' => $e->toBytes(true), 'privateExponent' => $d->toBytes(true), 'prime1' => $primes[1]->toBytes(true), 'prime2' => $primes[2]->toBytes(true), 'exponent1' => $exponents[1]->toBytes(true), 'exponent2' => $exponents[2]->toBytes(true), 'coefficient' => $coefficients[2]->toBytes(true));
     // if the format in question does not support multi-prime rsa and multi-prime rsa was used,
     // call _convertPublicKey() instead.
     switch ($this->privateKeyFormat) {
         case CRYPT_RSA_PRIVATE_FORMAT_XML:
             if ($num_primes != 2) {
                 return false;
             }
             return "<RSAKeyValue>\r\n" . '  <Modulus>' . base64_encode($raw['modulus']) . "</Modulus>\r\n" . '  <Exponent>' . base64_encode($raw['publicExponent']) . "</Exponent>\r\n" . '  <P>' . base64_encode($raw['prime1']) . "</P>\r\n" . '  <Q>' . base64_encode($raw['prime2']) . "</Q>\r\n" . '  <DP>' . base64_encode($raw['exponent1']) . "</DP>\r\n" . '  <DQ>' . base64_encode($raw['exponent2']) . "</DQ>\r\n" . '  <InverseQ>' . base64_encode($raw['coefficient']) . "</InverseQ>\r\n" . '  <D>' . base64_encode($raw['privateExponent']) . "</D>\r\n" . '</RSAKeyValue>';
             break;
         case CRYPT_RSA_PRIVATE_FORMAT_PUTTY:
             if ($num_primes != 2) {
                 return false;
             }
             $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
             $encryption = !empty($this->password) || is_string($this->password) ? 'aes256-cbc' : 'none';
             $key .= $encryption;
             $key .= "\r\nComment: " . $this->comment . "\r\n";
             $public = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus']);
             $source = pack('Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($encryption), $encryption, strlen($this->comment), $this->comment, strlen($public), $public);
             $public = base64_encode($public);
             $key .= "Public-Lines: " . (strlen($public) + 63 >> 6) . "\r\n";
             $key .= chunk_split($public, 64);
             $private = pack('Na*Na*Na*Na*', strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'], strlen($raw['coefficient']), $raw['coefficient']);
             if (empty($this->password) && !is_string($this->password)) {
                 $source .= pack('Na*', strlen($private), $private);
                 $hashkey = 'putty-private-key-file-mac-key';
             } else {
                 $private .= crypt_random_string(16 - (strlen($private) & 15));
                 $source .= pack('Na*', strlen($private), $private);
                 $sequence = 0;
                 $symkey = '';
                 while (strlen($symkey) < 32) {
                     $temp = pack('Na*', $sequence++, $this->password);
                     $symkey .= pack('H*', sha1($temp));
                 }
                 $symkey = substr($symkey, 0, 32);
                 $crypto = new AES();
                 $crypto->setKey($symkey);
                 $crypto->disablePadding();
                 $private = $crypto->encrypt($private);
                 $hashkey = 'putty-private-key-file-mac-key' . $this->password;
             }
             $private = base64_encode($private);
             $key .= 'Private-Lines: ' . (strlen($private) + 63 >> 6) . "\r\n";
             $key .= chunk_split($private, 64);
             $hash = new Hash('sha1');
             $hash->setKey(pack('H*', sha1($hashkey)));
             $key .= 'Private-MAC: ' . bin2hex($hash->_hash($source)) . "\r\n";
             return $key;
         default:
             // eg. CRYPT_RSA_PRIVATE_FORMAT_PKCS1
             $components = array();
             foreach ($raw as $name => $value) {
                 $components[$name] = pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($value)), $value);
             }
             $RSAPrivateKey = implode('', $components);
             if ($num_primes > 2) {
                 $OtherPrimeInfos = '';
                 for ($i = 3; $i <= $num_primes; $i++) {
                     // OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
                     //
                     // OtherPrimeInfo ::= SEQUENCE {
                     // prime INTEGER, -- ri
                     // exponent INTEGER, -- di
                     // coefficient INTEGER -- ti
                     // }
                     $OtherPrimeInfo = pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($primes[$i]->toBytes(true))), $primes[$i]->toBytes(true));
                     $OtherPrimeInfo .= pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($exponents[$i]->toBytes(true))), $exponents[$i]->toBytes(true));
                     $OtherPrimeInfo .= pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($coefficients[$i]->toBytes(true))), $coefficients[$i]->toBytes(true));
                     $OtherPrimeInfos .= pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfo)), $OtherPrimeInfo);
                 }
                 $RSAPrivateKey .= pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfos)), $OtherPrimeInfos);
             }
             $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
             if (!empty($this->password) || is_string($this->password)) {
                 $iv = $this->crypt_random_string(8);
                 $symkey = pack('H*', md5($this->password . $iv));
                 // symkey is short for symmetric key
                 $symkey .= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
                 $des = new TripleDES();
                 $des->setKey($symkey);
                 $des->setIV($iv);
                 $iv = strtoupper(bin2hex($iv));
                 $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" . "Proc-Type: 4,ENCRYPTED\r\n" . "DEK-Info: DES-EDE3-CBC,{$iv}\r\n" . "\r\n" . chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) . '-----END RSA PRIVATE KEY-----';
             } else {
                 $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" . chunk_split(base64_encode($RSAPrivateKey), 64) . '-----END RSA PRIVATE KEY-----';
             }
             return $RSAPrivateKey;
     }
 }
Esempio n. 11
0
 /**
  * Encryption using openssl's AES or phpseclib's AES
  * (phpseclib uses mcrypt when it is available)
  *
  * @param string $data   original data
  * @param string $secret the secret
  *
  * @return string the encrypted result
  */
 public function cookieEncrypt($data, $secret)
 {
     $mac_secret = $this->getMACSecret($secret);
     $aes_secret = $this->getAESSecret($secret);
     $iv = $this->createIV();
     if (self::useOpenSSL()) {
         $result = openssl_encrypt($data, 'AES-128-CBC', $secret, 0, $iv);
     } else {
         $cipher = new Crypt\AES(Crypt\Base::MODE_CBC);
         $cipher->setIV($iv);
         $cipher->setKey($aes_secret);
         $result = base64_encode($cipher->encrypt($data));
     }
     $iv = base64_encode($iv);
     return json_encode(array('iv' => $iv, 'mac' => hash_hmac('sha1', $iv . $result, $mac_secret), 'payload' => $result));
 }
Esempio n. 12
0
    /**
     * Convert a private key to the appropriate format.
     *
     * @access private
     * @see self::setPrivateKeyFormat()
     * @param string $RSAPrivateKey
     * @return string
     */
    function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients)
    {
        $signed = $this->privateKeyFormat != self::PRIVATE_FORMAT_XML;
        $num_primes = count($primes);
        $raw = array(
            'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi
            'modulus' => $n->toBytes($signed),
            'publicExponent' => $e->toBytes($signed),
            'privateExponent' => $d->toBytes($signed),
            'prime1' => $primes[1]->toBytes($signed),
            'prime2' => $primes[2]->toBytes($signed),
            'exponent1' => $exponents[1]->toBytes($signed),
            'exponent2' => $exponents[2]->toBytes($signed),
            'coefficient' => $coefficients[2]->toBytes($signed)
        );

        // if the format in question does not support multi-prime rsa and multi-prime rsa was used,
        // call _convertPublicKey() instead.
        switch ($this->privateKeyFormat) {
            case self::PRIVATE_FORMAT_XML:
                if ($num_primes != 2) {
                    return false;
                }
                return "<RSAKeyValue>\r\n" .
                       '  <Modulus>' . base64_encode($raw['modulus']) . "</Modulus>\r\n" .
                       '  <Exponent>' . base64_encode($raw['publicExponent']) . "</Exponent>\r\n" .
                       '  <P>' . base64_encode($raw['prime1']) . "</P>\r\n" .
                       '  <Q>' . base64_encode($raw['prime2']) . "</Q>\r\n" .
                       '  <DP>' . base64_encode($raw['exponent1']) . "</DP>\r\n" .
                       '  <DQ>' . base64_encode($raw['exponent2']) . "</DQ>\r\n" .
                       '  <InverseQ>' . base64_encode($raw['coefficient']) . "</InverseQ>\r\n" .
                       '  <D>' . base64_encode($raw['privateExponent']) . "</D>\r\n" .
                       '</RSAKeyValue>';
                break;
            case self::PRIVATE_FORMAT_PUTTY:
                if ($num_primes != 2) {
                    return false;
                }
                $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
                $encryption = (!empty($this->password) || is_string($this->password)) ? 'aes256-cbc' : 'none';
                $key.= $encryption;
                $key.= "\r\nComment: " . $this->comment . "\r\n";
                $public = pack(
                    'Na*Na*Na*',
                    strlen('ssh-rsa'),
                    'ssh-rsa',
                    strlen($raw['publicExponent']),
                    $raw['publicExponent'],
                    strlen($raw['modulus']),
                    $raw['modulus']
                );
                $source = pack(
                    'Na*Na*Na*Na*',
                    strlen('ssh-rsa'),
                    'ssh-rsa',
                    strlen($encryption),
                    $encryption,
                    strlen($this->comment),
                    $this->comment,
                    strlen($public),
                    $public
                );
                $public = base64_encode($public);
                $key.= "Public-Lines: " . ((strlen($public) + 63) >> 6) . "\r\n";
                $key.= chunk_split($public, 64);
                $private = pack(
                    'Na*Na*Na*Na*',
                    strlen($raw['privateExponent']),
                    $raw['privateExponent'],
                    strlen($raw['prime1']),
                    $raw['prime1'],
                    strlen($raw['prime2']),
                    $raw['prime2'],
                    strlen($raw['coefficient']),
                    $raw['coefficient']
                );
                if (empty($this->password) && !is_string($this->password)) {
                    $source.= pack('Na*', strlen($private), $private);
                    $hashkey = 'putty-private-key-file-mac-key';
                } else {
                    $private.= Random::string(16 - (strlen($private) & 15));
                    $source.= pack('Na*', strlen($private), $private);
                    $sequence = 0;
                    $symkey = '';
                    while (strlen($symkey) < 32) {
                        $temp = pack('Na*', $sequence++, $this->password);
                        $symkey.= pack('H*', sha1($temp));
                    }
                    $symkey = substr($symkey, 0, 32);
                    $crypto = new AES();

                    $crypto->setKey($symkey);
                    $crypto->disablePadding();
                    $private = $crypto->encrypt($private);
                    $hashkey = 'putty-private-key-file-mac-key' . $this->password;
                }

                $private = base64_encode($private);
                $key.= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
                $key.= chunk_split($private, 64);
                $hash = new Hash('sha1');
                $hash->setKey(pack('H*', sha1($hashkey)));
                $key.= 'Private-MAC: ' . bin2hex($hash->hash($source)) . "\r\n";

                return $key;
            default: // eg. self::PRIVATE_FORMAT_PKCS1
                $components = array();
                foreach ($raw as $name => $value) {
                    $components[$name] = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($value)), $value);
                }

                $RSAPrivateKey = implode('', $components);

                if ($num_primes > 2) {
                    $OtherPrimeInfos = '';
                    for ($i = 3; $i <= $num_primes; $i++) {
                        // OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
                        //
                        // OtherPrimeInfo ::= SEQUENCE {
                        //     prime             INTEGER,  -- ri
                        //     exponent          INTEGER,  -- di
                        //     coefficient       INTEGER   -- ti
                        // }
                        $OtherPrimeInfo = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($primes[$i]->toBytes(true))), $primes[$i]->toBytes(true));
                        $OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($exponents[$i]->toBytes(true))), $exponents[$i]->toBytes(true));
                        $OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($coefficients[$i]->toBytes(true))), $coefficients[$i]->toBytes(true));
                        $OtherPrimeInfos.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfo)), $OtherPrimeInfo);
                    }
                    $RSAPrivateKey.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfos)), $OtherPrimeInfos);
                }

                $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);

                if ($this->privateKeyFormat == self::PRIVATE_FORMAT_PKCS8) {
                    $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
                    $RSAPrivateKey = pack(
                        'Ca*a*Ca*a*',
                        self::ASN1_INTEGER,
                        "\01\00",
                        $rsaOID,
                        4,
                        $this->_encodeLength(strlen($RSAPrivateKey)),
                        $RSAPrivateKey
                    );
                    $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
                    if (!empty($this->password) || is_string($this->password)) {
                        $salt = Random::string(8);
                        $iterationCount = 2048;

                        $crypto = new DES();
                        $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
                        $RSAPrivateKey = $crypto->encrypt($RSAPrivateKey);

                        $parameters = pack(
                            'Ca*a*Ca*N',
                            self::ASN1_OCTETSTRING,
                            $this->_encodeLength(strlen($salt)),
                            $salt,
                            self::ASN1_INTEGER,
                            $this->_encodeLength(4),
                            $iterationCount
                        );
                        $pbeWithMD5AndDES_CBC = "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03";

                        $encryptionAlgorithm = pack(
                            'Ca*a*Ca*a*',
                            self::ASN1_OBJECT,
                            $this->_encodeLength(strlen($pbeWithMD5AndDES_CBC)),
                            $pbeWithMD5AndDES_CBC,
                            self::ASN1_SEQUENCE,
                            $this->_encodeLength(strlen($parameters)),
                            $parameters
                        );

                        $RSAPrivateKey = pack(
                            'Ca*a*Ca*a*',
                            self::ASN1_SEQUENCE,
                            $this->_encodeLength(strlen($encryptionAlgorithm)),
                            $encryptionAlgorithm,
                            self::ASN1_OCTETSTRING,
                            $this->_encodeLength(strlen($RSAPrivateKey)),
                            $RSAPrivateKey
                        );

                        $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);

                        $RSAPrivateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" .
                                         chunk_split(base64_encode($RSAPrivateKey), 64) .
                                         '-----END ENCRYPTED PRIVATE KEY-----';
                    } else {
                        $RSAPrivateKey = "-----BEGIN PRIVATE KEY-----\r\n" .
                                         chunk_split(base64_encode($RSAPrivateKey), 64) .
                                         '-----END PRIVATE KEY-----';
                    }
                    return $RSAPrivateKey;
                }

                if (!empty($this->password) || is_string($this->password)) {
                    $iv = Random::string(8);
                    $symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key
                    $symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
                    $des = new TripleDES();
                    $des->setKey($symkey);
                    $des->setIV($iv);
                    $iv = strtoupper(bin2hex($iv));
                    $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
                                     "Proc-Type: 4,ENCRYPTED\r\n" .
                                     "DEK-Info: DES-EDE3-CBC,$iv\r\n" .
                                     "\r\n" .
                                     chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) .
                                     '-----END RSA PRIVATE KEY-----';
                } else {
                    $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
                                     chunk_split(base64_encode($RSAPrivateKey), 64) .
                                     '-----END RSA PRIVATE KEY-----';
                }

                return $RSAPrivateKey;
        }
    }
 /**
  * @inheritDoc
  */
 public function encrypt($decryptedValue, $key, $iv)
 {
     $this->aes->setKey($key);
     $this->aes->setIV($iv);
     return $this->aes->encrypt($decryptedValue);
 }
 /**
  * @param string $decrypted
  *
  * @return bool|string
  */
 public function encrypt($decrypted)
 {
     return base64_encode($this->aes->encrypt($decrypted));
 }