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. What is the best way to encrypt large binary data using QCA
Forum Update on Monday, May 27th 2025

What is the best way to encrypt large binary data using QCA

Scheduled Pinned Locked Moved General and Desktop
qca
1 Posts 1 Posters 973 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.
  • N Offline
    N Offline
    ngendlio
    wrote on 6 Sept 2015, 23:51 last edited by ngendlio 9 Jul 2015, 00:26
    #1

    Hello everyone ! I hope you're having a nice day!
    In fact, I was to using the QCA library in Qt , and everything works great except when I want to encrypt large files ....
    When i try to encrypt large files that are binary , the output is really small.. I don't understand why ?
    I dont 't know if it is teh problem of my code
    For example , look at my code, I do not know if anyone could give me an example of a project that I can use as a reference

    void AESCrypt::writeBinary(QByteArray arr, QString filePath)
    {
        QFile f(filePath);
        qDebug()<<" Transfering to new File"+f.fileName();
        
        if(!f.open(QIODevice::WriteOnly|QIODevice::Truncate)){
            qDebug()<<" Failed to Write to the file "+filePath+"__>"+f.errorString();return;
        }
        //    QDataStream out(&f);
        //    out.setByteOrder(QDataStream::LittleEndian); // *** set little endian byte order
        //    out<<arr;
        f.write(arr);
        qDebug()<<" J ai ecrit "+QString::number(arr.size())+" "+hash(arr).toHex();
        
        f.close();
    }
    
    
    QByteArray AESCrypt::readBinary(QString filePath)
    {
        QFile f(filePath);    
        if(!f.open(QIODevice::ReadOnly)){
            qDebug()<<" Failed to Open to the file "+filePath+"__>"+f.errorString();
            return QByteArray();
        }
        //    QDataStream in(&f);
        //        in.setByteOrder(QDataStream::LittleEndian); // *** set little endian byte order
        QByteArray arr=f.readAll();
        //    in>>arr;
        
        f.close();
        qDebug()<<" J ai lu "+QString::number(arr.size())+" "+hash(arr).toHex();
        return arr;
    }
    
    QByteArray AESCrypt::encrypt(QByteArray data, QString _pass)
    {
        qDebug()<<"pass to encrypt "+_pass;
        QByteArray chiperKey = hash(_pass.toLocal8Bit());
        key =  QCA::SymmetricKey(chiperKey.left(64));
        iv = QCA::InitializationVector(chiperKey.right(64));
        QCA::Cipher cipher(CODEC,QCA::Cipher::CBC,QCA::Cipher::DefaultPadding, QCA::Encode,key, iv);
        
        QList<QByteArray> lista = split(data, 15);
        
        QCA::SecureArray encrypted;
        foreach (QByteArray item, lista) {
            cipher.setup(QCA::Encode, key, iv);
            QCA::SecureArray secureData = cipher.update(item);
            QCA::SecureArray encryptedData = cipher.final();
            if (!cipher.ok()) {
                qDebug() << "Encryption failed !";
                return "";
            }
            encrypted.append(encryptedData);
        }
        return encrypted.toByteArray();
    }
    
    QByteArray AESCrypt::decrypt(QByteArray data, QString _pass)
    {
        qDebug()<<"pass to decrypt "+_pass;
        QByteArray chiperKey = hash(_pass.toLocal8Bit());
        key =  QCA::SymmetricKey(chiperKey.left(64));
        iv = QCA::InitializationVector(chiperKey.right(64));
        QCA::Cipher cipher = QCA::Cipher(CODEC,QCA::Cipher::CBC,QCA::Cipher::DefaultPadding, QCA::Decode,key,iv);
        
        QList<QByteArray> lista = split(data, 16);
        QCA::SecureArray encrypted;
        foreach (QByteArray item, lista) {
            if(item.size()>0){
                cipher.setup(QCA::Decode, key, iv);
                QCA::SecureArray encryptedData = cipher.process(item);
                if (!cipher.ok()) {
                    qDebug() << "Decryption failed !";
                    return "";
                }
                encrypted.append(encryptedData);
            }
        }
        return QString(encrypted.data()).toUtf8();
    }
    

    Thank you

    Software developer, Cryptography ,Computer security.

    1 Reply Last reply
    0

    1/1

    6 Sept 2015, 23:51

    • Login

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