How to decript a string using RSA Private Key ?? lazarus / 21-General ---------------------------------------------------------------------- garlar27 · 2022-02-10 22:32 I need to decrypt a string using RSA Private Key but I couldn't find an FPC example. By the way I don't know what FPC is using now LibreSSL or OpenSSL. So i searched for a Delphi example and tried see if I could adapt it to the lazarus fpc\3.2.2\source\packages\openssl\src\openssl.pas The one I found was this https://github.com/ddlencemc/RSA-via-OpenSSL- libeay32 and some function had slightly different names and changes in param types and other where completely missing probably because differences in SSL version. The code I'm interested to port is this one (RSAOpenSSL.pas): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TRSAOpenSSL.PrivateDecrypt(var aRSAData: TRSAData);var  rsa: pRSA;  out_: AnsiString;  str, data: PAnsiChar;  len: Integer;  b64, mem: pBIO;  size: Integer;  err: Cardinal;begin  LoadSSL;  FPrivateKey := LoadPrivateKey;  //FPrivateKey := LoadPrivateKeyFromString(''); // Load PrivateKey from including ansistring;  if FPrivateKey = nil then  begin    err := ERR_get_error;    repeat      aRSAData.ErrorMessage:= aRSAData.ErrorMessage + string(ERR_error_string(err, nil)) + #10;    err := ERR_get_error;    until err = 0;    exit;  end;  rsa := EVP_PKEY_get1_RSA(FPrivateKey);  size := RSA_size(rsa);   GetMem(data, size);  GetMem(str, size);   b64 := BIO_new(BIO_f_base64);  mem := BIO_new_mem_buf(PAnsiChar(aRSAData.EncryptedData), Length(aRSAData.EncryptedData));  BIO_flush(mem);  mem := BIO_push(b64, mem);  BIO_read(mem, str , Length(aRSAData.EncryptedData));  BIO_free_all(mem);   len := RSA_private_decrypt(size, PAnsiChar(str), data, rsa, RSA_PKCS1_PADDING);   if len > 0 then  begin    SetLength(out_, len);    Move(data^, PAnsiChar(out_ )^, len);    aRSAData.ErrorResult := 0;  aRSAData.ErrorMessage := 'Base64 has been decoded and decrypted' + #10;    aRSAData.DecryptedData := out_;  end  else  begin    err := ERR_get_error;    aRSAData.ErrorResult := -1;    repeat      aRSAData.ErrorMessage := aRSAData.ErrorMessage + string(ERR_error_string(err, nil)) + #10;      err := ERR_get_error;  until err = 0;  end;  RSA_free(rsa);  FreeSSL;end;  procedure TRSAOpenSSL.PrivateEncrypt(var aRSAData: TRSAData);var  rsa: pRSA;  str, data: AnsiString;  len, b64len: Integer;  penc64: PAnsiChar;  b64, mem: pBIO;  size: Integer;  err: Cardinal;begin  LoadSSL;  FPrivateKey := LoadPrivateKey;   if FPrivateKey = nil then  begin    err := ERR_get_error;    repeat      aRSAData.ErrorMessage := aRSAData.ErrorMessage + string(ERR_error_string(err, nil)) + #10;    err := ERR_get_error;    until err = 0;    exit;  end;   rsa := EVP_PKEY_get1_RSA(FPrivateKey);  EVP_PKEY_free(FPrivateKey);   size := RSA_size(rsa);   GetMem(FCryptedBuffer, size);  str := AnsiString(aRSAData.DecryptedData);   len := RSA_private_encrypt(Length(str), PAnsiChar(str), FCryptedBuffer, rsa, RSA_PKCS1_PADDING);   if len > 0 then  begin    aRSAData.ErrorResult := 0;    //create a base64 BIO    b64 := BIO_new(BIO_f_base64);    mem := BIO_push(b64, BIO_new(BIO_s_mem));    try      //encode data to base64      BIO_write(mem, FCryptedBuffer, len);      BIO_flush(mem);      b64len := BIO_get_mem_data(mem, penc64);       //copy data to string      SetLength(data, b64len);      Move(penc64^, PAnsiChar(data)^, b64len);      aRSAData.ErrorMessage := 'String has been encrypted, then base64 encoded.' + #10;      aRSAData.EncryptedData := string(data);    finally      BIO_free_all(mem);    end;  end  else  begin    err := ERR_get_error;    aRSAData.ErrorResult := -1;    repeat      aRSAData.ErrorMessage := aRSAData.ErrorMessage + string(ERR_error_string(err, nil)) + #10;    err := ERR_get_error;    until err = 0;  end;  RSA_free(rsa);end;  Now I'm using this code to decrypt the string but I don't want to use a file for this task Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DecryptRSAFileTo(const DecryptedFile, EncryptedFile, KeyFile: string): Boolean;var   hProcess : TProcess;begin   Result := False;   hProcess := TProcess.Create(nil);   try      //openssl rsautl -decrypt -in input.txt -out output.txt -inkey private.pem -raw      hProcess.Executable := '/usr/bin/openssl';      hprocess.Parameters.Add('rsautl'                );//'aes-256-cbc');    hprocess.Parameters.Add('-decrypt'              );//'-d -salt');    hprocess.Parameters.Add('-in'                   );//'-in secrets.txt.enc'); //input file      hprocess.Parameters.Add(EncryptedFile           );//'-in secrets.txt.enc'); //input file      hprocess.Parameters.Add('-out'                );//'-out secrets.txt.new'); //output file      hprocess.Parameters.Add(DecryptedFile           );//'-out secrets.txt.new'); //output file      hprocess.Parameters.Add('-inkey'                );      hprocess.Parameters.Add(KeyFile               );      hprocess.Parameters.Add('-raw'                  );      hProcess.Options := hProcess.Options + [poWaitOnExit{, poStderrToOutPut, poUsePipes}];      hProcess.Execute;    finally    FreeAndNil(hProcess);   end;   Result := True;end;  Thanks. ---------------------------------------------------------------------- RDL · 2022-02-11 12:33 garlar27 At one time I solved this problem, but I had to use opensslext.pas https://github.com/frones/ACBr/blob/master/Fontes/ACBrOpenSSL/OpenSSLE xt.pas But there are also not all the openssl functions and I had to declare them myself.