(PHP Extension) RSA Import Public Key from Certificate PEM
Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.
<?php
// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");
$cert = new CkCert();
$success = $cert->LoadFromFile('qa_data/pem/mf_public_rsa.pem');
if ($success == false) {
print $cert->lastErrorText() . "\n";
exit;
}
// pubKey is a CkPublicKey
$pubKey = $cert->ExportPublicKey();
if ($cert->get_LastMethodSuccess() != true) {
print $cert->lastErrorText() . "\n";
exit;
}
$rsa = new CkRsa();
$success = $rsa->ImportPublicKeyObj($pubKey);
if ($success == false) {
print $rsa->lastErrorText() . "\n";
exit;
}
$rsa->put_EncodingMode('base64');
$encryptedStr = $rsa->encryptStringENC('hello',false);
print 'encrypted string = ' . $encryptedStr . "\n";
?>
|