I'm getting started with TurboPower Lockbox for Delphi. Unfortunately I couldn't find any documentation, so I'm using basic functionality to try and find out how Lockbox works. In the example below I have a button for encrypting plaintext to ciphertext, and one for decrypting the ciphertext back to plaintext. This seems to work, but note that I've used different encryption and decryption keys. (The generated ciphertext is also always the same, no matter what the key/passphrase is.)
So apparently I'm either using the SetKey method wrong, or I'm missing a step.
procedure TMainForm.BtnEncryptClick(Sender: TObject);
begin
with TLbRijndael.Create(nil) do
try
SetKey('Key1');
EdtEncrypted.Text := EncryptString(EdtPlainText.Text);
finally
Free;
end;
end;
procedure TMainForm.BtnDecryptClick(Sender: TObject);
begin
with TLbRijndael.Create(nil) do
try
SetKey('Key2');
EdtDecrypted.Text := DecryptString(EdtEncrypted.Text);
finally
Free;
end;
end;
What's wrong/missing in this code?
Edit
Extra credit for a link to Lockbox' documentation :-)
You're using a very old version of Lockbox (Lockbox2). You can find docs for that version at Turbo Power Lockbox Files
The problem with your code is that you've never set the KeySize, and by default the KeySize length in bytes is 0 (due to what appears to be a bug in the TLbRijndael constructor and destructor) . That being the case, your keys match because what you pass to SetKey() is, in effect, ignored. Using the debugger and tracing into the SetKey() method would have shown you that the move() statement was moving zero bytes.
However.... You should not call SetKey() directly unless you make sure that value passed to SetKey() is key-sized bytes long. Instead your should call the GenerateKey() method, which will take a string and generate the proper sized key.
procedure TMainForm.BtnEncryptClick(Sender: TObject);
begin
with TLbRijndael.Create(nil) do
try
KeySize := ks128;
GenerateKey('Key1');
EdtEncrypted.Text := EncryptString(EdtPlainText.Text);
finally
Free;
end;
end;