Need Examples of Use of RSA With OpenSSL Library lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- garlar27 · 2021-01-19 15:41 Hi everybody, I don't know if this is the best place for this question. I need to decrypt a string from a JSON/XML attribute/text field using RSA private key and exponent. Also I need to generate the Public Key, Private Key and exponent using through the OpenSSl library. I know that OpenSSL is been used in the https and sftp connection. But I don't know where to look to find out how all of this is done and therefore comes my question: Where can I find examples of how to decrypt an encrypted string using RSA private key and exponent? ---------------------------------------------------------------------- garlar27 · 2021-01-29 00:59 MAYBE THIS POST SHOULD BE MOVED TO THE GENERAL CATEGORY... First things first. I suck at C code!! Thus I need help to use this function from openssl.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 function RSA_private_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;  I'm using this example in C as a guide: 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 #include #include #include #include #include  #define KEY_LENGTH  2048#define PUB_EXP     3#define PRINT_KEYS#define WRITE_TO_FILE int main(void) {    size_t pri_len;            // Length of private key    size_t pub_len;            // Length of public key  char   *pri_key;           // Private key    char   *pub_key;         // Public key    char   msg[KEY_LENGTH/8];  // Message to encrypt  char   *encrypt = NULL;    // Encrypted message    char   *decrypt = NULL;    // Decrypted message    char   *err;               // Buffer for any error messages     // Generate key pair    printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);    fflush(stdout);    RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);     // To get the C-string PEM form:    BIO *pri = BIO_new(BIO_s_mem());    BIO *pub = BIO_new(BIO_s_mem());     PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);    PEM_write_bio_RSAPublicKey(pub, keypair);     pri_len = BIO_pending(pri);    pub_len = BIO_pending(pub);     pri_key = malloc(pri_len + 1);    pub_key = malloc(pub_len + 1);     BIO_read(pri, pri_key, pri_len);    BIO_read(pub, pub_key, pub_len);     pri_key[pri_len] = '\0';    pub_key[pub_len] = '\0';     #ifdef PRINT_KEYS        printf("\n%s\n%s\n", pri_key, pub_key);    #endif  printf("done.\n");     // Get the message to encrypt    printf("Message to encrypt: ");    fgets(msg, KEY_LENGTH-1, stdin);  msg[strlen(msg)-1] = '\0';     // Encrypt the message    encrypt = malloc(RSA_size(keypair));    int encrypt_len;    err = malloc(130);  if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,                                       keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {        ERR_load_crypto_strings();        ERR_error_string(ERR_get_error(), err);        fprintf(stderr, "Error encrypting message: %s\n", err);      goto free_stuff;    }     #ifdef WRITE_TO_FILE    // Write the encrypted message to a file        FILE *out = fopen("out.bin", "w");        fwrite(encrypt, sizeof(*encrypt),  RSA_size(keypair), out);      fclose(out);        printf("Encrypted message written to file.\n");        free(encrypt);        encrypt = NULL;         // Read it back      printf("Reading back encrypted message and attempting decryption...\n");        encrypt = malloc(RSA_size(keypair));        out = fopen("out.bin", "r");        fread(encrypt, sizeof(*encrypt), RSA_size(keypair), out);        fclose(out);    #endif     // 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;    }  printf("Decrypted message: %s\n", decrypt);     free_stuff:    RSA_free(keypair);    BIO_free_all(pub);    BIO_free_all(pri);    free(pri_key);    free(pub_key);    free(encrypt);    free(decrypt);  free(err);     return 0;}I'll appreciate any help on this subject. Now the whining and wailing (you can skip this part): Well, I have my eyes sore reading code from lNet, Synapse and code in JAVA as well, and I couldn't find much. In lNet it seams like there is no RSA decryption or it is really hard for me to find the place where decryption is done. In Synapse I can't see how it is done either. The LockBox component has an implementation for RSA but it can't decrypt beyond 1024 bit, so for what we need is useless and didn't even try to use it. Then after a long time of looking to the Lazarus forum and the wiki. Digging in JAVA Android Studio code. And also googling around I found the C code example (above mentioned) doing the decryption (I stumbled many times on it thanks to this old post RSA Encryption ). At first I didn't pay attention to it because I'm not comfortable with C code and there's a lot of things that I don't know well how to wrap it around with Pascal code. But after reaching the end of the road I found that FPC has something done with OpenSSL and there is an example (located in {Lazarusfolder}\ fpc\3.2.0\source\packages\openssl\examples\genkeypair.lpr) to generate both keys (public and private) which is good but not good enough. So I looked through the whole FPC and Lazarus code and couldn't find a place where the (deprecated from OpenSSL library) function RSA_private_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint; from openssl.pas is used  Thank you very much if you went this far reading.