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. [Split] QString/QByteArray conversion problems
Forum Update on Monday, May 27th 2025

[Split] QString/QByteArray conversion problems

Scheduled Pinned Locked Moved General and Desktop
28 Posts 3 Posters 20.5k 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.
  • S Offline
    S Offline
    swhweng
    wrote on last edited by
    #4

    You can take a look at the same post at QtCentre also:

    http://www.qtcentre.org/threads/39638-Convert-QString-into-QByteArray-and-vice-versa.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #5

      Pavel, please answer in the separate thread where your question was split off. This was not without intention - your question is unrelated to the OPs question in the other thread.

      There is no error with converting QStrings to QByteArrays and vice versa. You can do this happily as often as you want.

      BUT if you XOR the byte representation of your string, you manipulate the data in such a way, that it is not guaranteed to contain a valid utf-8 byte representation of any string. So calling fromUtf8() on this data is plain wrong. Everything afterwards leads to disaster.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        swhweng
        wrote on last edited by
        #6

        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:

        @QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
        QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));@

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

        @macEnEn = calculateXor(fromFile.append(this->macFromFile),encryptionKey);
        QString result = QString::fromUtf8(macEnEn.data(),macEnEn.size());@

        The proble 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!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          swhweng
          wrote on last edited by
          #7

          Ok Thank you very much, sorry if I've wrote a little not ordered.
          I will write ordered.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            swhweng
            wrote on last edited by
            #8

            any disaster Volker! - I have solved the problem you can try it by yourself - the things work!
            Here you have simple encryption method for any character set.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #9

              Oh. My. God.

              You will not understand, will you?

              If you manipulate bytes with XOR you are very likely to create a byte sequence, that is NOT a valid utf-8 encoded string. How often shall I repeat this again until it gets into you brain?!?

              You need a sample? Here we go:

              Let's have input char 'A' = 0x41 = 0100 0001
              Now you happen to XOR that 'A' with 0x41: the result 0x00 and you blew up your encoding. And all this does not count in 2, 3 and 4 byte encoded characters.

              And don't tell me this won't happen. You cannot guarantee. Period.

              Again: do not use fromUtf8() with your manipulated bytes!

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                swhweng
                wrote on last edited by
                #10

                Very interesting.
                Yes it can happen ( I do not understand deeply now but I think...), but it can happen when you convert to bytes, make xor, convert to string, store and convert vice versa. It happens when you put xored bytes coded of utf to QString for some of cases not always, am I right?

                Edit:
                What 8 bit character codec to use that has characters “coded” for any 0 to 2^8 – 1 numbers?

                Edit: merged posts. Please add to your previous comment if you hit the Post button before thinking if this is really all you wanted to say; Andre

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #11

                  For encryption, it really doesn't matter. As long as you are consistent. I would probably use UTF-8. After XOR-ing, you end up with a byte array containing binairy data, not a string (as Volker has tried to point out to you repeatingly). You could use QByteArray::toBase64 to create a QString you can store in a text file. Then, you can do the reverse on your way back: read in the base64 encoded byte array, XOR with your secret key, and then create a QString with fromUtf8 based on that decoded byte array.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    swhweng
                    wrote on last edited by
                    #12

                    Yes Yes Yes
                    Thank you
                    If I use QByteArray::toBase64 what kind of QString then?

                    Edit:
                    I am looking for simplest way to encrypt any characters set
                    Perhaps to use QCryptographicHash?

                    Edit: merged two consecutive messages again. Please do that yourself next time; Andre

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #13

                      Since base64 by definition only uses ASCII characters, any encoding that has those at the right places will do. UTF-8 is one of them, but of course QString::fromAscii() would do better, I think.

                      Edit: QCryptographicHash does, as the name implies, hashing. That is not the same as encryption. Hashing is (in theory) a one-way, non reversible operation, encryption a two-way operation (that is, you can reverse it). You might look into QCA.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        swhweng
                        wrote on last edited by
                        #14

                        After I checked and thought here it is:

                        There is a need to convert each character to 8 bit such way that any 8 bit number has it's representation in characters - one to one from 0000 0000 to 1111 1111 and then it is Ok to make xor and vice versa?

                        So what coding system ( standard ) provides 8 bit number one to one to characted mapping?
                        ASCII?

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          swhweng
                          wrote on last edited by
                          #15

                          if I use QByteArray QCryptographicHash::hash ( const QByteArray & data, Algorithm method )
                          and then should I put the QByteArray to text file ( if I need for example to store encrypted data on hdd ) as QDataStream or can I use QByteArray::toBase64 ? What codec Qt supports crossplatform that provides one - to - one coding of characters to bytes ( 8 bit numbers ) ?
                          Pavel
                          14.03.2011

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            swhweng
                            wrote on last edited by
                            #16

                            This also can help to any who is interesting in fast and reliable encryption:

                            http://wiki.forum.nokia.com/index.php/How_to_Encrypt_a_text_using_QCryptographicHash_in_Qt_for_Symbian

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              swhweng
                              wrote on last edited by
                              #17

                              because ASCII has no period and UTF8 does have?

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #18

                                I am sorry, but: you really don't get it, do you?

                                You still keep on posting multiple messages in a row, instead of adding to your previous messages with the edit link on the right side of your last message

                                I already explained to you that hashing is not the same as encryption, no matter what that misguided wiki entry tells you

                                I already explained that the character encoding you use for your string is inconsequential

                                QByteArray already can be thought off as an array of 8-bit (yes, that is a byte in the x86 world) characters. Do your xor magic on that.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  swhweng
                                  wrote on last edited by
                                  #19

                                  Sorry, ASCII is one to one character to 8 bit conversion in all 0 - 255 range?
                                  I am not sure
                                  I need Qt supported codec that converts characters one to one to 8 bit number in 0 -255 range, another worlds each 8 bit number in byte array has it's unique character.
                                  What is the codec?

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    swhweng
                                    wrote on last edited by
                                    #20

                                    Andre I understand - Ok I won't post multiple but by using edit.
                                    Ok Thanks a Lot. Using hash can I make "unhash" with the same key? How can I get unhashing one - to -one?

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #21

                                      [quote author="Pavel Mazniker" date="1300124063"]Andre I understand - Ok I won't post multiple but by using edit.[/quote]

                                      You just did.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goetz
                                        wrote on last edited by
                                        #22

                                        [quote author="Pavel Mazniker" date="1300122778"]What codec Qt supports crossplatform that provides one - to - one coding of characters to bytes ( 8 bit numbers ) ?[/quote]

                                        The whole thing about codecs is to map all the big bunch of non-ASCII characters into some 8-bit representation. This can reduce the mappable characters (like in ISO-8859-1) or it can lead to a representation consisting of multiple 8-bit bytes (like in UTF-8).

                                        All conversion methods that return QByteArray give you an array of 8bit bits.

                                        http://www.catb.org/~esr/faqs/smart-questions.html

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          swhweng
                                          wrote on last edited by
                                          #23

                                          [quote author="Volker" date="1300125063"]
                                          [quote author="Pavel Mazniker" date="1300122778"]What codec Qt supports crossplatform that provides one - to - one coding of characters to bytes ( 8 bit numbers ) ?[/quote]

                                          The whole thing about codecs is to map all the big bunch of non-ASCII characters into some 8-bit representation. This can reduce the mappable characters (like in ISO-8859-1) or it can lead to a representation consisting of multiple 8-bit bytes (like in UTF-8).

                                          All conversion methods that return QByteArray give you an array of 8bit bits.[/quote]

                                          Hi
                                          Thank you very much for your help.
                                          Your explanations here at the forum really helped me.
                                          It seems this time I fixed the problem finally - encryption/decryption works for letters,numbers and @ sign,

                                          • I've checked for some inputs and it is ok and also theoretically shoud not be any problem( not for any possible character ).So as to be sure 100% one should understand
                                            how Ascii,Base64 map char to byte.
                                            I encrypt by manipulating bytes of byte array: QString someString.toAscii(),
                                            after that I convert it .toBase64() and save it using QDataStream to .dat file.
                                            When I decrypt I open .dat file with QDataStream,convert readed data from Base64 using QByteArray::fromBase64()
                                            and make byte manipulations needed for decryption on the byte array decoded from file.
                                            Pavel
                                          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