Sample code for 30+ languages & platforms
Delphi DLL

RSA Encrypt with Modulus and Exponent

See more RSA Examples

Demonstrates how to RSA encrypt with a given modulus and exponent.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, PublicKey, Rsa, Xml;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
rsa: HCkRsa;
modulus: PWideChar;
exponent: PWideChar;
xml: HCkXml;
pubKey: HCkPublicKey;
usePrivateKey: Boolean;
plainText: PWideChar;
encryptedStrBase64: PWideChar;

begin
success := False;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

rsa := CkRsa_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 := CkXml_Create();
CkXml_putTag(xml,'RSAPublicKey');
CkXml_NewChild2(xml,'Modulus',modulus);
CkXml_NewChild2(xml,'Exponent',exponent);

pubKey := CkPublicKey_Create();
success := CkPublicKey_LoadFromString(pubKey,xml);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPublicKey__lastErrorText(pubKey));
    Exit;
  end;

success := CkRsa_UsePublicKey(rsa,pubKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa));
    Exit;
  end;

usePrivateKey := False;
plainText := 'message in a bottle';

CkRsa_putEncodingMode(rsa,'base64');
encryptedStrBase64 := CkRsa__encryptStringENC(rsa,plainText,usePrivateKey);
Memo1.Lines.Add(encryptedStrBase64);

CkRsa_Dispose(rsa);
CkXml_Dispose(xml);
CkPublicKey_Dispose(pubKey);

end;