Results 1 to 11 of 11

Thread: Convert QString into QByteArray and vice versa.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Convert QString into QByteArray and vice versa.

    Hi
    please take a look I explain here perhaps it will help to both of us:

    I have some application wroten fully in Qt.
    In the application I take input from user from line edits and receive QStrings ( ->text() function of the QLineEdit class ). Then I need to save the input ( QStrings ) encrypted to .txt file on user machine’s hdd.
    For that purpose I decided to use simple XOR encryption. So I need to convert QString to QByteArray , after that make XOR between 2 byte arrays – input and encryption key and the result I need to save to .txt file so I convert the result vice versa to QString and put it to file.
    Also I need to read from file the encrypted data and to check it in some place in the application. For that purpose I read encrypted from previous user input from .txt file to QString and convert it to QByteArray make decryption on QByteArray, convert the result to QString – and very sorrow I do not receive the same input as before encryption – right side of user input lost!!
    Perhaps it is because of incorrect conversion QByteArray – QString and vice versa.
    What is strangeous – that only for large strings data lost and for number.
    I use UTF8 codec:

    Qt Code:
    1. codec = QTextCodec::codecForName(“utf8”);
    To copy to clipboard, switch view to plain text mode 

    I get user input and write to file:

    Qt Code:
    1. emailEntered = this->login->lineEdit_email->text();
    2.  
    3. keyEntered = this->login->lineEdit_code->text();
    To copy to clipboard, switch view to plain text mode 
    I make conversion to QByteArray and encryption:
    Qt Code:
    1. QString emailToSave = QString::fromUtf8(calculateXor(this->emailEntered.toUtf8(),encryptionKey));
    2.  
    3. QString keyToSave = QString::fromUtf8(calculateXor(this->keyEntered.toUtf8(),encryptionKey));
    To copy to clipboard, switch view to plain text mode 

    I save to file:

    Qt Code:
    1. QTextStream stream(&file);
    2. stream.setCodec(this->codec); stream<<‘‘<<emailToSave<<’\n’<<’’<<keyToSave<<’\n’<<’*’<<macAdressToSave<<’\n’; file.close();
    To copy to clipboard, switch view to plain text mode 

    I read from file:

    Qt Code:
    1. QTextStream stream( &file );
    2. stream.setCodec(this->codec);
    3. emailFromFile.clear();
    4. keyFromFile.clear();
    5. macFromFile.clear();
    6. stream>>ch; // ‘*’
    7. stream>>ch; // email from file:
    8. while(ch!=’\n’)
    9. {
    10. emailFromFile.append(ch);
    11. stream>>ch;
    12. }
    13. stream>>ch;
    14. // ‘*’
    15. stream>>ch; // first digit of the coded license key
    16. while(ch!=’\n’)
    17. {
    18. keyFromFile.append(ch);
    19. stream>>ch;
    20. }
    21. stream>>ch; // ‘*’ stream>>ch;
    22. // first digit of mac from file
    23. while(!stream.atEnd()) // another while(stream!=’\n’)
    24. {
    25. macFromFile.append(ch);
    26. stream>>ch;
    27. }
    28. file.close();
    To copy to clipboard, switch view to plain text mode 
    I decrypt readed data from file:

    Qt Code:
    1. QString result = QString::fromUtf8(calculateXor(this->emailFromFile.toAscii(),encryptionKey));
    2.  
    3. QString result = QString::fromUtf8(calculateXor(this->keyFromFile.toAscii(),encryptionKey));
    To copy to clipboard, switch view to plain text mode 

    For email I get the same as before encryption, but for key ( another user input ) there is lost numbers at the right
    What is the problem? I try to fix it this time…

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Convert QString into QByteArray and vice versa.

    Perhaps it is because of incorrect conversion QByteArray – QString and vice versa.
    It is because of the conversion, but the conversion is not incorrect!
    QString and QByteArray have many things in common, but they are not the same!
    If you read the documentation of the various conversion functions both in QByteArray and QString, you will see notes and warnings about the conversions, and how they are done.
    If you need to deal with non textual byte sequences, you will get problems when you convert from QByteArray to QString, since QString will either discard or replace non visible characters, changing the binary meaning of your original bytes!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Convert QString into QByteArray and vice versa.

    Yes of course it is one of the cases.
    I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
    I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

    How to accomplish such conversion?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Convert QString into QByteArray and vice versa.

    I don't think you can, from the simple fact, that QByteArray has non visible ASCII values (or can have).
    What you need is not to be able to convert QByteArray to QString, but to apply the functionality you have in QStinrg for your QByteArray data.
    As far as I understand it, you will have to implement that your self.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert QString into QByteArray and vice versa.

    Quote Originally Posted by freely View Post
    Yes of course it is one of the cases.
    I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
    I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

    How to accomplish such conversion?
    But why exactly do you want this conversion to take place? If you want to xor the string then just do it. Each QChar is a 16 bit value, just xor it with 16 bits of your key and you'll get two 8b values that you can store in the byte array.

    Qt Code:
    1. QByteArray xorEncrypt(const QString &string, const QByteArray &key){
    2. QByteArray result;
    3. int keyIndex = 0;
    4. for(int i=0;i<string.size();++i){
    5. quint16 val = string.at(i).unicode();
    6. char high = key.at(keyIndex++);
    7. if(keyIndex==key.size()) keyIndex = 0;
    8. char low = key.at(keyIndex++);
    9. if(keyIndex==key.size()) keyIndex = 0;
    10. quint16 keyPiece = ((high & 0xFF) << 8) | (low & 0xFF));
    11. quint16 encoded = val ^ keyPiece;
    12. result.append(encoded >> 8);
    13. result.append(encoded & 0xFF);
    14. }
    15. return result;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Decoding is similar, you just need to reassemble the string.

    Of course be aware that xor is a very lousy "cipher" that can be broken within a couple of seconds, especially when encoding data consisting mostly of 0x00:
    0x00 xor SECRET = SECRET

    The easiest way to break xor is to feed it with a message (either plaintext or ciphertext) consisting of zeroes. Then the key will reveal itself during encryption/decryption.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. sending signals to underlying qt code and vice versa
    By technoViking in forum Qt Quick
    Replies: 2
    Last Post: 9th November 2010, 12:10
  2. Mapping JavaScript object to QT (and vice versa)
    By leoalvesmachado in forum Newbie
    Replies: 2
    Last Post: 30th June 2010, 19:00
  3. Replies: 0
    Last Post: 25th March 2010, 12:10
  4. conversion between string to hex and vice versa
    By mohanakrishnan in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 11:25
  5. Convert QByteArray to object and vice versa
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 20:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.