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. XML file Encryption/Protection
Forum Updated to NodeBB v4.3 + New Features

XML file Encryption/Protection

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 1.5k Views 3 Watching
  • 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.
  • F Offline
    F Offline
    faiszalkhan
    wrote on 5 Feb 2019, 09:59 last edited by
    #1

    The project file of the developed application has a custom extension to it. Internally I have used XML to structure data inside.
    The Project file is used to open the project in the Application. Also, when the user hits save, the file is updated.
    Since the project file is of utmost importance, changing it outside the application could result in undefined behavior and many use cases to take care of.
    I want to protect this file from an average user who should not mess the file. What is the best way to achieve this?

    Thanks.

    B V 2 Replies Last reply 5 Feb 2019, 10:17
    0
    • F faiszalkhan
      5 Feb 2019, 09:59

      The project file of the developed application has a custom extension to it. Internally I have used XML to structure data inside.
      The Project file is used to open the project in the Application. Also, when the user hits save, the file is updated.
      Since the project file is of utmost importance, changing it outside the application could result in undefined behavior and many use cases to take care of.
      I want to protect this file from an average user who should not mess the file. What is the best way to achieve this?

      Thanks.

      B Offline
      B Offline
      beecksche
      wrote on 5 Feb 2019, 10:17 last edited by
      #2

      @faiszalkhan

      One big advantage of XML is that it's human readable. So to encrypt it would be contradictory.

      So why don't you save your project data as binary data?

      1 Reply Last reply
      2
      • F faiszalkhan
        5 Feb 2019, 09:59

        The project file of the developed application has a custom extension to it. Internally I have used XML to structure data inside.
        The Project file is used to open the project in the Application. Also, when the user hits save, the file is updated.
        Since the project file is of utmost importance, changing it outside the application could result in undefined behavior and many use cases to take care of.
        I want to protect this file from an average user who should not mess the file. What is the best way to achieve this?

        Thanks.

        V Offline
        V Offline
        VRonin
        wrote on 5 Feb 2019, 10:29 last edited by
        #3

        @faiszalkhan said in XML file Encryption/Protection:

        I want to protect this file from an average user who should not mess the file.

        To obfuscate you can just compress the file on save. Of course this does not give any kind of protection from intentional changes to the file contents outside your application. If you want to go one step further you can use openssl or crypto++ to encrypt your file but since the decryption key will be stored in your application anyway, a motivated attacker will always be able to crack it. And in any case, you can't protect the file from becoming unreadable by the user simply changing/chopping the content of the file using a text editor

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • F Offline
          F Offline
          faiszalkhan
          wrote on 5 Feb 2019, 10:31 last edited by
          #4

          I have not given a thought to binary data. Also, please think of the file as a project file which is created by the user via the application. This file would be stored on the disk(Any location). Think of .docx file, if we open it in notepad++, we cannot make anything out of it but works perfectly with Word.

          Thanks.

          M V 2 Replies Last reply 5 Feb 2019, 10:36
          0
          • F faiszalkhan
            5 Feb 2019, 10:31

            I have not given a thought to binary data. Also, please think of the file as a project file which is created by the user via the application. This file would be stored on the disk(Any location). Think of .docx file, if we open it in notepad++, we cannot make anything out of it but works perfectly with Word.

            Thanks.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 5 Feb 2019, 10:36 last edited by
            #5

            @faiszalkhan
            Hi
            But how much protected?
            How good are your avg. users?

            For some use cases, simply encode it with base 64 is enough to
            make it unreadable for them whereas for others user not even
            a binary file will stop them.

            You could also add a checksum to the file so you can detect if indeed changed. ( and stop loading it)

            1 Reply Last reply
            4
            • F faiszalkhan
              5 Feb 2019, 10:31

              I have not given a thought to binary data. Also, please think of the file as a project file which is created by the user via the application. This file would be stored on the disk(Any location). Think of .docx file, if we open it in notepad++, we cannot make anything out of it but works perfectly with Word.

              Thanks.

              V Offline
              V Offline
              VRonin
              wrote on 5 Feb 2019, 10:42 last edited by
              #6

              @faiszalkhan said in XML file Encryption/Protection:

              Think of .docx file, if we open it in notepad++, we cannot make anything out of it but works perfectly with Word.

              Try opening it with 7zip (or any other zip reader). office (both MS and Open/Libre) use the first method I mentioned i.e. compress the file on save

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              4
              • F Offline
                F Offline
                faiszalkhan
                wrote on 6 Feb 2019, 08:58 last edited by
                #7

                @beecksche @VRonin @mrjj

                So as per the thread, I can use the following methods to protect the project file from getting changed by an average user. The Average user is a Software developer who might get the motivation to play with it.

                1. Project Data as Binary Data (Not sure about this)

                2. Compress the file on save(Need to know what are the best ways to achieve this)

                3. openssl or crypto++ to encrypt

                4. Encoding with base64

                Since I am not an expert on this and have not worked on a task of this nature before, I am not sure which option is the best to go with.

                I have two basic requirements,

                • When the user double clicks the project file, it opens in the Application, Everything is good. This is Achieved.

                • When the user tries to edit the file from outside(Say notepad++), he/she should see obfuscated data just like when Word document is opened in Notepad++. This is pending.

                Thanks.

                V 1 Reply Last reply 6 Feb 2019, 09:36
                0
                • jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Feb 2019, 09:03 last edited by
                  #8

                  Is there a reason for such a requirement? Why should an user want to edit the project file manually? And if an user would do this - what is the problem? At the end he/she can edit it in your app anyway, right? As an user I would not like if the app I'm using would try to hide the data from me...
                  Word files are not really obfuscated: they are zip archives containing text, images,...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  F Pablo J. RoginaP 2 Replies Last reply 6 Feb 2019, 09:13
                  0
                  • jsulmJ jsulm
                    6 Feb 2019, 09:03

                    Is there a reason for such a requirement? Why should an user want to edit the project file manually? And if an user would do this - what is the problem? At the end he/she can edit it in your app anyway, right? As an user I would not like if the app I'm using would try to hide the data from me...
                    Word files are not really obfuscated: they are zip archives containing text, images,...

                    F Offline
                    F Offline
                    faiszalkhan
                    wrote on 6 Feb 2019, 09:13 last edited by
                    #9

                    @jsulm There is a problem with the user editing it. The user doesn't know the exact structure of the project file, changing it might make the file not readable by the application. This could result in loss of work for the user.

                    jsulmJ 1 Reply Last reply 6 Feb 2019, 09:34
                    0
                    • F faiszalkhan
                      6 Feb 2019, 09:13

                      @jsulm There is a problem with the user editing it. The user doesn't know the exact structure of the project file, changing it might make the file not readable by the application. This could result in loss of work for the user.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 6 Feb 2019, 09:34 last edited by
                      #10

                      @faiszalkhan That's clear, but why would a user want to edit this file outside of your app?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • F faiszalkhan
                        6 Feb 2019, 08:58

                        @beecksche @VRonin @mrjj

                        So as per the thread, I can use the following methods to protect the project file from getting changed by an average user. The Average user is a Software developer who might get the motivation to play with it.

                        1. Project Data as Binary Data (Not sure about this)

                        2. Compress the file on save(Need to know what are the best ways to achieve this)

                        3. openssl or crypto++ to encrypt

                        4. Encoding with base64

                        Since I am not an expert on this and have not worked on a task of this nature before, I am not sure which option is the best to go with.

                        I have two basic requirements,

                        • When the user double clicks the project file, it opens in the Application, Everything is good. This is Achieved.

                        • When the user tries to edit the file from outside(Say notepad++), he/she should see obfuscated data just like when Word document is opened in Notepad++. This is pending.

                        Thanks.

                        V Offline
                        V Offline
                        VRonin
                        wrote on 6 Feb 2019, 09:36 last edited by VRonin 2 Jun 2019, 09:51
                        #11

                        @faiszalkhan
                        1 basically means ditching all the code you already wrote to save/load in xml and start from scratch using binary data (pro: fastest serialisation/deserialisation; cons: require re-engineering the serialization process, strings serialised to binary are still pretty readable if opened in a text editor)
                        2 is a 2 stage process (pro: obfuscates the contents, doesn't require change of existing code to serialise, smaller file size. cons: slower save/load process)

                        // instead of saving directly to file, save the xml to a temporary buffer in memory
                        QByteArray plainxml;
                        QBuffer planxmldevice(&plainxml);
                        // save as usual
                        // void saveProjectFile(QIODevice* out);
                        saveProjectFile(&planxmldevice);
                        // save the compressed buffer to the final file
                        QSaveFile projectFile("projectFile.myextension");
                        Q_ASSUME(projectFile.open(QFile::WriteOnly));
                        projectFile.write(qCompress(plainxml));
                        projectFile.commit();
                        
                        // Load the compressed file and read it to a temporary buffer in memory
                        QFile projectFile("projectFile.myextension");
                        Q_ASSUME(projectFile.open(QFile::ReadOnly));
                        QByteArray compressedxml = projectFile.readAll();
                        // decompress the buffer
                        QByteArray plainxml = qUncompress(compressedxml);
                        compressedxml.clear();
                        QBuffer planxmldevice(&plainxml);
                        // void loadProjectFile(QIODevice* in);
                        loadProjectFile(&planxmldevice);
                        

                        3 is basically the same as 2 but instead of qCompress/qUncompress you'll have a function to encrypt and one to decrypt the data. The actual body of the function depends on the library and encryption method you chose (pro: encrypt the contents, doesn't require change of existing code to serialise, cons: slowerest save/load process, encryption is not hack proof)
                        4 can be used as 2 just by replacing projectFile.write(qCompress(plainxml)); with projectFile.write(plainxml.toBase64()); and QByteArray plainxml = qUncompress(compressedxml); with QByteArray plainxml = QByteArray::fromBase64(compressedxml); (pro: obfuscates the contents, doesn't require change of existing code to serialise. cons: the result will be a string of ascii chars that can still be readable and easily de-obfuscated by the user, base64 is very recognisable when opening the file with a text editor)

                        changing it might make the file not readable by the application

                        There's nothing that can prevent this to happen. The user can always mess with the content of the file with an hex editor and you just can't prevent it. All you can do is put redundancy in place (like MS office does with autorecover but as you might be aware if you ever used that feature is not 100% reliable)

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        3
                        • F Offline
                          F Offline
                          faiszalkhan
                          wrote on 6 Feb 2019, 09:59 last edited by
                          #12

                          @jsulm I don't know. We've had experiences before :D
                          Thanks.

                          @VRonin Thanks. Will look into this.

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm
                            6 Feb 2019, 09:03

                            Is there a reason for such a requirement? Why should an user want to edit the project file manually? And if an user would do this - what is the problem? At the end he/she can edit it in your app anyway, right? As an user I would not like if the app I'm using would try to hide the data from me...
                            Word files are not really obfuscated: they are zip archives containing text, images,...

                            Pablo J. RoginaP Offline
                            Pablo J. RoginaP Offline
                            Pablo J. Rogina
                            wrote on 6 Feb 2019, 14:07 last edited by Pablo J. Rogina 2 Jun 2019, 14:08
                            #13

                            As @jsulm said:

                            Why should an user want to edit the project file manually? And if an user would do this - what is the problem? At the end he/she can edit it in your app anyway

                            I'm thinking of translation files (.ts) or GUI files (.ui) in Qt itself. They're just plain XML files, and I create/edit them with proper applications (Linguist and Designer respectively). BUT I can also edit them with my favorite text editor, why not?
                            And if I mess them up, my favorite version control system (git, SVN, you name it) will come to the rescue to back them up to the point before the issue.

                            It looks like you're doing a lot of work/effort for something that could be avoided...

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            1

                            1/13

                            5 Feb 2019, 09:59

                            • Login

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