c++qtencryptioncryptographybotan

What do I wrong? Botan & QT & SHELL decrypting


first i create my singing keys: (Shell/Debian 9)

>openssl genrsa -out ./priv.rsa 2048

>openssl pkcs8 -topk8 -inform PEM -outform PEM -in ./priv.rsa -out priv.pem

>openssl rsa -in ./priv.pem -pubout -out pub.pem

Then i ceate my encrypt.txt with (Shell)

> echo "ęśłżół">encrypt.txt

and crypt it with my public Rsa key (Shell)

> openssl rsautl -encrypt -inkey ./pub.pem -pubin -in ./encrypt.txt -out
> ./encrypt.dat

.. and than comes Botan (dowloaded yesterday) with QT (5.9.1):

...

using namespace Botan;

using std::string;

QFile file,file2;

// Reading private key
file.setFileName(".../priv.pem");
file.open(QIODevice::ReadOnly);
QByteArray f = file.readAll();
file.close();

//reading encrypted file
file2.setFileName("...../encrypt.dat");
file2.open(QIODevice::ReadOnly);
QByteArray f2 = file2.readAll();
file2.close();
    enter code here
std::vector<uint8_t> ct;
for(QByteArray::Iterator it = f2.begin();it!=f2.end();it++)
{
    ct.push_back((uint8_t)(*it));
}

string password=ui->lineEdit_2->text().toStdString().c_str();
std::unique_ptr<Botan::RandomNumberGenerator> rng(new Botan::AutoSeeded_RNG);

DataSource_Memory keyData2( f.toStdString().c_str() );

std::unique_ptr<Private_Key> kp = PKCS8::load_key(keyData2,pass);
PK_Decryptor_EME dec(*kp,*rng.get(), "Raw");       
secure_vector<uint8_t> ct4=dec.decrypt(ct);

QByteArray aaa;

for(secure_vector<uint8_t>::iterator it=ct4.begin();it!=ct4.end();it++)
{
    aaa+=(uint8_t)(*it);
}

QFile fileX(".../encryptE.txt");
fileX.open(QIODevice::WriteOnly);
fileX.write(aaa);
fileX.close();

...and my Out file contains (and is 255 byte wide):

^B4�����x��^V�&��ߵݹ�*S�^T�㓠K��7�J CF^U^B^[��^Q�� =^H�+�7Y^^^U�^^O\�v�����bdK^N^Ev^QI=����)�)��n^KПV����Y�-23^^�5]���^\�լ1^U�9n�z萘Å^A��Vr��8�@^C�^S����o��0����S[x� ��2 �^P4�^L�p��i���t^D��� ��^Z��J^K�^G^Z^\�4\^D^Z^Ew^Tx�S�ٛ��i�5�^D��A��Ƨ�o�zÚ��#���^L�^G���Ŋ^FXIu��^@ęśłżół

My questions: 1. ... i see my string: ęśłżół .... but what do I wrong ? - decodedfile isn't orginal one?

  1. I'is allowed to my to use RAW type only like (FOR THIS FILE: encrpted.dat):

    PK_Decryptor_EME dec(*kp,*rng.get(), "Raw");

if i choose : PKCS1v15” || “EME-PKCS1-v1_5” “OAEP” || “EME-OAEP” || “EME1” || “EME1(SHA-1)” || “EME1(SHA-256)”

i become error:

terminate called after throwing an instance of 'Botan::Decoding_Error' what(): Invalid argument Decoding error: Invalid public key ciphertext, cannot decrypt


Solution

  • OpenSSL defaults to the less secure PKCS#1 v1.5 padding. To use the same unpadding you should probably use "PKCS1v15" in Botan.

    Beware that PKCS#1 v1.5 is succeptible to the Bleichenbacher (padding oracle) attack, so the use of PKCS#1 v1.5 padding should be used with care (i.e. not in automated systems that can act as a padding oracle).

    Using OAEP is recommended.