Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C+;中将RSA*打印为字符串+;? 如何在C++中正确打印RSA *作为字符串?< /P>_C++_Xcode_Rsa - Fatal编程技术网

如何在C+;中将RSA*打印为字符串+;? 如何在C++中正确打印RSA *作为字符串?< /P>

如何在C+;中将RSA*打印为字符串+;? 如何在C++中正确打印RSA *作为字符串?< /P>,c++,xcode,rsa,C++,Xcode,Rsa,我正在使用OpenSSL。它似乎没有.c_str()方法,对吗 RSA_print_fp(stdout, x, 0); 如果您不喜欢该输出,可以直接访问RSA成员。例如: // RSA object 'owns' the BIGNUM. Don't destroy it. BIGNUM* n = rsa.get()->n; BIGNUM* e = rsa.get()->e; BIGNUM* d = rsa.get()->d; string s; BIO_reset(b

我正在使用
OpenSSL
。它似乎没有
.c_str()
方法,对吗

RSA_print_fp(stdout, x, 0);


如果您不喜欢该输出,可以直接访问
RSA
成员。例如:

// RSA object 'owns' the BIGNUM. Don't destroy it.
BIGNUM* n = rsa.get()->n;
BIGNUM* e = rsa.get()->e;
BIGNUM* d = rsa.get()->d;

string s;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), n);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Modulus: " << endl << s << endl;
else
    cout << "Failed to retrieve modulus" << endl;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), e);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Public exponent: " << endl << s << endl;
else
    cout << "Failed to retrieve public exponent" << endl;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), d);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Private exponent: " << endl << s << endl;
else
    cout << "Failed to retrieve private exponent" << endl;
BIGNUM* n = rsa.get()->n;
cout << "BN_bn2dec:" << endl;
cout << BN_bn2dec(n) << endl;

如果您喜欢第一个示例的格式(以冒号分隔的八位字节),那么请查看
ASN1\u bn\u print
。您可以使用它依次打印
RSA
成员
RSA\u print
RSA\u print\u fp
内部调用
ASN1\u bn\u print
。这里有一个例子


你也可以使用
BN_bn2dec
和朋友。例如:

// RSA object 'owns' the BIGNUM. Don't destroy it.
BIGNUM* n = rsa.get()->n;
BIGNUM* e = rsa.get()->e;
BIGNUM* d = rsa.get()->d;

string s;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), n);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Modulus: " << endl << s << endl;
else
    cout << "Failed to retrieve modulus" << endl;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), e);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Public exponent: " << endl << s << endl;
else
    cout << "Failed to retrieve public exponent" << endl;

BIO_reset(bio.get());
mem = NULL;

rc = BN_print(bio.get(), d);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << "Private exponent: " << endl << s << endl;
else
    cout << "Failed to retrieve private exponent" << endl;
BIGNUM* n = rsa.get()->n;
cout << "BN_bn2dec:" << endl;
cout << BN_bn2dec(n) << endl;

谢谢你的回答,但请你详细解释一下。我对此了解不够。正如文档所述,此函数将给定
RSA*
对象
x
的字符串表示形式写入给定的
文件*
stdout
具有类型
FILE*
,表示您的控制台。写入此“文件”时,数据将显示在程序标准输出中。
BIGNUM* n = rsa.get()->n;

int req = BN_num_bytes(n) + 4;
unsigned char* ptr = (unsigned char*)OPENSSL_malloc(req);

rc = ASN1_bn_print(bio.get(), "Modulus:", n, ptr, 0);
ASSERT(rc == 1);

BIO_get_mem_ptr(bio.get(), &mem);
if(mem && mem->data && mem->length)
    s.assign(mem->data, mem->length);

if(s.length())
    cout << s << endl;
else
    cout << "Failed to retrieve modulus" << endl;
Modulus:
    00:bf:49:fa:f2:30:21:42:de:91:60:f3:02:37:87:
    86:f4:eb:85:2c:95:86:42:69:6f:bc:cf:3f:27:9e:
    17:b6:44:06:5f:e7:ea:9f:e8:38:a7:5a:d5:da:9f:
    4b:26:df:78:15:d7:df:22:c0:16:12:77:f9:18:aa:
    85:01:21:3a:b0:ae:39:b8:07:cd:d4:2c:a3:0b:1c:
    df:be:09:03:09:76:5f:a1:1e:c0:00:8e:bf:b2:40:
    e2:3c:eb:d1:85:6f:7a:ab:35:c3:3a:c4:23:db:39:
    0c:d0:2b:39:b3:a5:a5:08:a2:e4:00:d5:0b:b0:87:
    62:ae:7c:a4:6c:e6:1c:d6:c9
BIGNUM* n = rsa.get()->n;
cout << "BN_bn2dec:" << endl;
cout << BN_bn2dec(n) << endl;
BN_bn2dec: 
12243739000135209335165415265342326792394455644180015057748915050940947919482400
64106225312022889083399949792870649447719809295883754268473265926413766309145047
08000254475023744104439182803717332048247971986885955229841406443364223719542912
385690146029374797423322891147172613041161904860242946608474463378207