Search code examples
freepascallazarus

Asymmetric encryption in lazarus


Is there any library for lazarus ( free pascal ) which supports asymmetric encryption?


Solution

  • To make asymmetric encryption in Lazarus you need to integrate openssl.

    If you use windows download http://slproweb.com/download/Win32OpenSSL_Light-1_0_1e.exe

    Now you can use openssl in lazarus like this:

    uses ..., Process;
    
    ...
    
    procedure encrypt_file();
    var hProcess : TProcess;
    begin
      hProcess := TProcess.Create(nil);
      hProcess.Executable := 'openssl';
      hprocess.Parameters.Add('aes-256-cbc');
      hprocess.Parameters.Add('-a -salt');
      hprocess.Parameters.Add('-in secrets.txt'); //input file
      hprocess.Parameters.Add('-out secrets.txt.enc'); //output file  
      hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes]; 
      hProcess.Execute;
    end;
    
    procedure decrypt_file();
    var hProcess : TProcess;
    begin
      hProcess := TProcess.Create(nil);
      hProcess.Executable := 'openssl';
      hprocess.Parameters.Add('aes-256-cbc');
      hprocess.Parameters.Add('-d -salt');
      hprocess.Parameters.Add('-in secrets.txt.enc'); //input file
      hprocess.Parameters.Add('-out secrets.txt.new'); //output file  
      hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes]; 
      hProcess.Execute;
    end;
    

    Note: The example is for symmetric encryption for asymmetric read this article: http://www.devco.net/archives/2006/02/13/public_-_private_key_encryption_using_openssl.php