I want to encrypt a text using PublicKey in Chilkat but it doesn't work and the error message is: "Private Key Required, privatekey not provided" As Chilkat document says encryption should always use publicKey for encryption https://www.chilkatsoft.com/refdoc/dd_CkRsaRef.html
This is my code and i'm really confused about why my code doesn't work. This function has a input of Tbytes which contains the ASCII code of the text to be encrypted and text is in Hex mode.
and public key is stored in "C:\PK.txt" KeyLength shows that I have a 1024 Bit key and even CkRsa_ImportPublicKey returns true This is the PublicKey "8e9c531e740e72f386c0a9aad397ab7409efb3ec9177bb8ca341e7c104d72ab5a29489e4e4b6eae731eb3cd1c78672626de664491fcf9134454ca5df3d2779a89dc5e87eeb81b69900d2b86723826ce899a5a749d026b8cb37e11846d45e549d15448d546fef54330b8d6f8f02e22cfd8d4ccb97b3baa75eb9a5a56ff4b00d59"
I'm using Delphi 10.3 so I can declare inline variables
any answer in any language is ok, just tell me why I've got this error Thank you in advance
function EncryptText(PlainTextByte: TBytes): Boolean;
var InputByteArr: TBytes;
begin
var RSA3: HCkRsa:= CkRsa_Create();
Result:= False;
if Not CkRsa_UnlockComponent(RSA3, 's8xxx.xxxxxxxx') then
begin
CkRsa_Dispose(RSA3);
Exit(False);
end;
var tmp_Enc: PWideChar;
var RSA_PublicKey: PWideChar;
var tmp_File: TStringList:= TStringList.Create;
tmp_File.LoadFromFile('c:\pk.txt', TEncoding.ANSI);
var PubKey: String:= StringReplace(tmp_File.Text, sLineBreak, '', [rfReplaceAll]);
var PubKeyStr: String:='<RSAPublicKey><Modulus>' + PubKey + '</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>';
RSA_PublicKey:= PWideChar(PubKeyStr);
var PlainText: String:= '';
bUsePrivateKey := False;
var EncStr: String:= '';
var tmp_PlainText: PWideChar;
SetLength(InputByteArr, Length(PlainTextByte) Div 2);
for var i := 0 to Length(PlainTextByte) - 1 do // input is Hex
if i Mod 2 = 0 then
InputByteArr[i div 2]:= (PlainTextByte[i] * 16) + PlainTextByte[i + 1];
var InputByteArrPointer: PByte:= Addr(InputByteArr);
var ckBytes: HCkByteData:= CkByteData_Create();
CkByteData_borrowData(ckBytes, InputByteArrPointer, Length(InputByteArr));
if CkRsa_ImportPublicKey(RSA3, RSA_PublicKey) then
begin
CkRsa_putOaepPadding(RSA3, False);
CkRsa_putEncodingMode(RSA3, 'base64');
var bUsePrivateKey: Boolean:= False; // This tells Chilkat not to use privatekey
var KeyLength:Integer:= CkRsa_getNumBits(RSA3); // This shows bit length
tmp_Enc:= CkRsa__encryptBytesENC(RSA3, ckBytes, bUsePrivateKey); // This doesn't work neither => CkRsa__encryptStringENC()
tmp_Enc:= CkRsa__lastErrorText(RSA3); // this tells that i need to provide private key to encrypt
EncStr:= WideCharToString(tmp_Enc);
tmp_File.Text:= EncStr;
tmp_File.SaveToFile('c:\output.txt', TEncoding.ANSI);
CkByteData_Dispose(ckBytes);
InputByteArr:= nil;
Result:= True;
end;
tmp_File.Free;
tmp_File:= nil;
CkRsa_Dispose(RSA3);
end;