Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

RSA Import Public Key from Certificate PEM

See more RSA Examples

Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Cert;
use Chilkat2\PublicKey;
use Chilkat2\Rsa;

$cert = new Cert();

try {
    $cert->LoadFromFile('qa_data/pem/mf_public_rsa.pem');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$pubKey = new PublicKey();
$cert->GetPublicKey($pubKey);

$rsa = new Rsa();
$rsa->UsePublicKey($pubKey);

$rsa->EncodingMode = 'base64';
$encryptedStr = $rsa->EncryptStringENC('hello', false);
echo 'encrypted string = ', $encryptedStr, "\n";