RSA pem file vs from key

I am using Delphi 10.2 and Cryptography Pack 4.3.2.1

I am using RSAEncSign and having trouble with the difference between loading the keys from a file and having the same keys included in the project as a const.

When I use FromOpenSSLPublicKey or FromOpenSSLPrivateKey the encode and decode work as expected.
When I copy the contents of the file to a const variable, I get error "RSA Key Length not supported", using FromPublicKey or FromPrivateKey.

I have tried with and without the "-----BEGIN PUBLIC KEY-----" and tags.

I am not sure how to use the converter class to convert the file contents to a usable format.

working code:
RSA := TRSAEncSign.Create(nil);
RSA.keyLength := kl2048;
RSA.outputFormat := base64;
RSA.Unicode := yesUni;
RSA.encType := epkcs1_5;
sFileName := ExtractFilePath(Application.ExeName)+'\public-key.pem';
RSA.withOpenSSL := true;
RSA.FromOpenSSLPublicKey(sFileName);
memoOut.Text := RSA.Encrypt(memoIn.Text);

code that errors:
RSA := TRSAEncSign.Create(nil);
RSA.keyLength := kl2048;
RSA.outputFormat := base64;
RSA.Unicode := yesUni;
RSA.encType := epkcs1_5;
RSA.withOpenSSL := true;
RSA.FromPrivateKey(PrivateKey);
memoOut.Text := RSA.Encrypt(memoIn.Text);

Thanks.

Hello Nicola,
This is a conversion issue and the unexpected key format decodes into an "unsupported key length".
You need to extract and convert the key first (check byte ordering too). Then, I recommand you don't store any key (excepts for public ones) in the clear in any binary.
Regards,
bernard