Cifrado/descifrado Blowfish con guardado de resultados (Qt - qblowfish)

How can I restore a cipher text that I've got from a succesful encryption with blowfish, when i save it in the meantime?

secretKey is the hash value of a keyword.

QString clearText(textBrowser->toHtml());

QBlowfish bf(secretKey);
QByteArray encryptedBa = bf.encrypted(clearText.toUtf8());

  // I want to save the encrypted text in a file to store it.
  // Here I am "emulating" this intent:
  // I am saving with flushing an outStream, I convert it to QString before:
QString test_SavedText(encryptedBa); //This value gets saved
QString cryptedText(test_SavedText); //"Read the stored file"

QByteArray decryptedBa = bf.decrypted(cryptedText.toUtf8());

encryptedBa is working. If I directly insert this into my decrypted-call, it is getting decrypted. But when I call the decryption with my cryptedText.toUtf8(), No funciona.

Some debugging showed: The QString needs padding. So okay, in the decryption-method I added padding to its QByteArray (exactly the way it goes for encryption). But still my decryptedBa-output is empty. And still I don't understand why my QString needs padding - isn't it made from the QByteArray, that already includes padding?

preguntado el 28 de mayo de 14 a las 12:05

2 Respuestas

I realise this is time since you asked the question, but as I've just stumbled on it, you don't want to use .toUtf8() on the encrypted data, but instead, on the result después de desencriptación

Respondido 06 Oct 14, 12:10

Este código funcionó para mi

QString enc(QString data) {
    QBlowfish bf(QString("This is The key").toUtf8());
    QByteArray cipherText = bf.encrypted(data.toUtf8());
    return QString(cipherText.toHex());

QString dec(QString enc_data) {
    QBlowfish bf(QString("This is The key").toUtf8());
    QByteArray decryptedBa = bf.decrypted(QByteArray::fromHex(enc_data.toUtf8()));
    return QString::fromUtf8(decryptedBa.constData(), decryptedBa.size());

Respondido el 29 de Septiembre de 18 a las 10:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.