Decrypt with RSAES-OAEP + MGF1+SHA256

@param string $plaintext
@param PublicKey $rsaPublicKey
@return string
@throws InvalidCiphertextException
501657
protected static function rsaEncrypt($plaintext, PublicKey $rsaPublicKey)
    {
        $rsa = self::getRsa(RSA::ENCRYPTION_OAEP);

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

        return $rsa->encrypt($plaintext);
    }
1333435
public static String encrypt(byte[] key256Bits, String plainText) {
        SecretKey secretKey = new SecretKeySpec(key256Bits, "AES");
        return encryptBouncyCastle(secretKey, plainText);
//        return encryptLegacyJavaSecurity(secretKey, plainText);
    }
1203741
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
		return encryptByPublicKey(data, publicKey, "RSA/ECB/PKCS1Padding");
	}
690179
def decode_ack_plain(ciphertext)
      message = begin
                  @ecc.ecies_decrypt ciphertext[0,210]
                rescue
                  raise AuthenticationError, $!
                end
      raise RLPxSessionError, 'invalid message length' unless message.size == 64+32+1

      ephemeral_pubkey = message[0,64]
      nonce = message[64,32]
      known = message[-1].ord
      raise RLPxSessionError, 'invalid known byte' unless known == 0

      [210, ephemeral_pubkey, nonce, 4]
    end
436993
function decrypt($ciphertext)
    {
        if ($this->engine == self::ENGINE_OPENSSL) {
            $temp = $this->key;
            $this->key = $this->orig_key;
            $result = parent::decrypt($ciphertext);
            $this->key = $temp;
            return $result;
        }

        return parent::decrypt($ciphertext);
    }
492080
public function getDecryptExpression($text, $decryptionKey, $quote = false)
    {
        $quotedText = $quote ? DB::connection()->getPdo()->quote($text) : $text;
        $quotedDecryptKey = DB::connection()->getPdo()->quote($decryptionKey);
        return "pgp_sym_decrypt({$quotedText}::bytea, $quotedDecryptKey)";
    }
170437
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
        String keyBytes = Base64Utils.decode(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes.getBytes());
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicK);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        int i = 0;
        setData(encryptedData, cipher, inputLen, out, offSet, i);

        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }
322848
public static function encrypt($plainText, $key, $cipher)
    {
        $cipher = strtolower($cipher);

        if (false === in_array($cipher, openssl_get_cipher_methods())) {
            throw new \LogicException(sprintf('Cipher %s not supported', $cipher));
        }

        $ivLen = openssl_cipher_iv_length($cipher);
        $iv = openssl_random_pseudo_bytes($ivLen);

        $cipherTextRaw = openssl_encrypt($plainText, $cipher, $key, OPENSSL_RAW_DATA, $iv);
        $hmac = hash_hmac('sha256', $cipherTextRaw, $key, true);

        return base64_encode(sprintf('%s%s%s', $iv, $hmac, $cipherTextRaw));
    }
21863
function decrypt($encryptedText)
    {
        $parts = explode(':', $encryptedText);

        if (count($parts) != 2) {
            throw new DecryptionException();
        }

        $main = base64_decode($parts[0]);
        $iv = base64_decode($parts[1]);
        return openssl_decrypt($main, $this->method, $this->key, $this->options, $iv);
    }
1335070
function encrypt($plainText)
    {
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method));
        $encrypted = openssl_encrypt($plainText, $this->method, $this->key, $this->options, $iv);
        return base64_encode($encrypted) . ':' . base64_encode($iv);
    }
983985
final public static function encrypt(string $plainText, string $key = null, string $method = null): string
    {
        if ($method === null) {
            $method = static::getMethod();

            if (!in_array($method, openssl_get_cipher_methods())) {
                throw new CryptException("OpenSSL method={$method} defined in application config is not available");
            }
        } else {
            if (!in_array($method, openssl_get_cipher_methods())) {
                throw new CryptException("Passed OpenSSL method={$method} is not available");
            }
        }

        $key = Util::str2hex($key ?? Application::getKey());
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));

        $encrypted = @openssl_encrypt($plainText, $method, $key, 0, $iv);

        if ($encrypted === false) {
            throw new CryptException('Unable to encrypt your plain text; check your openssl_default_method in application config');
        }

        return $encrypted . ':' . bin2hex($iv);
    }
