0
我的C程序使用RSA_genarate_key()
函數創建RSA公鑰和私鑰。然後使用i2d_RSA_PUBKEY()
將公鑰轉換爲DER格式後保存在文件中。OpenSSL d2i_RSA_PUBKEY導致分段錯誤
文件與公鑰似乎是確定當我嘗試讀回,並用它來加密
openssl rsa -in public.der -inform DER -pubin -text //shows up as below
Public-Key: (2048 bit)
Modulus:
00:b5:99:9a:d3:7e:....
.......39:86:6b:ae:29
Exponent: 3 (0x3)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIDANBgkqhkiG9.............
....................+1Y5lSk5hmuu
KQIBAw==
-----END PUBLIC KEY-----
問題出現。下面是代碼:
File* fp;
RSA *pub=NULL;
const unsigned char* public_key_buf;
int size;
fp = fopen("public.der","r");
fseek (fp , 0 , SEEK_END);
size = ftell (fp);
size = sizeof(char)*size;
rewind (fp);
public_key_buf = malloc(size*sizeof(char));
fread(&public_key_buf,sizeof(char),size,fp);
pub = d2i_RSA_PUBKEY(0, &public_key_buf, size);
fclose(fp);
正在運行的程序會導致Segmentation fault
這裏是GDB說什麼:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b0fe85 in ASN1_get_object()
from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
(gdb) bt
#0 0x00007ffff7b0fe85 in ASN1_get_object() from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#1 0x00007ffff7b05f30 in ??() from /lib/x86_64-linux- gnu/libcrypto.so.1.0.0
#2 0x00007ffff7b071ad in ASN1_item_ex_d2i() from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#3 0x00007ffff7b07914 in ASN1_item_d2i() from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#4 0x00007ffff7affa3e in d2i_PUBKEY() from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#5 0x00007ffff7affb14 in d2i_RSA_PUBKEY() from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#6 0x0000000000400daa in main() at rsa_1.c:99
(gdb) p pub
$1 = (RSA *) 0x0
(gdb) p public_key_buf
$2 = (const unsigned char **) 0x603250
(gdb) p *public_key_buf
$3 = (const unsigned char *) 0x9060d3020018230 <error: Cannot access memory at address 0x9060d3020018230>
(gdb) p size
$4 = 293
任何幫助,請。
你的代碼不會在C++編譯器上編譯,那你爲什麼要標記C++? –
對不起,「mea culpa」,再也不會。但是,有什麼建議呢? – michal