C++;使用RSA_Public_encrypt的公钥加密 我目前正努力使用C++中的公钥加密技术。我正在使用openssl,但生成的字符串总是被破坏。我目前的解决方案如下: string encryptWithPublicKey(const char* pData, int iLen) { // pData: String to encrypt, iLen: length char chPublicKey[] = "---Public Key-- ..."; BIO* bio = BIO_new_mem_buf(chPublicKey, -1); RSA* rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL); int nLen = RSA_size(rsa); char* pEncode = (char*)malloc(nLen + 1); int rc = RSA_public_encrypt(iLen, (unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_NO_PADDING); cout << "From: " << pData << " To: '" << pEncode << "'"; RSA_free(rsa); CRYPTO_cleanup_all_ex_data(); std::string strName = std::string(pEncode); return strName; } string encryptWithPublicKey(const char*pData,int-iLen){//pData:string要加密,iLen:length char chPublicKey[]=“--公钥--…”; BIO*BIO=BIO_new_mem_buf(chPublicKey,-1); RSA*RSA=PEM_read_bio_RSA_PUBKEY(bio,NULL,NULL,NULL); int nLen=RSA_大小(RSA); char*pEncode=(char*)malloc(nLen+1); int rc=RSA\u public\u encrypt(iLen,(无符号字符*)pData,(无符号字符*)pEncode,RSA,RSA\u NO\u PADDING); 库特

C++;使用RSA_Public_encrypt的公钥加密 我目前正努力使用C++中的公钥加密技术。我正在使用openssl,但生成的字符串总是被破坏。我目前的解决方案如下: string encryptWithPublicKey(const char* pData, int iLen) { // pData: String to encrypt, iLen: length char chPublicKey[] = "---Public Key-- ..."; BIO* bio = BIO_new_mem_buf(chPublicKey, -1); RSA* rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL); int nLen = RSA_size(rsa); char* pEncode = (char*)malloc(nLen + 1); int rc = RSA_public_encrypt(iLen, (unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_NO_PADDING); cout << "From: " << pData << " To: '" << pEncode << "'"; RSA_free(rsa); CRYPTO_cleanup_all_ex_data(); std::string strName = std::string(pEncode); return strName; } string encryptWithPublicKey(const char*pData,int-iLen){//pData:string要加密,iLen:length char chPublicKey[]=“--公钥--…”; BIO*BIO=BIO_new_mem_buf(chPublicKey,-1); RSA*RSA=PEM_read_bio_RSA_PUBKEY(bio,NULL,NULL,NULL); int nLen=RSA_大小(RSA); char*pEncode=(char*)malloc(nLen+1); int rc=RSA\u public\u encrypt(iLen,(无符号字符*)pData,(无符号字符*)pEncode,RSA,RSA\u NO\u PADDING); 库特,c++,C++,加密数据是二进制的。您不应该打印二进制数据。在打印之前,您应该将其编码为base64或十六进制 ════════════是0xCC字节,是未初始化内存的魔法填充字节。似乎输出中没有写入任何内容,这意味着调用RSA\u public\u encrypt失败 您没有检查返回值rc int rc = RSA_public_encrypt(iLen, (unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_NO_PADDING); 添加一些

加密数据是二进制的。您不应该打印二进制数据。在打印之前,您应该将其编码为base64或十六进制

  • ════════════
    是0xCC字节,是未初始化内存的魔法填充字节。似乎输出中没有写入任何内容,这意味着调用
    RSA\u public\u encrypt
    失败

  • 您没有检查返回值
    rc

    int rc = RSA_public_encrypt(iLen, (unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_NO_PADDING);
    
    添加一些错误报告以查看确切的错误:

    if (rc < 0) {
        char buf[128];
        cerr << "RSA_public_encrypt: " << ERR_error_string(ERR_get_error(), buf) << endl;
    }
    
    if(rc<0){
    char-buf[128];
    
    CURR加密数据是二进制的,在打印出什么值之前,你可能想要BASE64编码或什么东西?你忘记了<代码>免费的密码> <代码>。无论如何,C++中你应该使用<代码>新< /COD>和<代码>删除<代码>,或者只是<代码> STD::String < /C> >如果你需要访问原始数组,请用<代码> CySTR 方法。加密任意数据是为了加密密钥。如果使用
    RSA\u NO\u PADDING
    的话,输入的大小必须正好是
    RSA\u size(RSA)
    (即密钥大小)。正常的过程是通过RSA交换临时AES密钥,然后使用AES进行加密。尽管我只会使用经过验证的HTTPS,然后就可以使用它了。