How to wrap these C functions? lazarus / 21-General ---------------------------------------------------------------------- garlar27 · 2021-02-01 15:02 Hello, I need to wrap some C functions and I'm having a hard time doing it. For instance this function: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);  int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen);  Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     char   msg[KEY_LENGTH/8];  // Message to encrypt    char   *encrypt = NULL;  // Encrypted message    char   *decrypt = NULL;    // Decrypted message     // Decrypt it    decrypt = malloc(encrypt_len);    if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, keypair, RSA_PKCS1_OAEP_PADDING) == -1) {        ERR_load_crypto_strings();        ERR_error_string(ERR_get_error(), err);        fprintf(stderr, "Error decrypting message: %s\n", err);      goto free_stuff;    } The previous code was taken from here I'm implementing the above code like this but I don't know if I'm doing it OK (specially on the PByte cast, I never did any thing like that before) since i have a SIGSEG exception: 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 uses openssl;var   Msg: String;   pk: PEVP_PKEY;   TheRSA: PRSA;   DecryptedData, EncryptedData: PChar;//PByte;   PBEnc, PBDec: PByte;   Resulta: cint;begin   {...}      EncryptedData := 'SomesSringThatCameFromAParameter';      DecryptedData := strings.stralloc(512);      PBEnc := PByte(EncryptedData);      PBDec := PByte(DecryptedData);      TheRSA := openssl.RSA_new(); // I know I'm not loading the private key and that is wrong...       Resulta := openssl.RSA_private_decrypt(Length(Msg), PBEnc, PBDec, TheRSA, RSA_PKCS1_OAEP_PADDING);//<---  HERE FAILS      if Resulta = -1 then begin         //openssl.ERR_load_crypto_strings;         ERR_error_string(openssl.ErrGetError(), err);         ShowMessageFmt('Error decrypting message: %s\n', [err]);         Exit;      end;end;  I need to know if the wrapping is well done or if I need to change something. Thanks. ---------------------------------------------------------------------- sstvmaster · 2021-02-01 20:22 Hi, i dont know if this helps, but here you get an idea: https://github.com/sinitcin/RsaLazarusOpenSSL DecryptRSA line 119 in main.pas -> line 252 in extendedssl.pas ---------------------------------------------------------------------- Zvoni · 2021-02-02 09:06 the variable msg is an Array of Char in your C-Code with Length of KEY_LENGTH / 8 In your Pascal-Code it's a String, i don't see intialized to any Length. Are you passing a Length=0 to the Function? ---------------------------------------------------------------------- garlar27 · 2021-02-02 18:06 Thanks @sstvmaster, it helped me a lot!!! Do you know how to load an existing Private Key from a file or a string? The man page of OpenSSL seems to lack of that sort of thing... There's an example in FPC ({Lazarusfolder}\fpc\3.2.0\source\packages\o penssl\examples\genkeypair.lpr) for generating the keys but not of how to load it and use them. @Zvoni: Oops, that array shouldn't have stayed at the code snip, the C example I took it used it for input from the console and then cast it to a PChar for encryption...