Это C / C ++ в linux, но я не нашел простых примеров, подобных этому, пока я мучительно не заставил это работать
Генерация ключа командной строки
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