2017-07-03 35 views
1

我想從模數和指數加載rsa密鑰。但我找不到什麼錯誤。返回碼爲零,OpenSSL主頁中的文檔沒有規定。我想使用gdb進行調試,但是它在主函數退出後進行核心轉儲。所以任何人都可以幫助我。我被困了好幾個星期。從模數和指數加載rsa密鑰,加密一個字符串,然後核心轉儲

#include <openssl/rsa.h> 
#include <openssl/engine.h> 
#include <openssl/rsa.h> 
#include <openssl/pem.h> 
#include <openssl/rand.h> 
#include <openssl/x509.h> 
#include <string.h> 

/* 
*publicexponent 
*/ 

const char* public_exp = "65537"; 
const char* priv_exp = "00992ba89ce7cafcc2213192ca6d7cee60cae934a7fb50f394892c62c09e4dae53c362960ceff295be188c 1e2bf7b9949be539bbab716906b35976e9c2104eace225f51c3e79138c9a49855b638ddea9bc01b028bac45 7632068e740c7f7a5661dd7f6e8db64e2fd212857485f863244cc4f8cf3596b50773c08357a7c040863fce8 e506c5f52553d544f762caba378f1202798957be8e4182722daee062f5105aea2a1930f01baf54753051534 a6db6fd409d439381e003a591dea3db456ce30970d58e6d4102ac09dcbb2c1983b9f295e2e45bf090dc6f8a d0813857aae51ae80abebde5b027b4537e67fa280517c6f70e605b6639dfc74c3c69066e33d56401"; 
const char *mod = "00ba015fe5f824e84e77b4f4f9da8ab844467acc8c2e6a538335d0b26b52b82f84acffbdbd641b0bd7d7ab dcbf0f6ef19b0729375a7485d8367ea503d661610f080efc717b95d4765019a6c4c45028565865e947c63a8 6d4044eaa5bc16cd2d4ada4911e5ea1f4c6de8e31de1b3d7c24ba320f584c588ae73db3943d417def0984d8 7b8a1d5f140bde6ff20de89c55ecb160a429da46f82f57c2e5ca354a673784900fbd2b3e318e200083250cb b6722c6b44fb03cc7e865a685a72e8daadd0f8b33706dd1a34be342a45f40efa4b8914052ae91d7028e8f83 c1749cf2fae280b5d91165c108a6e7729b2f685b21b7765f9ce7f9798e0c26d9b8c9d37019813515"; 

int main() { 
    RSA *rsa = RSA_new(); 
    rsa->p = NULL; 
    rsa->q = NULL; 
    rsa->dmp1 = NULL; 
    rsa->dmp1 = NULL; 
    rsa->iqmp = NULL; 

    BIGNUM *public_exp_bn = BN_new(); 
    BN_dec2bn(&public_exp_bn, public_exp); 
    rsa->e = public_exp_bn; 

    BIGNUM *priv_exp_bn = BN_new(); 
    BN_hex2bn(&priv_exp_bn, priv_exp); 
    rsa->d = priv_exp_bn; 

    BIGNUM *mod_bn = BN_new(); 
    BN_hex2bn(&mod_bn, mod); 
    rsa->n = mod_bn; 

    unsigned char *plain_txt = (unsigned char*)"hello world!"; 
    unsigned char cipher[128] = {0}; 

    int cipher_len = RSA_public_encrypt(strlen((char*)plain_txt), plain_txt, cipher,  rsa, RSA_PKCS1_PADDING); 
    RSA_free(rsa); 
} 

這裏是gdb的錯誤:

(gdb) 
*** stack smashing detected ***: /mnt/c/Users/rq868/Desktop/openssl/a.out terminated 

Program received signal SIGABRT, Aborted. 
0x00007ffffec15428 in __GI_raise ([email protected]=6) at ../sysdeps/unix/sysv/linux/raise.c:54 
54  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. 
+0

您是否在GDB中設置了斷點並逐步完成它們,而不是一次全部運行整個程序? –

+0

@CharlesAddis是的,我相信沒有犯錯誤的句子。 – Andy

+0

也許這可以幫助你:[Stack Smashing](https://stackoverflow.com/questions/1345670/stack-smashing-detected) – woz

回答

2

加密數據的大小將模數的大小,你的情況這將是256個字節,但變量的大小傳遞給RSA_public_encrypt收集加密輸出只有128字節,這可能會緩衝溢出。

你還需要檢查你的mod和exp數據,因爲它們有空格。

+0

這幫助我,謝謝。 – Andy

+1

你應該解釋爲什麼尺寸受到限制,以及填充如何影響消息長度。 – jww