PHP Requires Chilkat v11.0.0+
PHP
RSA Sign Binary Data and Verify (Recover the Original Data)
See more RSA Examples
Demonstrates how to RSA sign binary data and then verify/recover the original data.Chilkat PHP Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\PrivateKey;
use Chilkat2\PublicKey;
use Chilkat2\Rsa;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load an RSA private key for signing.
$privKey = new PrivateKey();
try {
$privKey->LoadEncryptedPemFile('qa_data/pem/rsa_passwd.pem', 'passwd');
$rsa = new Rsa();
$rsa->UsePrivateKey($privKey);
// We have some binary data (in hex) to sign
$originalData = '0102030405060708090A';
$bd = new BinData();
$bd->AppendEncoded($originalData, 'hex');
// If successful, the contents of bd are replaced with the RSA signature.
$rsa->SignRawBd($bd);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Show the RSA signature in base64
echo $bd->GetEncoded('base64'), "\n";
// ------------------------------------------
// Get the public key from the private key
$pubKey = new PublicKey();
$privKey->ToPublicKey($pubKey);
// Verify the signature and extract the original data.
$rsa2 = new Rsa();
$rsa2->UsePublicKey($pubKey);
try {
$rsa2->VerifyRawBd($bd);
$bVerified = true;
} catch (\Chilkat2\Exception $e) {
$bVerified = false;
}
echo 'signature verified: ', ($bVerified ? 'true' : 'false'), "\n";
// Show the original data:
echo 'original data: ', $bd->GetEncoded('hex'), "\n";