C语言 从模数和指数加载 rsa 密钥,加密字符串,然后进行核心转储



我想从模数和指数加载RSA密钥。但是我找不到什么错误。返回码为零,OpenSSL 主页文档中没有规范。我想使用 gdb 进行调试,但它在主函数退出后进行核心转储。所以谁能帮我。我已经被困了几个星期了。

#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 (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.

加密数据的大小将是模数的大小,在您的情况下,它将是 256 字节,但传递给RSA_public_encrypt用于收集加密输出的变量大小仅为 128 字节,这可能会缓冲溢出。

您还需要查看您的模组和 exp 数据,因为它们有空格。

最新更新