Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get RSA Key Modulus from .cer or .key
See more Certificates Examples
Demonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be:openssl x509 -inform DER -in "test.cer" -modulus -nooutor
openssl pkcs8 -inform DER -inβ "test.key"β -outform PEM -passin pass:"12345β678aβ" | openssl rsa -inform PEM -modulus -noout
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.BinData,
Chilkat.PrivateKey,
Chilkat.Xml,
Chilkat.PublicKey,
Chilkat.Cert;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
privKey: TPrivateKey;
password: string;
xml: TXml;
modulus: string;
binDat: TBinData;
hexModulus: string;
cert: TCert;
pubKey: TPublicKey;
begin
success := False;
privKey := TPrivateKey.Create;
password := '12345678a';
success := privKey.LoadPkcs8EncryptedFile('qa_data/certs/test_12345678a.key',password);
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
xml := TXml.Create;
xml.LoadXml(privKey.GetXml());
// The XML contains the parts of the key in base64.
WriteLn('Private Key XML:');
WriteLn(xml.GetXml());
// We can get the base64 modulus like this:
modulus := xml.GetChildContent('Modulus');
WriteLn('base64 modulus = ' + modulus);
// To convert to hex:
binDat := TBinData.Create;
binDat.AppendEncoded(modulus,'base64');
hexModulus := binDat.GetEncoded('hex');
WriteLn('hex modulus = ' + hexModulus);
// Now get the modulus from the cert:
cert := TCert.Create;
success := cert.LoadFromFile('qa_data/certs/test_12345678a.cer');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// The cert contains the public key, which is composed of the
// modulus + exponent (for RSA keys).
pubKey := TPublicKey.Create;
cert.GetPublicKey(pubKey);
xml.LoadXml(pubKey.GetXml());
WriteLn('Public Key XML:');
WriteLn(xml.GetXml());
// Proceed in the same way as before....
modulus := xml.GetChildContent('Modulus');
WriteLn('base64 modulus = ' + modulus);
// To convert to hex:
binDat.Clear();
binDat.AppendEncoded(modulus,'base64');
hexModulus := binDat.GetEncoded('hex');
WriteLn('hex modulus = ' + hexModulus);
privKey.Free;
xml.Free;
binDat.Free;
cert.Free;
pubKey.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.