Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:02:14+00:00 2026-06-15T19:02:14+00:00

I’m using C++ library PolarSSL for RSA encryption and decryption. But I’m not able

  • 0

I’m using C++ library PolarSSL for RSA encryption and decryption. But I’m not able to decrypt an encrypted string unless it’s an output from encryption. Following code doesn’t work (it’s not refactored). It encrypts text and encodes the output to the Base64 and back. Condition on strcmp works (strings are the same).

AsymetricCipher::encrypt(const std::string &pathToPublicKey, std::istream &inputData, std::ostream &encryptedData) {
    if(initServerPublicCtx(pathToPublicKey, 512)) {
        std::cout << "Encryption error: Can't load public key from file: " << pathToPublicKey << std::endl;
        return false;
    }

    entropy_context entropy;
    ctr_drbg_context ctr_drbg;
    char *pers = "rsa_encrypt";

    entropy_init(&entropy);
    if(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (unsigned char*)pers, strlen(pers)) != 0) {
        std::cout << "Encryption error: ctr_drbg_init failed" << std::endl;
        return false;
    }

    size_t inputSize = ::getStreamSize(inputData);
    char *input = new char[inputSize];
    memset(input, 0, inputSize);
    inputData.read(input, inputSize);
    input[inputSize] = '\0';

    unsigned char *buffer = new unsigned char[ctx.len];
    memset(buffer, 0, ctx.len);
    memcpy(buffer, input, inputSize);

    // This has to be rewritten
    size_t MAX_OUTPUT_LENGTH = ctx.len;
    unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];
    memset(outputBuffer, 0, MAX_OUTPUT_LENGTH);

    if(rsa_pkcs1_encrypt(&ctx, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, inputSize, buffer, outputBuffer) != 0) {
        std::cout << "Encryption error: rsa_pkcs1_encrypt failed" << std::endl;
        return false;
    }

    initServerPrivateCtx("data/private.key", 512);
    size_t outputSize = 0;

    std::string copyBuffer = "";

    std::stringstream encStream;

    std::string base64 = "";

    Base64Wrapper::encode(outputBuffer, strlen((char*)outputBuffer), base64);


    for(size_t i = 0; i < base64.length();) {
        encStream << base64[i++];
    }

    unsigned char *encBuffer = new unsigned char[MAX_OUTPUT_LENGTH+10];
    memset(encBuffer, 0, MAX_OUTPUT_LENGTH);
    encStream.read((char *)encBuffer, MAX_OUTPUT_LENGTH+10);
    copyBuffer.append((char *)encBuffer);

    unsigned char *decoded = NULL;
    size_t decodedSize = 0;

    Base64Wrapper::decode(copyBuffer, &decoded, &decodedSize);
    decoded[decodedSize] = '\0';

    if(strcmp((char*)outputBuffer, (char*)decoded) != 0) {
        std::cout << "Different";
    }

    memset(buffer, 0, ctx.len);

    if(rsa_pkcs1_decrypt(&ctx, RSA_PRIVATE, &outputSize, decoded, buffer, MAX_OUTPUT_LENGTH) != 0) {
        std::cout << "Decryption error: rsa_pkcs1_decrypt failed" << std::endl;
        return false;
    }

    ::cleanMemory(outputBuffer, MAX_OUTPUT_LENGTH);
    ::cleanMemory(buffer, ctx.len);

    delete [] outputBuffer;
    delete [] buffer;
    delete [] encBuffer;
    delete [] decoded;
    //delete [] input;

    return true;
}

However, if I call rsa_pkcs1_decrypt with outputBuffer from encryption, everything works fine.

I need to encrypt text, send it and decrypt on another place in code.

Any suggestions what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:02:15+00:00Added an answer on June 15, 2026 at 7:02 pm

    I’ve finally found a solution.

    The problem was strlen((char*)outputBuffer) becuase it was always 0 since output from rsa_pkcs1_encrypt starts with \0.

    The right solution is

    bool AsymetricCipher::encrypt(const std::string &pathToPublicKey, std::istream &inputData, std::ostream &encryptedData) {
        if(initServerPublicCtx(pathToPublicKey, 512)) {
            std::cout << "Encryption error: Can't load public key from file: " << pathToPublicKey << std::endl;
            return false;
        }
    
        entropy_context entropy;
        ctr_drbg_context ctr_drbg;
        char *pers = "rsa_encrypt";
    
        entropy_init(&entropy);
        if(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (unsigned char*)pers, strlen(pers)) != 0) {
            std::cout << "Encryption error: ctr_drbg_init failed" << std::endl;
            return false;
        }
    
    
        size_t inputSize = ::getStreamSize(inputData);
        unsigned char *buffer = new unsigned char[inputSize];
        memset(buffer, 0, inputSize);
        inputData.read((char *)buffer, inputSize);
    
    
        size_t MAX_OUTPUT_LENGTH = ctx.len;
        unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];
        memset(outputBuffer, 0, MAX_OUTPUT_LENGTH);
    
        bool retVal = true;
        if(rsa_pkcs1_encrypt(&ctx, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, inputSize, buffer, outputBuffer) != 0) {
            std::cout << "Encryption error: rsa_pkcs1_encrypt failed" << std::endl;
            retVal = false;
        }
    
        if(retVal) {
            std::string base64;
            Base64Wrapper::encode(outputBuffer, MAX_OUTPUT_LENGTH, base64);
            encryptedData << base64;
            ::cleanMemory(base64);
        }
    
        ::cleanMemory(outputBuffer, MAX_OUTPUT_LENGTH);
        ::cleanMemory(buffer, ctx.len);
    
        delete [] outputBuffer;
        delete [] buffer;
    
        return retVal;
    }
    

    And for decryption

    bool AsymetricCipher::decrypt( const std::string &pathToPrivateKey, std::istream &encryptedData, std::ostream &decryptedData ) {
        if(initServerPrivateCtx(pathToPrivateKey, 512)) {
            std::cout << "Decrypt error: Can't load private key from file: " << pathToPrivateKey << std::endl;
            return false;
        }
    
        size_t inputSize = ::getStreamSize(encryptedData);
        size_t outputSize = 0;
    
        unsigned char* buffer = NULL;
        std::string base64;
        size_t bufferSize = 0;
        encryptedData >> base64;
    
        Base64Wrapper::decode(base64, &buffer, &bufferSize);
        ::cleanMemory(base64);
    
        size_t MAX_OUTPUT_LENGTH = ctx.len;
        unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];
    
        bool retVal = true;
        if(rsa_pkcs1_decrypt(&ctx, RSA_PRIVATE, &outputSize, buffer, outputBuffer, MAX_OUTPUT_LENGTH) != 0) {
            std::cout << "Decryption error: rsa_pkcs1_decrypt failed" << std::endl;
            retVal = false;
        }
    
        if(retVal) {
            outputBuffer[outputSize] = '\0';
            decryptedData << outputBuffer;
        }
    
        ::cleanMemory(buffer, bufferSize);
        ::cleanMemory(outputBuffer, outputSize);
    
        delete [] outputBuffer;
        delete [] buffer;
    
        return retVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.