ECC Sign Error, returning a #0 String

Hi,
So I have this project that encrypts a string using the TECCEncSign object with cc25519 encryption, its supposed to encrypt a string, send on an HTTP Request to another project that receives and decrypts it.

At first I assumed it was the HTTP Request messing with the string, however after putting a decrypt on the same function that encrypts it on the client project, I realized it returns me a string full of #0s

Here is the code -

function EncryptAuthentication(const S: String): String;
var
ecc, ecc2: TECCEncSign;
ES, DS: String;
begin
ecc := TECCEncSign.Create(nil);
ecc.ECCType := cc25519;
ecc.OutputFormat := TConvertType.base64url;
ecc.Unicode := noUni;
TDialogService.ShowMessage('Data before encrypt - ' + S);
ecc.PublicKey := 'xxxxx=';
ecc.PrivateKey := 'xxxxx=';
//ES := ecc.Encrypt(S);
ES := ecc.Encrypt('TEST');
DS := ecc.Decrypt(ES);

Of course, both Public and Private Keys were generated using ecc.GenerateKeys and then fixed on both the client and server projects, I swapped out the string for a mere ‘TEST’, but still when I try to decrypt it, it returns my DS variable with something like ‘#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0’ regardless of what I do.

Any ideas? It seems to be something on the decrypt function itself that breaks the mathematic output

Hi Ron,

I need to have a look but it looks like a conversion issue or, more likely, a parameter that is not passed the way it should to the AES.

Hi Ron,

This works for me, but I may have a slightly different implementation of ECC and need to publish an update.

procedure TMainForm.EncryptBtnClick(Sender: TObject);
var
ecc: TECCEncSign;
ES, DS, S: String;

begin
ecc              := TECCEncSign.Create(nil);
ecc.ECCType      := cc25519;
ecc.OutputFormat := TConvertType.hexa;
ecc.Unicode      := noUni;
ecc.NaCl         := naclyes;
S := 'TEST';

MainMemo.Lines.Add('Data before encrypt - ' + S);
//  ecc.GenerateKeys; // using NaCl = YES
ecc.PublicKey    := '42C65EEDD0A440C390B972282F6049AF6F22B4387894E379EC30E03881420BAE'; // test keys
ecc.PrivateKey   := 'E992599D16179BA38A5BA30670D7C0AECE852A0DA9FA1904D0A74A570F5A7618';

ES := ecc.Encrypt(S);
MainMemo.Lines.Add(ES);
DS := ecc.Decrypt(ES);
MainMemo.Lines.Add(DS);
end;

The trick is you have to generate keys with ecc.NaCl := naclyes. This will not be interoperable with v4.3 as ECIES did not use the NaCl property that changes the way bytes are ordered.

If you don’t do that, encryption and tag generation keys are incorrect, verification fails and returns an empty buffer.That’s why you get a string packed with 0’s.

Regards,

bernard

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.