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

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

my $privKey = Chilkat2::PrivateKey->new;

my $sbPem = Chilkat2::StringBuilder->new;
$sbPem->AppendLine('-----BEGIN RSA PRIVATE KEY-----', 1);
$sbPem->AppendLine('MIIC .... j5A==', 1);
$sbPem->AppendLine('-----END RSA PRIVATE KEY-----', 1);

my $success = $privKey->LoadPem($sbPem->GetAsString());
if (!$success) {
    print $privKey->LastErrorText, "\n";
    exit;
}

my $rsa = Chilkat2::Rsa->new;

$success = $rsa->UsePrivateKey($privKey);
if (!$success) {
    print $rsa->LastErrorText, "\n";
    exit;
}

my $bd = Chilkat2::BinData->new;
$bd->AppendString('12345678', 'utf-8');

$success = $rsa->SignRawBd($bd);
if (!$success) {
    print $rsa->LastErrorText, "\n";
    exit;
}

# Get the base64 RSA signature.
print $bd->GetEncoded('base64'), "\n";

$success = $rsa->VerifyRawBd($bd);
if (!$success) {
    print $rsa->LastErrorText, "\n";
    exit;
}

my $strOriginal = $bd->GetString('utf-8');
print $strOriginal, "\n";