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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\PrivateKey;
use Chilkat2\PublicKey;
use Chilkat2\Rsa;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$rsa = new Rsa();

// First demonstrate importing a PEM public key:
$publicKeyPem = 'PEM public-key data goes here';
$pubkey = new PublicKey();

try {
    $pubkey->LoadFromString($publicKeyPem);

    $rsa->UsePublicKey($pubkey);

    // Demonstrate importing a PEM private key:
    $privateKeyPem = 'PEM private-key data goes here';
    $privkey = new PrivateKey();

    // If the private key PEM is protected with a password, then
    // call LoadEncryptedPem.  Otherwise call LoadPem.
    $privkey->LoadPem($privateKeyPem);

    $rsa->UsePrivateKey($privkey);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "OK!\n";