POCO C ++加密/解密

问题描述 投票:0回答:1

我正在使用POCO C ++ API,并使用CRYPTO模块进行加密和解密。我遇到解密无法正常工作并重新调整以下错误的问题:

   RSA_padding_check_PKCS1_type_2:pkcs decoding error; error:04065072:rsa 
   routines:rsa_ossl_private_decrypt:padding check failed"

我正在使用RSA密钥/对。密钥对的生成

void Support::GenerateKeyPair(std::string& PublicKey, std::string& PrivateKey)
{   
    Poco::Crypto::RSAKey key(Poco::Crypto::RSAKey::KL_1024, Poco::Crypto::RSAKey::EXP_SMALL);

    std::ostringstream strPub;
    std::ostringstream strPriv;

    key.save(&strPub, &strPriv); // EmailEncryptionPassphrase);
    PublicKey = strPub.str();
    PrivateKey = strPriv.str();             
}

这些密钥放置在文件中-并将被显式检索为用于加密/解密活动的字符串-即,在不直接使用文件的情况下进行加密/解密

我的加密操作如下:

    std::istringstream iPub(PublicKey);
    Poco::Crypto::RSAKey MulaRSAKey(&iPub);

    Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
    Poco::Crypto::Cipher* MulaCipherImpl = factory.createCipher(MulaRSAKey);


    //encrypt using RSA (keys prepped with RSA)
    EncryptedData = MulaCipherImpl->encryptString(Data, Poco::Crypto::Cipher::Encoding::ENC_NONE);

同样,公钥是从配置文件中检索的字符串。此操作不会失败-加密似乎正在起作用。

我以如下方式解密数据:

   std::istringstream iPub(Keys.PublicKey);
   std::istringstream iPriv(Keys.PrivateKey);
   Poco::Crypto::RSAKey MulaRSAKey(&iPub, &iPriv); // EmailEncryptionPassphrase);
   Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
   Poco::Crypto::Cipher* MulaCipherImpl = factory.createCipher(MulaRSAKey);

   DecryptedData = std::make_shared<std::string>(MulaCipherImpl->decryptString(EncryptData, 
         Poco::Crypto::Cipher::Encoding::ENC_NONE));

这是发生故障的地方。由于上面提到的填充错误,解密失败。解密操作还使用配置文件中密钥对的字符串。

我不确定密钥对是否有效,因此我使用RSA密钥(在线)测试器来验证密钥对-测试可以加密和解密。因此,看起来该密钥是有效的。

我能够将编码从Poco :: Crypto :: Cipher :: Encoding :: ENC_NONE更改为Poco :: Crypto :: Cipher :: Encoding :: ENC_BINHEX并且解密确实通过了-生成了十六进制输出-但确实通过。令人困惑的方面是加密操作仍在使用Poco :: Crypto :: Cipher :: Encoding :: ENC_NONE。我的理解是加密和解密应使用相同的编码。但是,这并不能阐明为什么基于NONE的编码在解密时会失败。

如果任何人都可以弄清配置错误,将有很大帮助。

thx彼得

c++ rsa poco-libraries
1个回答
0
投票

好的-问题是静音。意识到加密的数据(要取消加密)无效。在用于将数据迁移到远程服务器的JSON操作中进行了修改。

Peter

© www.soinside.com 2019 - 2024. All rights reserved.