System Key based encryption

This example demonstrates how to use system keys to encrypt/decrypt data

Example code encrypt:


procedure TFrmMain.btnEncryptClick(Sender: TObject);
var
  Source: TRwBytes;
begin
  // The Encrypt function performs encryption on the data in a ASource.
  // Typically, only a user with the same logon credential as the user who encrypted the data can decrypt the data.
  // In addition, the encryption and decryption usually must be done on the same computer.
  //
  // When the ALocalMachine argument is set, it associates the data encrypted with the current computer instead of with an individual user.
  // Any user on the computer on which Encrypt is called can use Decrypt to decrypt the data.
  Source := TRwBytes.FromString(memoToEncrypt.Text, True);
  edtEncrypted.Text := TRwDataProtection.Encrypt(Source, False).Encode(beBase64);
end;

Example code decrypt:


procedure TFrmMain.btnDecryptClick(Sender: TObject);
var
  Source: TRwBytes;
begin
  // The Decrypt function decrypts previously encrypted data
  // Note that the data can only be decrypted by the same user (when encrypted with ALocalMachine False)
  // on the same system!
  //
  // Try copying the decrypted data to a different system and encrypte it there
  Source := TRwBytes.Decode(edtEncrypted.Text, beBase64);
  memoDecrypted.Text := TRwDataProtection.Decrypt(Source).ToString(True);
end;


Back to Examples