login about faq

Hello, I am unsure how to correctly load a pem file I generate and saved to disk. I want to load the public key, encode a string and load the private key from another pem file to decrypt the string. I did try with LoadPem as no Password is given, but can not get it to work.

I am currently generating and writing the pem files as follows (works very well):

<?php
include("chilkat_9_5_0.php");

$rsa = new CkRsa();

$success = $rsa->UnlockComponent("using my key");
if ($success != true) {
    print 'RSA unlock failed' . "\n";
    exit;
}
$success = $rsa->GenerateKey(2048);
if ($success != true) {
    print $rsa->lastErrorText() . "\n";
    exit;
}

$publicKeyXml = $rsa->exportPublicKey();
print "public: ".$publicKeyXml . "\n";

$privateKeyXml = $rsa->exportPrivateKey();
print "private: ".$privateKeyXml . "\n";

//  Save the private key in PEM format:
$privKey = new CkPrivateKey();
$success = $privKey->LoadXml($privateKeyXml);
$success = $privKey->SaveRsaPemFile('privateKey.pem');

//  Save in PEM format
$pubKey = new CkPublicKey();
$success = $pubKey->LoadXml($publicKeyXml);
$success = $pubKey->SaveOpenSslPemFile('publicKey.pem');

?>

asked Dec 08 '16 at 16:32

RootTag's gravatar image

RootTag
35121623


Can you post the contents of the LastErrorText property after the LoadPem call fails?

link

answered Dec 09 '16 at 09:49

jpbro's gravatar image

jpbro ♦
1.1k2618

Generating a new RSA key makes no sense here. If you already have a private key, then you already have both the private and public key.

Most people don't realize that a public key is just a sub-set of the private key.
For example, an RSA private key is composed of the following parts: Modulus, Exponent, P, DP, DQ, InverseQ, and D. For example, a private key in XML format:

<RSAKeyValue>
    <Modulus>vqZ07gIBoLEz...JZJgk002xPgSAd</Modulus>
    <Exponent>AQAB</Exponent>
    <P>8RUZbaB91v8...VeGowfkHnEl</Q>
    <DP>oTHmarKg8N7...QKTTCGhQKz/aGg/3haj</DP>
    <DQ>TOlF8Bhyf...AmJbs9J</DQ>
    <InverseQ>3awPjKI4b...SOMqN/r7UZ96upx</InverseQ>
    <D>VieCz8u4YNTtbyue...pKx7Ga3J2HXtQ/d</D>
</RSAKeyValue>

A public key is just a sub-set of the private key. It contains just the Modulus and Exponent. Therefore, if you already have the private key, by definition you have the matching public key. To get the public key object from the private key object, just call privateKey.GetPublicKey.

link

answered Dec 09 '16 at 11:56

chilkat's gravatar image

chilkat ♦♦
12.8k317363471

I only need to find a way to load my previously stored private key (inside my privateKey.pem file) into my rsa object. I saw the function getRsaPem() which is deprecated and will be removed.

Anyway it does not load the correct string as written into the pem file itself.

With my public key I was able to use the function getPem() and the Output of the string is exactly what is written inside the publicKey.pem.

Isn't there such a function for the Private key as well?

link

answered Dec 09 '16 at 17:14

RootTag's gravatar image

RootTag
35121623

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×54
×44
×5

Asked: Dec 08 '16 at 16:32

Seen: 637 times

Last updated: Dec 14 '16 at 20:21

powered by OSQA