Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple encrypt/decrypt with QCA-BLOWFISH
Forum Updated to NodeBB v4.3 + New Features

Simple encrypt/decrypt with QCA-BLOWFISH

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 602 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • David_001D Offline
    David_001D Offline
    David_001
    wrote on last edited by David_001
    #1

    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();
        }
    
    JonBJ 1 Reply Last reply
    0
    • David_001D David_001

      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();
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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?

      1 Reply Last reply
      0
      • David_001D Offline
        David_001D Offline
        David_001
        wrote on last edited by David_001
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved