Jose\Util\RSA::encrypt PHP Method

encrypt() public static method

Encryption.
public static encrypt ( RSAKey $key, string $plaintext, string $hash_algorithm ) : string
$key Jose\KeyConverter\RSAKey
$plaintext string
$hash_algorithm string
return string
    public static function encrypt(RSAKey $key, $plaintext, $hash_algorithm)
    {
        /*
         * @var Hash
         */
        $hash = Hash::$hash_algorithm();
        $length = $key->getModulusLength() - 2 * $hash->getLength() - 2;
        Assertion::greaterThan($length, 0);
        $plaintext = str_split($plaintext, $length);
        $ciphertext = '';
        foreach ($plaintext as $m) {
            $ciphertext .= self::encryptRSAESOAEP($key, $m, $hash);
        }
        return $ciphertext;
    }

Usage Example

Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function encryptKey(JWKInterface $key, $cek, array $complete_headers, array &$additional_headers)
 {
     $this->checkKey($key);
     $pub = RSAKey::toPublic(new RSAKey($key));
     if (self::ENCRYPTION_OAEP === $this->getEncryptionMode()) {
         $encrypted = JoseRSA::encrypt($pub, $cek, $this->getHashAlgorithm());
         Assertion::string($encrypted, 'Unable to encrypt the data.');
         return $encrypted;
     } else {
         $res = openssl_public_encrypt($cek, $encrypted, $pub->toPEM(), OPENSSL_PKCS1_PADDING | OPENSSL_RAW_DATA);
         Assertion::true($res, 'Unable to encrypt the data.');
         return $encrypted;
     }
 }