简体   繁体   English

Botan 使用 RSA 密钥对解密数据失败

[英]Botan failed to decrypt data using RSA key pair

I'm making a program that has a client and server, and to send data from the client to the server it uses encryption.我正在制作一个具有客户端和服务器的程序,并将数据从客户端发送到它使用加密的服务器。 The data is encrypted with a key and the key gets encrypted with the server's public RSA key, but the server fails to decrypt the key giving me the message Invalid public key ciphertext, cannot decrypt but sometimes it does work.数据使用密钥加密,密钥使用服务器的公共 RSA 密钥加密,但服务器无法解密密钥给我消息Invalid public key ciphertext, cannot decrypt但有时它确实有效。 The RSA keys are saved as plain text and sent to the client on request (public key only). RSA 密钥保存为纯文本并根据请求发送给客户端(仅限公钥)。 The private key is used only by the server.私钥仅供服务器使用。 To encrypt the key the client does the following:要加密密钥,客户端执行以下操作:

Botan::AutoSeeded_RNG rng;
Botan::DataSource_Memory DSMPublicServer(serverPublicKey); // serverPublicKey = key from server
Botan::X509_PublicKey *X509Key_publicServer = Botan::X509::load_key(DSMPublicServer); // Load the key
std::unique_ptr <Botan::Public_Key> publicKeyServer(X509Key_publicServer); // The key used to encrypt
Botan::PK_Encryptor_EME encKey(*publicKeyServer, rng, "EME-PKCS1-v1_5");
std::vector <uint8_t> encKey_t = encKey.encrypt(key, rng);

And the server tries to decrypt it as follows:并且服务器尝试如下解密它:

Botan::AutoSeeded_RNG rngTest;
Botan::DataSource_Memory DSMPrivate(this->myKeyString); // myKeyString = server private key
Botan::PKCS8_PrivateKey *PKCS8Key_Private = Botan::PKCS8::load_key(DSMPrivate, rngTest) // Load the key
std::unique_ptr <Botan::Private_Key> privateKey(PKCS8Key_Private);
Botan::PK_Decryptor_EME dec(*privateKey, rngTest, "EME-PKCS1-v1_5"); // Decryptor
std::vector <uint8_t> dec_t = Botan::unlock(dec.decrypt(this->key)); // This throws errors
this->key.clear(); // The key used on the rest of the data
std::copy(dec_t.begin(), dec_t.end(), std::back_inserter(this->key)); // Put the decrypted key back

If I test this on the server with some data it works fine, but it seems that either data is lost during transmission, but it uses TCP so that should not be it or that the encryption does not run properly every time since it does work sometimes.如果我在服务器上用一些数据测试它,它工作正常,但似乎数据在传输过程中丢失,但它使用 TCP,所以不应该是这样,或者加密每次都不能正常运行,因为它有时确实有效. Is there a way to validate the encrypted data so I can test it before sending or how could I fix the issue?有没有办法验证加密数据,以便我可以在发送之前对其进行测试,或者我该如何解决这个问题?

Ok, so after a lot of different attempts I've managed to get it to work.好的,所以经过很多不同的尝试后,我设法让它工作。 The thing I've changed is how the server gets the data, at first this was in a long message after the function request, but now the server requests it in parts from the client.我改变的是服务器如何获取数据,起初这是在函数请求之后的一条长消息中,但现在服务器部分地从客户端请求它。 The small packets seem to work fine and I don't have any more problems with the encryption.小数据包似乎工作正常,我对加密没有任何问题。 Why it did not work with the original code I don't know but from all the tests I've done, it could be in several places, on the client when encrypting or building the message, on the server when disassembling the message.为什么它不适用于我不知道的原始代码,但从我所做的所有测试来看,它可能在多个地方,在加密或构建消息时在客户端上,在反汇编消息时在服务器上。
--Edit-- - 编辑 -
Since more info was requested here it is.由于这里要求提供更多信息。
At first, the client sends data to the server in a long string with encrypted data ie somefilter|function|data where the filter is used for ZMQ, the function is a function call to the server, and data contained plain text, text encrypted using the key of the client and the key encrypted using the RSA public key of the server.首先,客户端将数据以带有加密数据的长字符串形式发送到服务器,即somefilter|function|data其中过滤器用于 ZMQ,该函数是对服务器的函数调用,数据包含纯文本,使用加密的文本客户端的密钥和使用服务器的 RSA 公钥加密的密钥。 The server then disassembled the message using the lengths of those parts which were also in the message, but this did not work properly.然后服务器使用同样在消息中的那些部分的长度来分解消息,但这不能正常工作。 I have not been able to figure out where it goes wrong exactly.我一直无法弄清楚它到底哪里出了问题。 Instead, I overhauled the server and client to send small parts, the client would send a function request to the server and one part of data to distinguish the client, after this the server would send messages for the other info that was previously part of the long message string, so now all the data comes in its own small packed instead of a large message.相反,我对服务器和客户端进行了大修以发送小部分,客户端将向服务器发送功能请求和一部分数据以区分客户端,之后服务器将发送消息以获取以前属于的其他信息的一部分长消息字符串,所以现在所有数据都以自己的小包装而不是大消息的形式出现。 This solution, however, was only possible because I can change the server and client, if this was not possible the solution would not work.然而,这个解决方案之所以可行,是因为我可以更改服务器和客户端,如果这不可能,解决方案将无法工作。 Also, this is not an answer to the question I asked but the issue with Botan failing to decrypt has been resolved ever since I overhauled the server and client, so I'm not sure as to what was going on with that.此外,这不是我提出的问题的答案,但自从我对服务器和客户端进行大修以来,Botan 无法解密的问题已经得到解决,所以我不确定这是怎么回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM