dtg78700 2015-02-08 21:06
浏览 176

C ++中的RSA解密,PHP中的加密

I am working on an application which generates RSA encrypted session keys and stores them in a database. Later theses keys are transfered to a C++ application via Javascript. Therefore I want to use the OpenSSL library. I generated a 2048 bit key pair with openssl, which is used in both methods.

My PHP functions works like this:

function encrypt_with_public_key($input, $key)
{   
    openssl_public_encrypt($input, $crypttext, $key, OPENSSL_PKCS1_OAEP_PADDING); 
    return $crypttext;          
}

and

$fp = fopen("public.pem","r");
$public_pem = fread($fp,8192);
fclose($fp);

$public_key = openssl_get_publickey($public_pem);

$sessionKey = ...;
$encSessionKey = encrypt_with_public_key($sessionKey, $public_key);

I tested this part successfully. The part I have trouble with, is the C++ part. I use MS Visual Studio 2013. Edit: added hex encoding (using Crypto++)

#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
...
string decrypted, encoded;
decrypted.clear();

char privateKey[] =
    "-----BEGIN RSA PRIVATE KEY-----
"\
    ...

RSA *rsa = NULL;
BIO *keybio;
keybio = BIO_new_mem_buf(privateKey, strlen(privateKey));

rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);

StringSource ss2(input, true,
    new HexEncoder(
        new StringSink(encoded)
    )
);

RSA_private_decrypt(encoded.length(), (unsigned char *)encoded.data(), (unsigned char *)decrypted.data(), rsa, RSA_PKCS1_OAEP_PADDING);

FBLOG_INFO("", ERR_error_string(ERR_get_error(), NULL));

return decrypted;

Note that the private key is not read from a file.

OpenSSL returns the following error: 0406506C: lib(4): func(101): reason(108).

It means afaik that my input data is longer than the modulus length (please correct me if I'm wrong). Anyone who knows how to handle this? I thought such problems are solved through the padding parameters.

The input data is the direct output of the php encrypt function (no base64 oder anything).

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示