加载公钥以创建用于公共加密的 rsa 对象 [英] Load public key to create rsa object for public encryption

查看:173
本文介绍了加载公钥以创建用于公共加密的 rsa 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从生成的公钥加载 rsa 对象.我使用 PEM_write_bio_RSAPublicKey 生成公钥字符串.然后我使用 PEM_read_bio_RSA_PUBKEY 从公钥字符串加载 rsa 对象.问题是 rsa 对象为空.据我所知,生成的字符串看起来不错.有什么想法吗?

I am trying to load a rsa object from a generated public key. I used PEM_write_bio_RSAPublicKey to generate the public key string. Then I used PEM_read_bio_RSA_PUBKEY to load the rsa object from the public key string. The problem is the rsa object is null. The generated string looks okay as far as I can tell. Any ideas?

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAxIReUspesPy6a4CPBjt/4Jt+H13q9MekMiutzNKdNO1uuwqcdqDX
pKPeTKXyUH6oCyRdUxkk6IVXGlBlxtW7OsxaYWhpfl9z3CCERCEpFmzN++dvlK2v
mckFL66e9q6Y+HwgyP1LJqrszeqlg2d29TCVKfD/UURVNmc/nPPjs9nO+IDhh7+P
NTQ2OqGBq8ghwVL5ZZyW3yVO5OAbRB6pjKBe9+j4B2TGnD5JO9Nu0jlFANZOKFJu
HDVE3XuTvOkuzL2i8Lwp4Myk42tbIgcCe4G58vKFddL651rWhg4hN3fRSx5YtDnQ
r5cgfNBOAww58S8lwXgU8lvzvEoNV+WMgwIDAQAB
-----END RSA PUBLIC KEY-----

gcc test_public_private_key.c -lcrypto -o test

gcc test_public_private_key.c -lcrypto -o test

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>

char* get_public_key() {

        RSA* rsa = RSA_new();
        int kbits = 2048;
        BIGNUM* bne = BN_new();
        BN_set_word(bne, RSA_F4);

        int status = RSA_generate_key_ex(rsa, kbits, bne, NULL);
        if(status == 0) {
                fprintf(stderr, "rsa key generation failed\n");
        }


        BIO *bio = BIO_new(BIO_s_mem());
        PEM_write_bio_RSAPublicKey(bio, rsa);

        int length = BIO_pending(bio);

        char* pem_key = malloc(length+1);
        memset(pem_key, '\0', length+1);

        BIO_read(bio, pem_key, length);

        return pem_key;

}

int main(int argc, char* argv[]) {

        char* public_key = get_public_key();

        printf("%s", public_key);

        BIO* keybio = BIO_new_mem_buf(public_key, -1);

        if (keybio == NULL) {
                fprintf(stderr, "failed to create key BIO");
        }

        printf("keybio: %p\n", keybio);
        RSA* rsa = PEM_read_bio_RSA_PUBKEY(keybio, NULL, NULL, NULL);
        printf("rsa result %p\n", rsa);
        BIO_free(keybio);

        free(public_key);

        return 0;
}

推荐答案

我认为最好补充一点,这不起作用的原因是因为 PEM_read_RSA_PUBKEY 需要 SubjectPublicKeyInfo 结构,以 BEGIN PUBLIC KEY 开头,而 PEM_read_RSAPublicKey 需要一个 RSAPublicKey 结构,它以 BEGIN RSA PUBLIC KEY 开头.
PEM_write_bio_RSAPublicKey 生成的后者对应于第二个函数,PEM_read_RSAPublicKey.

I thought it would be good to add that the reason this didn't work is because PEM_read_RSA_PUBKEY expects a SubjectPublicKeyInfo structure, which starts with BEGIN PUBLIC KEY, while PEM_read_RSAPublicKey expects a RSAPublicKey structure, which starts with BEGIN RSA PUBLIC KEY.
PEM_write_bio_RSAPublicKey generated the latter which corresponds to the second function, PEM_read_RSAPublicKey.

这篇关于加载公钥以创建用于公共加密的 rsa 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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