Когда я передаю initialText86
, приведенный ниже код работает как надо. Когда я передаю initialText87
, он не может построить StringSource ss1
, и мы встречаем исключение invalid argument
!
Как я могу закодировать строку длиной 87?
#include <string>
#include <new>
using namespace std;
#include <../pub/cryptopp/rsa.h>
#include <../pub/cryptopp/osrng.h>
#include <../pub/cryptopp/oaep.h>
#include <../pub/cryptopp/sha.h>
using namespace CryptoPP;
AutoSeededRandomPool& rng_get() {
static AutoSeededRandomPool defRng;
return defRng;
}
string rsa_encode( const string& plainText, const RSA::PublicKey& pubKey ) {
RSAES_OAEP_SHA_Encryptor rsaEnc( pubKey );
string cipherText;
StringSource ss1( reinterpret_cast< const byte* >( plainText.c_str() ), plainText.size(), true,
new PK_EncryptorFilter( rng_get(), rsaEnc,
new StringSink( cipherText )
) // PK_EncryptorFilter
); // StringSource
return move( cipherText );
}
string rsa_decode( const string& cipherText, const RSA::PrivateKey& secretKey ) {
RSAES_OAEP_SHA_Decryptor rsaDec( secretKey );
string plainText;
StringSource ss2( reinterpret_cast< const byte* >( cipherText.c_str() ), cipherText.size(), true,
new PK_DecryptorFilter( rng_get(), rsaDec,
new StringSink( plainText )
) // PK_DecryptorFilter
); // StringSource
return move( plainText );
}
static const size_t keyLength = 1024;
RSA::PrivateKey _secretKey;
RSA::PublicKey _pubKey;
bool test( const string& initialText ) {
auto cipherText = rsa_encode( initialText, _pubKey );
auto plainText = rsa_decode( cipherText, _secretKey );
return plainText == initialText;
}
int main() {
_secretKey.GenerateRandomWithKeySize(rng_get(), keyLength );
new( &_pubKey ) RSA::PublicKey( _secretKey );
string initialText87 = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
string initialText86 = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
auto testResult = test( initialText87 );
assert( testResult );
return testResult ? 0 : -1;
}