需要openssl.net RSA加密工作示例 [英] openssl.net rsa encryption working example needed

查看:165
本文介绍了需要openssl.net RSA加密工作示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人有RSA加密与 OpenSSL.NET 的任何工作的例子吗?我要加密使用存储在PEM格式的私钥的一些数据。

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.

我创建了一个OpenSSL.Crypto.RSA对象,并要使用PrivateEncrypt方法,但它抛出OpenSSLException没有额外的数据(空数组的错误,没有内部异常)。使用PrivateEncrypt方法之前,我填写的所有属性(如PublicModulus,PrivateExponent等)从命令
OpenSSL的RSA -in private_key.pem -text -noout

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

有谁知道如何读PEM文件到OpenSSL.Crypto.RSA对象或者有其他工作的加密的例子吗?

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

推荐答案

这是C / C ++在Linux上,但我发现这样的没有简单的例子,直到我痛苦地得到了这个工作。

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

生成密钥的命令行

的OpenSSL genrsa -out privkey.pem 2048

openssl genrsa -out privkey.pem 2048

HelloWord.cpp

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

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

这篇关于需要openssl.net RSA加密工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