gpt4 book ai didi

c++ - 如何从 RSA_private_encrypt 获取人类可读的 std::string

转载 作者:行者123 更新时间:2023-11-30 03:49:49 26 4
gpt4 key购买 nike

我用 RSA 加密了我的字符串,但它现在是一个 unsigned char *。如何创建可以为用户输出的人类可读的 std::string?我想在亚马逊签名的网址中使用它。这是来自 GitHub 的代码的主要内容

unsigned char* RSA_SHA1_Sign(std::string policy, RSA *privateKey) throw(std::runtime_error)
{

//sha1 digest the data
unsigned char hash[SHA_DIGEST_LENGTH] = {'0'};
SHA1((const unsigned char *)policy.c_str(), policy.length(), hash);

// Sign the data
int rsaSize = RSA_size(privateKey);
// std::unique_ptr<unsigned char[]> signedData(new unsigned char[size]);//if c++11 available
unsigned char *signedData = (unsigned char *)malloc(sizeof(unsigned char) * rsaSize);
unsigned int signedSize = 0;

//use RSA_sign instead of RSA_private_encrypt
if(!RSA_sign(NID_sha1, hash, SHA_DIGEST_LENGTH, signedData, &signedSize, privateKey)){
throw std::runtime_error("Failed to sign");
}

return signedData;
}

std::string base64Encode(unsigned char *signedData)
{
//prepare
BIO *b64 = BIO_new(BIO_f_base64());
BIO *bmem = BIO_new(BIO_s_mem());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
b64 = BIO_push(b64, bmem);

//write
BIO_write(b64, signedData, 256);
BIO_flush(b64);


//create string
BUF_MEM *bptr;
BIO_get_mem_ptr(b64, &bptr);
std::string base64String(bptr->data);

BIO_free_all(b64);
return base64String;
}
int main(int argc, const char * argv[]) {

RSA *privateKey = createRSAFromPrivateKeyFile("/path/to/privatekey");
std::string sourceString = "testing";
std::string signature = RSA_SHA1_Sign(sourceString, privateKey);
std::string encodedSignature = base64Encode(signature);
std::cout << "RESULT: " << encodedSignature << std::endl;

return 0;
}

更新:我使用了错误的符号函数。更新后,使用 base64 编码为我提供了正确的字符串。

RSA_PKCS1_PADDING PKCS #1 v1.5 padding. This function does not handle the algorithmIdentifier specified in PKCS #1. When generating or verifying PKCS #1 signatures, RSA_sign(3) and RSA_verify(3) should be used.

最佳答案

要保存所有数据,请使用此 std::string 构造函数:std::string( char *data, int size )。该大小将很有用,因为输出可能包含空字符。

要通过 url 将其发送到亚马逊,再次考虑使用 base64 编码,因为加密数据可能包含 NULL 和其他恶作剧。

关于c++ - 如何从 RSA_private_encrypt 获取人类可读的 std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32214731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com