Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simple encrypt/decrypt with QCA-BLOWFISH



  • Hi there,

    i would like to encrypt and decrypt a String with QCA-BLOWFISH but it doesnt work, when i change it to sha my example is working does anyone knows how to do it ?

        //encrypt//
        QString ciphertext = "Hallo wie geht es dir";
        QString password = "!WJKLAS";
    
        QCA::init();
    
        QCA::InitializationVector iv(16);
        iv.fill('\0');
    
        if (!QCA::isSupported("sha256"))  //blowfish-cbc etc. is supported
            throw std::runtime_error("QCA_OSSL_MISSING");
    
        QCA::SecureArray pw(password.toLocal8Bit());
        QCA::SymmetricKey key(QCA::Hash("sha256").hash(pw));  //blowfish does not work
    
     //blowfish does not work
        QCA::Cipher *cipher = new QCA::Cipher("aes256", QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Encode, key, iv);
        if (!cipher)
        {
            qDebug() << "ERROR no cipher object";
        }
    
        QCA::SecureArray encrypted = cipher->process(QCA::SecureArray(ciphertext.toLatin1()));
        bool ok = cipher->ok();
        delete cipher;
    
        QByteArray cryptedText;
        if (ok) {
            qDebug() << encrypted.toByteArray();
            cryptedText = encrypted.toByteArray();
        }
        else
            qDebug() << QByteArray();
    
    
        //decrypt//
        QCA::init();
    
        QCA::InitializationVector iv2(16);
        iv.fill('\0');
    
        if (!QCA::isSupported("sha256"))    //blowfish-cbc etc. is supported
            throw std::runtime_error("QCA_OSSL_MISSING");
    
        QCA::SecureArray pw2(password.toLocal8Bit());
        QCA::SymmetricKey key2(QCA::Hash("sha256").hash(pw));  //blowfish does not work
    
     //blowfish does not work
        QCA::Cipher *cipher2 = new QCA::Cipher("aes256", QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Decode, key, iv);
        if (!cipher)
        {
            qDebug() << "ERROR no cipher object";
        }
    
        QCA::SecureArray plain = cipher2->process(QCA::SecureArray(cryptedText));
        bool ok2 = cipher2->ok();
        delete cipher2;
    
        if (ok2)
            qDebug() << QString::fromUtf8(plain.data());
        else
        {
            qDebug()<<"Error in chipher";
            qDebug() <<  QString();
        }
    


  • @David_001 said in Simple encrypt/decrypt with QCA-BLOWFISH:

    but it doesnt work

    What does this mean?

    QCA::SymmetricKey key(QCA::Hash("sha256").hash(pw)); //blowfish does not work

    What does this mean? Is it that if you replace "sha56" by "blowfish" your machine switches off on this line?



  • i get an segmentation fault on this line, when i use blowfish, blowfish-cbc oder blowfish-cfb

        //encrypt//
        QString ciphertext = "Hallo wie geht es dir";
        QString password = "!WJKLAS";
        QByteArray temp = ciphertext.toUtf8().data();
    
        while ((temp.length() % 8) != 0)
            temp.append('\0');
    
        QCA::init();
    
        QCA::InitializationVector iv(8);
        iv.fill('\0');
    
        if (!QCA::isSupported("blowfish-cbc"))
            throw std::runtime_error("QCA_PSSL_MISSING");
    
        QCA::SecureArray pw(password.toLocal8Bit());
        QCA::SymmetricKey key(QCA::Hash("sha256").hash(pw));
    
        QCA::Cipher *cipher = new QCA::Cipher("blowfish", QCA::Cipher::CBC, QCA::Cipher::NoPadding, QCA::Encode, key.toByteArray(), iv);
        if (!cipher)
        {
            qDebug() << "ERROR no cipher object";
        }
    
        QCA::SecureArray encrypted = cipher->process(QCA::SecureArray(temp));
        bool ok = cipher->ok();
        delete cipher;
    
        QByteArray cryptedText;
        if (ok) {
            qDebug() << encrypted.toByteArray();
            cryptedText = encrypted.toByteArray();
        }
        else
            qDebug() << QByteArray();
    
    
        //decrypt//
        QCA::init();
    
        QCA::InitializationVector iv2(8);
        iv.fill('\0');
    
        if (!QCA::isSupported("blowfish-cbc"))
            throw std::runtime_error("QCA_PSSL_MISSING");
    
        QCA::SecureArray pw2(password.toLocal8Bit());
        QCA::SymmetricKey key2(QCA::Hash("sha256").hash(pw));
    
        QCA::Cipher *cipher2 = new QCA::Cipher("blowfish", QCA::Cipher::CBC, QCA::Cipher::NoPadding, QCA::Decode, key, iv);
        if (!cipher)
        {
            qDebug() << "ERROR no cipher object";
        }
    
        QCA::SecureArray plain = cipher2->process(QCA::SecureArray(cryptedText));
        bool ok2 = cipher2->ok();
        delete cipher2;
    
        if (ok2)
            qDebug() << QString::fromUtf8(plain.data());
        else
        {
            qDebug()<<"Error in chipher";
            qDebug() <<  QString();
        }
    

    This code seems to run => output:
    "\x8D\x92\x16\x93'\xA9\r\x18\xE4\xD3Jf\xD8\xA0k@\x94\x95\f4\xDCrSB"
    "Hallo wie geht es dir"

    But seems not correct


Log in to reply