需要 openssl.net rsa 加密工作示例

发布于 2024-09-15 10:24:25 字数 432 浏览 4 评论 0原文

有人有使用 OpenSSL.NET 进行 RSA 加密的工作示例吗?我想使用以 PEM 格式存储的私钥加密一些数据。

我创建一个 OpenSSL.Crypto.RSA 对象并想要使用 PrivateEncrypt 方法,但它抛出 OpenSSLException 且没有附加数据(空 Errors 数组,没有内部异常)。在使用 PrivateEncrypt 方法之前,我使用从命令读取的数据填充所有属性(如 PublicModulus、PrivateExponent 等) openssl rsa -in private_key.pem -text -noout

有谁知道如何将 PEM 文件读入 OpenSSL.Crypto.RSA 对象或有任何其他工作加密示例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-09-22 10:24:25

这是Linux上的C/C++,但我没有找到这样的简单例子,直到我痛苦地让它工作

生成关键命令行

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} 
lt;

.cc.o:
        ${CPP} ${CPPFLAGS} ${INCLUDE} 
lt;

.pc.o:
        ${PROC} SYS_INCLUDE=/usr/include include=${ORAINC} code=CPP cpp_suffix=cpp parse=NONE dbms=v8 iname=
lt; 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=. 
lt;
        ${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

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} 
lt;

.cc.o:
        ${CPP} ${CPPFLAGS} ${INCLUDE} 
lt;

.pc.o:
        ${PROC} SYS_INCLUDE=/usr/include include=${ORAINC} code=CPP cpp_suffix=cpp parse=NONE dbms=v8 iname=
lt; 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=. 
lt;
        ${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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文