@Danil2004
Пишу на C++, использую QT (Учусь)

С++ Шифрование Crypto++ RSA?

#include <cryptopp/modes.h>
#include <cryptopp/osrng.h>
#include <cryptopp/rsa.h>
#include <cryptopp/sha.h>

class MyClass
{

		CryptoPP::AutoSeededRandomPool rng;
		CryptoPP::RSA::PrivateKey privateKey;
		CryptoPP::RSA::PublicKey publicKey;
public:
                MyClass()
                {
	CryptoPP::RSAES_OAEP_SHA_Decryptor priv( rng, 4096 );
	CryptoPP::RSAES_OAEP_SHA_Decryptor pri( rng, 2048 );
	CryptoPP::InvertibleRSAFunction params;
	params.GenerateRandomWithKeySize( rng, 1536 );
	privateKey = CryptoPP::RSA::PrivateKey(params);
	publicKey = CryptoPP::RSA::PublicKey(params);

                }
                QString encrypt(QString data)
{
	std::string plaintext = data.trimmed().toStdString();
	std::string encryptedText;
	CryptoPP::RSAES_OAEP_SHA_Encryptor e( publicKey );
	CryptoPP::StringSource( plaintext, true, new CryptoPP::PK_EncryptorFilter( rng, e, new CryptoPP::StringSink( encryptedText )));
	return QString::fromStdString(encryptedText);
}

		QString decrypt(QString encryptData)
{
	std::string encryptedText = encryptedData.toStdString();
	std::string decryptedtext;
	CryptoPP::RSAES_OAEP_SHA_Decryptor d( privateKey );
	CryptoPP::StringSource( encryptedText, true, new CryptoPP::PK_DecryptorFilter( rng, d, new CryptoPP::StringSink( decryptedtext )));
	return QString::fromStdString(decryptedtext);
}
};


Шифрует, но обратно не хочет, с ошибкой:
terminate called after throwing an instance of 'CryptoPP::InvalidArgument'
  what():  RSA/OAEP-MGF1(SHA-1): ciphertext length of 386 doesn't match the required length of 192 for this key
  • Вопрос задан
  • 1119 просмотров
Пригласить эксперта
Ответы на вопрос 1
IGHOR
@IGHOR Куратор тега Qt
Qt/C++ DEV/CTO
Можно обойтись без CryptoPP и использовать OpenSSL, вот пример https://github.com/JulyIghor/QtBitcoinTrader/blob/...
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы