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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.