Greetings,
I'm trying to perform an AES encryption based on the CryptoChatClient demo in Web Core:
unit CryLib;
interface
Uses SysUtils, Classes, WEBLib.WebSocketClient, JS, XData.Web.Client, WEBLib.Controls, Web, WEBLib.Crypto,WEBLib.REST, WEBLib.JSON;
Type
TBtEncryptionClient = class
EncryptedString:String;
DecryptedString:String;
aesGCM: TAESEncryption;
procedure CryptoError(AError: string);
procedure CryptoKeyCreated(Sender: TObject);
procedure MessageEncrypted(ADataAsArrayBuffer: TJSArrayBuffer);
procedure MessageDecrypted(AData: string);
procedure DoCreate;
procedure DoEncrypted(str:String);
procedure DoDecrypted(str:String);
end;
implementation
Const
KEY_FIRST = '{"alg":"A128GCM","ext":true,"k":"CQ4H9zufO9lxBhecfx-aXQ","key_ops":["encrypt","decrypt"],"kty":"oct"}';
procedure TBtEncryptionClient.CryptoError(AError: string);
begin
{
if ContainsText(AError, 'OperationError') then
begin
lst.BeginUpdate;
lst.HTML.Add('A message has arrived. Decoding was unsuccessful with the selected key.<br>');
lst.EndUpdate;
end;
}
end;
procedure TBtEncryptionClient.CryptoKeyCreated(Sender: TObject);
begin
aesGCM.ImportKey(KEY_FIRST);
end;
procedure TBtEncryptionClient.MessageEncrypted(ADataAsArrayBuffer: TJSArrayBuffer);
begin
BtMes('MessageEncrypted');
EncryptedString := ABToBinaryString(ADataAsArrayBuffer);
BtMEs('hjahaha: ' +EncryptedString);
//WebSocketClient1.Send(s);
end;
procedure TBtEncryptionClient.MessageDecrypted(AData: string);
begin
DecryptedString := AData;
end;
procedure TBtEncryptionClient.DoCreate;
begin
aesGCM := TAESEncryption.Create(aetGCM);//(Nil);
aesGCM.OnKeyCreated := CryptoKeyCreated; //Change key to a predefined one when class is ready
aesGCM.OnEncrypted := MessageEncrypted;
aesGCM.OnDecryptedString := MessageDecrypted;
aesGCM.OnError := CryptoError;
aesGCM.ImportKey(KEY_FIRST);
end;
procedure TBtEncryptionClient.DoEncrypted(str:String);
begin
aesGCM.Encrypt(str);
end;
procedure TBtEncryptionClient.DoDecrypted(str:String);
var
s: String;
ab: TJSArrayBuffer;
begin
s := str;
ab := BinaryStringToAB(s);
aesGCM.Decrypt(ab, drtString);
end;
function NewSocketEnCrypt(str:String):String;
Var
ss:TBtEncryptionClient;
Begin
ss:=TBtEncryptionClient.Create;
ss.DoCreate;
ss.DoEncrypted(str);
Result := ss.EncryptedString;
End;
procedure BtnClick(Sender: TObject);
begin
NewSocketEnCrypt('ABCD');
end;
This results in an error: Uncaught SyntaxError: Unexpected end of JSON input.
What am I missing?
Kind regards.