R
R
Robotex2011-04-24 21:21:48
Qt
Robotex, 2011-04-24 21:21:48

Qt4 QCA: encrypting large amounts of information using RSA?

I am writing a qt4 application that needs to use symmetric encryption. But here's the problem - when encrypting, the data is cut off by the length of the key (i.e. if the key is 1024 bits, then we will get only 128 bytes of information at the output, and after decryption we will get only a part). What is the problem? How to encrypt large amounts of data?

bool CCryptor::generateKeys(int size)
{
    if(init())
    {
        QCA::PrivateKey seckey = QCA::KeyGenerator().createRSA(size);

        if(seckey.isNull())
        {
            std::cout << "Failed to make private RSA key" << std::endl;
            return false;
        }

        QCA::PublicKey pubkey = seckey.toPublicKey();

        privateKey = seckey;
        publicKey = pubkey;

        return true;
    }
    else
        return false;
}

QByteArray CCryptor::dataEnrypt(QByteArray data)
{
    // check if the key can encrypt
    if(!publicKey.canEncrypt())
    {
        std::cout << "Error: this kind of key cannot encrypt" << std::endl;
        return QByteArray();
    }

    QCA::SecureArray arg = data;

    // encrypt some data - note that only the public key is required
    // you must also choose the algorithm to be used
    QCA::SecureArray result = publicKey.encrypt(arg, QCA::EME_PKCS1_OAEP);

    if(result.isEmpty()) {
        std::cout << "Error encrypting" << std::endl;
        return QByteArray();
    }

    return result.toByteArray();
}

QByteArray CCryptor::dataDecrypt(QByteArray data)
{
    QCA::SecureArray encrypt = data;
    QCA::SecureArray decrypt;
    if(0 == privateKey.decrypt(encrypt, &decrypt, QCA::EME_PKCS1_OAEP))
    {
        std::cout << "Error decrypting.\n";
        return QByteArray();
    }

    return decrypt.data();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WikiLeaks, 2011-04-25
@WikiLeaks

I will not be specific, because I have never worked with encryption in Qt (I just “play around” with C ++).
But most likely this behavior is due to the fact that, due to the resource intensity, RSA is not used to encrypt large amounts of information.
Usually, large data arrays are encrypted using symmetric algorithms like AES (it should be in QCA), while the keys are already encrypted using RSA.
This is exactly how, if I'm not mistaken, almost all popular protocols (SSL for example) using asymmetric cryptography are arranged.
I assume this is exactly what you need to do in your case.

R
Robotex, 2011-04-25
@Robotex

I found out that QCA supports OpenPGP (GnuPG), the use of which will probably be more secure and convenient, but I did not find documentation and examples anywhere (only a few lines in the official one). Can someone help?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question