0

我在 .pem 文件中有一个 rsa 2048 位公钥,我使用这个命令 'openssl rsa -inform PEM -in rsa_public_key.pem -pubin -text' 来获取模块和指数。但是我不知道如何将它传递给具有 rsa_context 格式的 polarssl 函数。

const char RSA_E[] = {0x01, 0x00, 0x01};
const char RSA_N[256] =
    {
0xa9, 0x71, 0x0b, 0x68, 0x16, 0xb1, 0x1c, 0x26, 0x62, 0x02, 0xee, 0xc6, 0x7b, 0x00,
0x1e, 0x1e, 0x79, 0x22, 0x86, 0x12, 0x9b, 0x90, 0x96, 0x8c, 0x90, 0x71, 0x90, 0x29, 0xb8,
0x6c, 0x72, 0x69, 0x2e, 0x9b, 0xb1, 0xb5, 0x28, 0x65, 0x16, 0x26, 0xae, 0xcb, 0x62, 0xc9,
0x67, 0xe1, 0x52, 0x88, 0xae, 0x75, 0x52, 0x16, 0x1b, 0xe1, 0xc7, 0xe5, 0x05, 0x69, 0xc9,
0xa2, 0x9b, 0x98, 0x9b, 0xb5, 0x10, 0x1b, 0xc5, 0xba, 0xc6, 0x15, 0x61, 0x69, 0xaa, 0x12,
0x26, 0x1e, 0xc7, 0x62, 0x6c, 0x66, 0x60, 0x0b, 0x18, 0x2c, 0x2e, 0x91, 0x22, 0x20, 0x67,
0x07, 0x96, 0x8e, 0x86, 0x79, 0xb2, 0xa9, 0x61, 0x75, 0x2a, 0x62, 0x96, 0x8b, 0x8b, 0x56,
0x2b, 0x06, 0x7e, 0xbb, 0x2e, 0xb9, 0xb8, 0x82, 0xe9, 0xbe, 0x6c, 0x81, 0x26, 0x59, 0x12,
0x99, 0x75, 0x1b, 0xc5, 0x60, 0x2c, 0x2e, 0x91, 0xb9, 0x59, 0x29, 0x6a, 0x6c, 0x61, 0xaa,
0x29, 0x6b, 0xcb, 0xa8, 0x06, 0x98, 0x51, 0x29, 0x86, 0xc9, 0x78, 0x61, 0xbc, 0x12, 0x61,
0x66, 0xbc, 0x6a, 0x89, 0x80, 0x18, 0x62, 0xc1, 0xc7, 0xaa, 0x09, 0x59, 0x26, 0x72, 0xe1,
0x2a, 0x76, 0x57, 0x9b, 0x8b, 0x28, 0x16, 0x62, 0x22, 0x6a, 0x89, 0x61, 0xc7, 0x11, 0x82,
0x19, 0x81, 0x61, 0x91, 0x88, 0xc1, 0x05, 0xb1, 0x26, 0xa2, 0x10, 0x9c, 0x51, 0x09, 0x98,
0x29, 0x95, 0x0c, 0x25, 0x7c, 0x81, 0x5a, 0x07, 0x99, 0x7a, 0x2e, 0x58, 0x16, 0xea, 0x99,
0x28, 0x70, 0x22, 0x6e, 0xb1, 0x9c, 0xa6, 0x1e, 0x97, 0x25, 0xa9, 0x18, 0x29, 0x6a, 0xe8,
0xc2, 0x19, 0xa2, 0xac, 0x71, 0x97, 0x12, 0x96, 0xbe, 0x29, 0xaa, 0xcb, 0x22, 0x52, 0x81,
0xb9, 0x71, 0xb1, 0x8e, 0x92, 0x8b, 0x8e, 0x72, 0x26, 0x56, 0x52, 0x99, 0x8e, 0x15, 0xe6,
0x6e, 0xb1
    };



static char* _get_rsa_pub_key(void) {
int i, length;
char* xor_content;
length = sizeof(RSA_E);
xor_content = (char*) malloc(length + 1);
for (i = 0; i < length; i++) {
    xor_content[i] = (RSA_E[i]) ^ MASK;
}
xor_content[i] = '\0';
return xor_content;
}

static int _check_key(const unsigned char * rsa_encrypted, unsigned char *      rsa_decrypted, int* out_len) {
int rst;
size_t len;
rsa_context rsa;
char* rsa_pub_key = NULL;

rsa_pub_key = _get_rsa_pub_key();
rsa_init(&rsa, RSA_PKCS_V15, 0);
rsa.len = KEY_LEN;
mpi_read_string(&rsa.N, 16, RSA_N);
mpi_read_string(&rsa.E, 16, rsa_pub_key);
rst = rsa_pkcs1_decrypt(&rsa, NULL, NULL, RSA_PUBLIC, &len, rsa_encrypted, rsa_decrypted, *out_len);
if (0 != rst)
    rst = -1;
rsa_free(&rsa);
*out_len = len;
free(rsa_pub_key);
return rst;

}

它总是什么都不返回。我认为问题在于 rsa 公钥的格式,但我不知道如何解决。

4

1 回答 1

1

您可能应该考虑使用更新版本的 PolarSSL(现在称为 mbed TLS)。

你为什么不使用mbedtls_pk_parse_public_keyfile(),这是为了这个特定目的(将公共密钥文件加载到上下文中)?

于 2016-09-14T21:06:46.037 回答