我正在使用openssl库,我想从BIO文件中读取.pem文件中的公钥。我尝试过这个,但我的rsa变量仍然没有初始化:
RSA *rsa = RSA_new();
BIO *keybio = NULL;
keybio = BIO_new(BIO_s_file());
BIO_read_filename(keybio, "public.pem");
// and also tried this instead of last two lines:
// keybio = BIO_new_file("public.rem", "r");
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
当我调试我的应用程序时,它显示了类似这样的内容:
rsa { padding = ???, n = ??? , ...}
rsa->n <unable to read from memory> and so on for all rsa fields.
我的文件有效,密钥是根据PKCS#1格式生成的。我用asn1解析器解析它。
答案 0 :(得分:0)
您的代码看起来很好。试试这个输入:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1IHYYTavz9PQRxpcyO7J
m0dtiIjNUvW3coLQQKhq+wySTTN1cwm2zYTw0FSfLDPOtOBNXFwKF9wykiyHS2uU
D8vIU+T/fvlcADYTtZqdC5AoBWlSuhp0xqqtHmNUEjGa4FpRmKusL8s5/cuAfNRV
NVSxA3JCN3kYrT9Q1qBN+XbOQn+h7gPQU3ICmG7L1R/CwIsq/wwUbq+NeY0TMvz5
LM6AIS+GCV0UeJVm6UN6GDBCOHk02XuplyhkbCsNhq+HTfhHVeE1s7NcIavmgvqm
EtlIcTGemW9tXs5/REZUv+SDpR6RLUKhwuij/Ft5Pe9b7cH3wXqNmOBhJ3F/ht2C
swIDAQAB
-----END PUBLIC KEY-----
将此与您在asn1解析器中看到的内容进行比较:
$ openssl asn1parse -in public.pem
0:d=0 hl=4 l= 290 cons: SEQUENCE
4:d=1 hl=2 l= 13 cons: SEQUENCE
6:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
17:d=2 hl=2 l= 0 prim: NULL
19:d=1 hl=4 l= 271 prim: BIT STRING