1

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
  • I might be able to help out, working on something right now but it's not complete yet so I can't tell if it really works or not. Commented Sep 3, 2010 at 12:55

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.