1203790
function rsaEncrypt(messageBuffer, publicKey) {
    var logger = getLogger('security.cipher.rsaEncrypt');
    var messageLength = messageBuffer.length;
    if (!messageBuffer || messageLength > 255) {
        logger.warn('Message is undefined or exceeds 255 bytes length.');
        return;
    } else if (messageLength < 255) {
        // Add random bytes as padding
        var paddingLength = 255 - messageLength;
        messageBuffer = Buffer.concat([messageBuffer, utility.createRandomBuffer(paddingLength)]);
    }
    // Encrypt the message
    var modulus = new BigInteger(publicKey.getModulus(), 16);
    var exponent = new BigInteger(publicKey.getExponent(), 16);
    var message = new BigInteger(messageBuffer);
    var encryptedMessage = new Buffer(message.modPowInt(exponent, modulus).toByteArray());
    if (encryptedMessage.length > 256) {
        encryptedMessage = encryptedMessage.slice(encryptedMessage.length - 256);
    }
    if (logger.isDebugEnabled()) {
        logger.debug('Plain-text message(%s) = %s', messageBuffer.length, messageBuffer.toString('hex'));
        logger.debug('Encrypted message(%s) = %s', encryptedMessage.length, encryptedMessage.toString('hex'));
    }
    return encryptedMessage;
}
980275
function RSAEncryptOAEP(text, hash, hashLen) {
  var m = oaep_pad(text, (this.n.bitLength() + 7) >> 3, hash, hashLen);
  if(m == null) return null;
  var c = this.doPublic(m);
  if(c == null) return null;
  var h = c.toString(16);
  if((h.length & 1) == 0) return h; else return "0" + h;
}
283286
function decrypt($ciphertext)
    {
        if ($this->engine == CRYPT_ENGINE_OPENSSL) {
            $temp = $this->key;
            $this->key = $this->orig_key;
            $result = parent::decrypt($ciphertext);
            $this->key = $temp;
            return $result;
        }

        return parent::decrypt($ciphertext);
    }
176284
static byte []
fromPublicKey(PublicKey key, int alg) throws DNSSECException
{
	byte [] data = null;

	switch (alg) {
	case Algorithm.RSAMD5:
	case Algorithm.RSASHA1:
	case Algorithm.RSA_NSEC3_SHA1:
	case Algorithm.RSASHA256:
	case Algorithm.RSASHA512:
		if (! (key instanceof RSAPublicKey))
			throw new IncompatibleKeyException();
		return fromRSAPublicKey((RSAPublicKey) key);
	case Algorithm.DSA:
	case Algorithm.DSA_NSEC3_SHA1:
		if (! (key instanceof DSAPublicKey))
			throw new IncompatibleKeyException();
		return fromDSAPublicKey((DSAPublicKey) key);
	default:
		throw new UnsupportedAlgorithmException(alg);
	}
}
477297
public static function rsaPublicEncrypt(
        $str,
        $publicKeyPath,
        $padding = OPENSSL_PKCS1_PADDING,
        $format = self::FORMAT_HEX
    )
    {
        openssl_public_encrypt(
            trim($str),
            $cryrted,
            openssl_get_publickey(file_get_contents($publicKeyPath)),
            $padding
        );

        $cryrted = trim($cryrted);

        switch ($format) {
            case self::FORMAT_HEX:
                return bin2hex($cryrted);
            case self::FORMAT_BASE64:
                return EncodeHelper::base64Encode($cryrted);
        }

        return bin2hex($cryrted);
    }
709030
public function hashPassword($plainTextPassword) {

		if(strlen($plainTextPassword) < self::MIN_LENGTH) {
			throw new \InvalidArgumentException(sprintf("Password too short. Minimum length is %d.", self::MIN_LENGTH));
		}
		
		if(strlen($plainTextPassword) > self::MAX_LENGTH) {
			throw new \InvalidArgumentException(sprintf("Password too long. Maximum length is %d.", self::MAX_LENGTH));
		}
		
		return password_hash($plainTextPassword, PASSWORD_DEFAULT, ['cost' => $this->cost]);
		
	}