Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
RSA Encrypt with Modulus and Exponent
See more RSA Examples
Demonstrates how to RSA encrypt with a given modulus and exponent.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.PublicKey,
Chilkat.Rsa,
Chilkat.Xml;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rsa: TRsa;
modulus: string;
exponent: string;
xml: TXml;
pubKey: TPublicKey;
usePrivateKey: Boolean;
plainText: string;
encryptedStrBase64: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rsa := TRsa.Create;
// Assuming you already have a base64 modulus and exponent,
// wrap it in XML like this:
modulus := 'qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM=';
exponent := 'AQAB';
xml := TXml.Create;
xml.Tag := 'RSAPublicKey';
xml.NewChild2('Modulus',modulus);
xml.NewChild2('Exponent',exponent);
pubKey := TPublicKey.Create;
success := pubKey.LoadFromString(xml);
if (success = False) then
begin
WriteLn(pubKey.LastErrorText);
Exit;
end;
success := rsa.UsePublicKey(pubKey);
if (success = False) then
begin
WriteLn(rsa.LastErrorText);
Exit;
end;
usePrivateKey := False;
plainText := 'message in a bottle';
rsa.EncodingMode := 'base64';
encryptedStrBase64 := rsa.EncryptStringENC(plainText,usePrivateKey);
WriteLn(encryptedStrBase64);
rsa.Free;
xml.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.