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

  • SEARCH
  • Home
  • 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 1108693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:07:28+00:00 2026-05-17T02:07:28+00:00

Does anybody have any working example of RSA encryption with OpenSSL.NET ? I want

  • 0

Does anybody have any working example of RSA encryption with OpenSSL.NET ? I want to encrypt some data using private key stored in PEM format.

I create a OpenSSL.Crypto.RSA object and want to use the PrivateEncrypt method, but it throws OpenSSLException with no additional data (empty Errors array, no inner exception). Before using the PrivateEncrypt method I fill all the properties (like PublicModulus, PrivateExponent etc) with data read from command
openssl rsa -in private_key.pem -text -noout

Does anybody know how to read the PEM file into OpenSSL.Crypto.RSA object or has any other working encryption example?

  • 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-05-17T02:07:28+00:00Added an answer on May 17, 2026 at 2:07 am

    This is C/C++ on linux but I found no simple examples like this until I painfully got this to work

    Generate key command line

    openssl genrsa -out privkey.pem 2048

    HelloWord.cpp

    #include <global_inc.h>
    #include <openssl/rsa.h>
    #include <openssl/pem.h>
    
    int main()
    {
            char *message = "Hello World";
            unsigned char* encrypted = (unsigned char *) malloc(500);
            unsigned char* decrypted = (unsigned char *) malloc(500);
            int bufSize;
    
            FILE *keyfile = fopen("privkey.pem", "r");
            RSA *rsa = PEM_read_RSAPrivateKey(keyfile, NULL, NULL, NULL);
            printf("\n\nStarting Message = %s\n", message);
            if (rsa == NULL)
            {
                    printf("Badness has occured! Did not read key file\n");
                    return 0;
            }
            else
            {
                    printf("Opened the key file OK!\n");
            }
    
            bufSize = RSA_public_encrypt(strlen(message), (unsigned char *) message, encrypted, rsa, RSA_PKCS1_PADDING);
            if (bufSize == -1)
            {
                    printf("Badness has occured! encryption failed\n");
                    RSA_free(rsa);
                    return 0;
            }
            else
            {
                    printf("Encrypted the message OK! = \n%s\n", encrypted );
            }
    
            if (RSA_private_decrypt(bufSize, encrypted, decrypted, rsa, RSA_PKCS1_PADDING) != -1)
            {
                    printf("\nMessage decrypted to : %s\n", decrypted);
            }
            else
            {
                    printf("Badness has occured! decryption failed\n");
                    RSA_free(rsa);
                    return 0;
            }
    
            RSA_free(rsa);
            return 1;
    }
    

    Makefile

    #-----------------------------------------------------------------------------
    #
    # File    : global.make
    # Date    : 09/03/2009
    # Author  : Tom Nortillo
    #
    # Description: universal make definitions for development area
    #
    #-----------------------------------------------------------------------------
    
    #----------------------------------
    #   GENERAL
    #----------------------------------
    CPP=g++
    BASE=/home/joneil001/RSAEncryption
    CPPFLAGS = -c -fPIC
    LDFLAGS = -static
    BIN = ${BASE}
    
    
    #===================================================================
    #
    #                    THIRD-PARTY LIBRARIES
    #
    #===================================================================
    
    #-------------------
    #       ORACLE
    #-------------------
    ORALIB= -L${ORACLE_LIB} -lclntsh
    ORAINC= -I${ORACLE_HOME}/precomp/public -I${ORACLE_HOME}/rdbms/public
    
    PROC=${ORACLE_BIN}/proc
    ORAEXT = -DORACA_STORAGE_CLASS=extern -DSQLCA_STORAGE_CLASS=extern
    
    
    #-------------------
    #     LIBXML
    #-------------------
    XML_INC = -I${BASE}/lib_xml/include/libxml2
    XML_LIB = -L${BASE}/lib_xml/lib -lxml2
    
    
    #--------------------------------
    #     GOOGLE PROTOCOL BUFFERS
    #--------------------------------
    GOOGLE_INC = -I${BASE}/lib_google/include
    GOOGLE_LIB = -L${BASE}/lib_google/lib -lprotobuf
    GOOGLE_BIN = ${BASE}/lib_google/bin
    
    
    #==============================================
    #
    #                   OpenSSL
    #
    #=============================================
    
    OPENSSL_LIB = -L/usr/lib64 -lcrypto -L/usr/lib64/openssl/engines -laep -lcswift  -lchil -l4758cca -lgmp -lubsec -lsureware -lnuron -latalla
    
    
    #===================================================================
    #
    #                    BUILD COMMAND-LINES
    #
    #===================================================================
    
    #--------------------
    #   LIBRARIES
    #--------------------
    LIBLIST = -L${BASE}/lib \
              ${OPENSSL_LIB}
    
    # Repeated twice because of library inter-dependencies
    LIBS = ${LIBLIST} ${LIBLIST}
    
    
    
    #--------------------
    #   INCLUDES
    #--------------------
    LOCAL_INC = -I.
    
    INCLUDE = ${LOCAL_INC} ${ORAINC}
    
    
    
    
    #===================================================================
    #
    #                          RULES
    #
    #===================================================================
    .SUFFIXES: .cpp
    .SUFFIXES: .cc $(SUFFIXES)
    .SUFFIXES: .pc $(SUFFIXES)
    .SUFFIXES: .proto $(SUFFIXES)
    
    .cpp.o:
            ${CPP} ${CPPFLAGS} ${INCLUDE} $<
    
    .cc.o:
            ${CPP} ${CPPFLAGS} ${INCLUDE} $<
    
    .pc.o:
            ${PROC} SYS_INCLUDE=/usr/include include=${ORAINC} code=CPP cpp_suffix=cpp parse=NONE dbms=v8 iname=$< oname=$(*F).cpp lname=$(*F).lis
            ${CPP} ${CPPFLAGS} ${INCLUDE} ${ORAINC} ${ORAEXT} $*.cpp
            rm -f $*.cpp
            rm -f $*.lis
            rm -f tp*
    
    .proto.o:
            ${GOOGLE_BIN}/protoc --cpp_out=. $<
            ${CPP} ${CPPFLAGS} ${INCLUDE} ${ORAINC} ${ORAEXT} $*.pb.cc
    
    
    
    
    #===================================================================
    #
    #                           TARGETS
    #
    #===================================================================
    
    
    TARGET=doit
    
    OBJECTS = HelloWorld.o
    
    all: ${OBJECTS}
            ${CPP} ${INCLUDE} -o ${BIN}/${TARGET} ${OBJECTS} ${LIBS}
    
    clean:
            touch HelloWorld.o; rm *.o
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody have any experience with the Raphael.js SVG library? I'm using Raphael.js to
Does anybody have any reference material that details Cauchy-Reed algorithm? Googling for Cauchy-Reed Solomon
Does anybody have any experience with different fonts for OCR? I am generating an
I have three easy questions. Does anybody use QuickTest Pro for automated testing? Any
I have to analyse some existing Erlang code. Does anybody knows about a tool
Does anybody have any tips for utilizing multiple languages at the same time? I
Does anybody have any idea why iPad zoom (you know, where you use two
Does anybody have any experience developing with all of these for the iPhone, in
Does anybody have a short code sample that can be run in the VS
I've been looking for a simple regex for URLs, does anybody have one handy

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.