Create a new public/private key pair

@param string $publicKey Public key to create
@param string $privateKey Private key for the new public key
@return Model
@throws InvalidArgumentException If the public key name is invalid
424282
public function addPublicKey($publicKey, $privateKey) {
        $this->validatePublicKeyName($publicKey);

        if ($this->publicKeyExists($publicKey)) {
            throw new InvalidArgumentException(
                'Public key with name "' . $publicKey . '" already exists'
            );
        }

        return $this->getCommand('EditPublicKey', array(
            'properties' => json_encode(array('privateKey' => $privateKey)),
            'publicKey' => $publicKey,
        ))->execute();
    }
607407
public function startHarvesting($privateKey) 
    {
        $params = ["value" => $privateKey];

        $apiUrl = $this->getPath('unlock', $params);
        $response = $this->api->post($apiUrl, []);

        //XXX include Error checks
        $object = json_decode($response);
        return $this->createBaseModel($object); //XXX brr => error/content validation first
    }
165447
public function getPublicKey()
    {
        $res = \openssl_pkey_get_private($this->keyMaterial);
        $pubkey = \openssl_pkey_get_details($res);
        $public = \rtrim(
            \str_replace("\n", "\r\n", $pubkey['key']),
            "\r\n"
        );
        return new PublicKey($public);
    }
270523
public function decryptByPublicKey_data($data, $publickey = '')
	{
		$RSACrypt = new RSACrypt();

		if ($publickey != "") {
			$RSACrypt->pubkey = $publickey;
		}
		$decrypt_res = "";
		$datas = explode('@', $data);
		foreach ($datas as $value) {
			$decrypt_res = $decrypt_res . $RSACrypt->decryptByPublicKey($value);
		}
		return $decrypt_res;
	}
1206578
public function store(PrivateKey $privateKey, $name, array $meta = null)
    {
        $exportedData = $this->privateKeyExporter->exportPrivateKey($privateKey);

        $this->keyStorage->store(new KeyEntry($name, $exportedData, $meta));
    }
1125052
public function setPrivateKey($path)
    {
        if(!file_exists($path)) {
            throw new \InvalidArgumentException('the private key file does\'t exist');
        }

        $this->privateKey = file_get_contents($path);

        return $this;
    }
287784
public function setKey($key = null)
    {
        if (empty($key)) {

            $key = $this->get('private_key');

            if (empty($key)) {
                $key = gplcart_string_random();
                $this->set('private_key', $key);
            }

        } else {
            $this->set('private_key', $key);
        }

        return $this->key = $key;
    }
147633
protected static function rsaDecrypt($ciphertext, PrivateKey $rsaPrivateKey)
    {
        $rsa = self::getRsa(RSA::ENCRYPTION_OAEP);

        $return = $rsa->loadKey($rsaPrivateKey->getKey());
        if ($return === false) {
            throw new InvalidKeyException('Decryption failed due to invalid key');
        }

        $return = @$rsa->decrypt($ciphertext);
        if ($return === false) {
            throw new InvalidCiphertextException('Decryption failed');
        }
        return $return;
    }
364596
public function privateKey(): PrivateKey
    {
        $algo = $this->algorithmIdentifier();
        switch ($algo->oid()) {
            // RSA
            case AlgorithmIdentifier::OID_RSA_ENCRYPTION:
                return RSA\RSAPrivateKey::fromDER($this->_privateKeyData);
            // elliptic curve
            case AlgorithmIdentifier::OID_EC_PUBLIC_KEY:
                $pk = EC\ECPrivateKey::fromDER($this->_privateKeyData);
                // NOTE: OpenSSL strips named curve from ECPrivateKey structure
                // when serializing into PrivateKeyInfo. However RFC 5915 dictates
                // that parameters (NamedCurve) must always be included.
                // If private key doesn't encode named curve, assign from parameters.
                if (!$pk->hasNamedCurve()) {
                    if (!$algo instanceof ECPublicKeyAlgorithmIdentifier) {
                        throw new \UnexpectedValueException(
                            "Not an EC algorithm.");
                    }
                    $pk = $pk->withNamedCurve($algo->namedCurve());
                }
                return $pk;
        }
        throw new \RuntimeException(
            "Private key " . $algo->name() . " not supported.");
    }
484844
public static function create($params, $publickey = null, $secretkey = null)
    {
        return parent::g_create(get_class(), self::getUrl(), $params, $publickey, $secretkey);
    }
1339984
public function exportPrivateKey(VirgilPrivateKey $privateKey, $password = '')
    {
        try {
            return $this->cryptoService->privateKeyToDer($privateKey->getValue(), $password);
        } catch (Exception $exception) {
            throw new VirgilCryptoException($exception->getMessage());
        }
    }
1004855
public static function generateKeyPair($size = 2048)
    {
        if ($size < 2048) {
            throw new InvalidKeyException('Key size must be at least 2048 bits.');
        }
        $rsa = new RSA();
        $keypair = $rsa->createKey($size);
        return new KeyPair(
            new PrivateKey($keypair['privatekey']),
            new PublicKey($keypair['publickey'])
        );
    }
1114123
public function add($user, \fpoirotte\Pssht\PublicKeyInterface $key)
    {
        if (!is_string($user)) {
            throw new \InvalidArgumentException();
        }

        $this->keys[$user][$this->getIdentifier($key)] = $key;
    }
441313
private function isValidKeyPair(PrivateKey $private, PublicKey $public = null) : bool
    {
        // If no public key was supplied, the public key will be extracted from the private key.
        if ($public === null) {
            return true;
        }

        // If the public key extracted from the private key matches the supplied public key, the pair must be valid.
        if ($private->getPublicKey() == $public) {
            return true;
        }

        return false;
    }
894875
public static PublicKey createPublicKey(byte[] publicKey) {
    if (publicKey == null) {
      return null;
    }
    
    try {
      KeyFactory fac = KeyFactory.getInstance("RSA");
      EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
      return fac.generatePublic(spec);
    }
    catch (NoSuchAlgorithmException e) {
      throw new IllegalStateException(e);
    }
    catch (InvalidKeySpecException e) {
      throw new IllegalStateException(e);
    }
  }
1213631
public function sign($contents, $privateKey)
    {
        $rsa = new RSA();
        $rsa->loadKey($privateKey);

        $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);

        return $rsa->sign($contents);
    }
572447
public static PrivateKey loadPrivateKey(File privateKeyFile) throws InvalidKeySpecException, IOException {
        if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".pem") ) {
            InputStream is = new FileInputStream(privateKeyFile);
            try {
                return PEM.readPrivateKey(is);
            } finally {
                try {is.close();} catch(IOException ignore) {}
            }
        } else if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".der") ) {
            InputStream is = new FileInputStream(privateKeyFile);
            try {
                return RSA.privateKeyFromPKCS8(IOUtils.toByteArray(is));
            } finally {
                try {is.close();} catch(IOException ignore) {}
            }
        } else {
            throw new AmazonClientException("Unsupported file type for private key");
        }
    }