所以,我有这样的代码:
#include <openssl/dh.h>
#include <iostream>
const char* userA_PrivateKey = "6e11 ...
const char* userA_PublicKey = "365b ...
const char* userB_PublicKey = "16db ...
const char* p = "00a7 ...
const char* g = "2";
int main()
{
DH* dh = DH_new();
BN_dec2bn(&dh->g, g);
BN_hex2bn(&dh->p, p);
BN_hex2bn(&dh->priv_key, userA_PrivateKey);
BIGNUM* pubKeyUserB = NULL;
BN_dec2bn(&pubKeyUserB, userB_PublicKey);
//Compute the shared secret
int secret_size;
unsigned char* secret;
//
int dhSize = DH_size(dh);
//
secret = reinterpret_cast<unsigned char*>(OPENSSL_malloc(sizeof(unsigned char) * dhSize));
if (0 > (secret_size = DH_compute_key(secret, pubKeyUserB, dh)))
{
std::cerr << "Error[33]!\n", -1;
}
std::cout << '\n' << secret << '\n';
return 0;
}
但是在输出中我得到这个:
如何获取ANSI字符串中的输出十六进制密钥? (我真的希望秘密在于我需要的东西,而不是垃圾之类的东西)
答案 0 :(得分:0)
所以,解决方案是
std::cout << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(my_byte_value);
(感谢@Someprogrammerdude)