Jose\Util\RSA::decrypt PHP Method

decrypt() public static method

Decryption.
public static decrypt ( RSAKey $key, string $ciphertext, string $hash_algorithm ) : string
$key Jose\KeyConverter\RSAKey
$ciphertext string
$hash_algorithm string
return string
    public static function decrypt(RSAKey $key, $ciphertext, $hash_algorithm)
    {
        Assertion::greaterThan($key->getModulusLength(), 0);
        $hash = Hash::$hash_algorithm();
        $ciphertext = str_split($ciphertext, $key->getModulusLength());
        $ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $key->getModulusLength(), chr(0), STR_PAD_LEFT);
        $plaintext = '';
        foreach ($ciphertext as $c) {
            $temp = self::getRSAESOAEP($key, $c, $hash);
            $plaintext .= $temp;
        }
        return $plaintext;
    }

Usage Example

Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function decryptKey(JWKInterface $key, $encrypted_key, array $header)
 {
     $this->checkKey($key);
     Assertion::true($key->has('d'), 'The key is not a private key');
     $priv = new RSAKey($key);
     if (self::ENCRYPTION_OAEP === $this->getEncryptionMode()) {
         $decrypted = JoseRSA::decrypt($priv, $encrypted_key, $this->getHashAlgorithm());
         Assertion::string($decrypted, 'Unable to decrypt the data.');
         return $decrypted;
     } else {
         $res = openssl_private_decrypt($encrypted_key, $decrypted, $priv->toPEM(), OPENSSL_PKCS1_PADDING | OPENSSL_RAW_DATA);
         Assertion::true($res, 'Unable to decrypt the data.');
         return $decrypted;
     }
 }