Results 1 to 11 of 11

Thread: Convert QString into QByteArray and vice versa.

  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,360
    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.


  6. #6
    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.

    The problem with data loss during encryption was solved my mine by
    a next way:
    I work with UTF8 codec always when I treat strings at any platform:
    I added next static “methods” to define codecs:

    Qt Code:
    1. QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    2. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    To copy to clipboard, switch view to plain text mode 

    when I convert from QByteArray to QString I use next static “method” with next arguments:

    Qt Code:
    1. macEnEn = calculateXor(fromFile.append(this->macFromFile),encryptionKey);
    2. QString result = QString::fromUtf8(macEnEn.data(),macEnEn.size());
    To copy to clipboard, switch view to plain text mode 

    The problel was – you should provide second argument an exact size of array to static “method” QString::fromUtf8(…)
    it works!
    You can take any character set, convert it, encode using bitwise manipulations, store or transmit
    and decode to any character set using your symmetrical key!
    I have my own xor method, I do not provide it here.


    Added after 34 minutes:


    sorry I am not sure about converting xor'ed QByteArray to utf8 coded QString and vice versa because of codec you can loose characters "meaning".
    Somebody already knows what codec suitable for such conversion?
    I will check and get back to here later

    So what "cypher" * symmetrical to use ?


    Added after 33 minutes:


    Next link will help for simple and fast text data encryption:

    http://wiki.forum.nokia.com/index.ph...Qt_for_Symbian
    Since XOR encryption is not so reliable.
    Last edited by freely; 14th March 2011 at 17:17.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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
    The problem with data loss during encryption was solved my mine
    Data loss can still occur as there are character sets with characters outside the utf-8 scope.

    Somebody already knows what codec suitable for such conversion?
    Yeah... Unicode...

    So what "cypher" * symmetrical to use ?
    Hmm?

    Next link will help for simple and fast text data encryption:

    http://wiki.forum.nokia.com/index.ph...Qt_for_Symbian
    True. Unfortunately it is not possible to decrypt such message.
    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.


  8. #8
    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.

    True. Unfortunately it is not possible to decrypt such message.
    Why not if using hash on hash you won't receive the previous message ( using the same hashing algorithm )?
    Last edited by freely; 15th March 2011 at 14:03.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.

    Hashes (such as SHA1) are one way functions: H(H(x)) !=x. Besides, they don't use a key (aka "password"). Since I know you are using OpenSSL for your mailing application I suggest you use one of encryption algorithms supported by OpenSSL:
    text Code:
    1. Cipher commands (see the `enc' command for more details)
    2. aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb aes-256-cbc
    3. aes-256-ecb base64 bf bf-cbc bf-cfb
    4. bf-ecb bf-ofb cast cast-cbc cast5-cbc
    5. cast5-cfb cast5-ecb cast5-ofb des des-cbc
    6. des-cfb des-ecb des-ede des-ede-cbc des-ede-cfb
    7. des-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofb
    8. des-ofb des3 desx rc2 rc2-40-cbc
    9. rc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofb
    10. rc4 rc4-40
    To copy to clipboard, switch view to plain text mode 

    I'd suggest blowfish (bf) (e.g. in cipher block chaining mode - bf-cbc) or 3des (des3).
    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.


  10. #10
    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.

    Witold you are guru for me
    Hhh
    I saw your interview at QtDevDays highlights
    How are you doing?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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
    How are you doing?
    Fine, thank you, although my PhD is lagging behind, as usual. But let's stick to the subject.
    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.