Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Writing secure data to a protected file
Forum Updated to NodeBB v4.3 + New Features

Writing secure data to a protected file

Scheduled Pinned Locked Moved Unsolved Brainstorm
4 Posts 4 Posters 2.0k Views 4 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.
  • M Offline
    M Offline
    Maxim DC
    wrote on 29 May 2016, 19:51 last edited by
    #1

    Hello everyone,
    I was making an application today and noticed that i only can save files like this localy

    QByteArray txtBuffer = "txtdata";
    QFile file("file.txt");
    if (file.open(QIODevice::WriteOnly))
    {
    	file.write(txtBuffer);
    	qInfo() << "[Info] Txt has been written to the disk";
    }
    file.close();
    

    and read it like this

    QByteArray txtBuffer;
    QFile file("file.txt");
    if (file.open(QIODevice::ReadOnly))
    {
    	txtBuffer = file.readAll();
    	qInfo() << "[Info] Txt has been read from the disk";
    }
    file.close();
    

    but what if that data isn't that stupid like "txtdata" but something more like progress in a game, how to save this? I could do it like this but then the player can just alter the progress in the game with a text-editor so it has more abilities or something like that. I would like to prevent this by writing to a protected location or a password-protected file, especially on Android. What is the right approach?

    T 1 Reply Last reply 30 May 2016, 05:48
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 29 May 2016, 20:21 last edited by
      #2

      Hi! One simplistic approach that comes to my mind: Once you want to save the contents to your file.txt, take this content, append a secret string to it and compute a hash over everything. Then save the content (without the secret, of course) to a file. Also save the computed hash to another file. Next time, your application reads the two files, recomputes the hash and checks if they match.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 May 2016, 20:42 last edited by
        #3

        Hi,

        Thinking of something like encrypting that file ?

        The CryptFileDevice project might be of interest.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Maxim DC
          29 May 2016, 19:51

          Hello everyone,
          I was making an application today and noticed that i only can save files like this localy

          QByteArray txtBuffer = "txtdata";
          QFile file("file.txt");
          if (file.open(QIODevice::WriteOnly))
          {
          	file.write(txtBuffer);
          	qInfo() << "[Info] Txt has been written to the disk";
          }
          file.close();
          

          and read it like this

          QByteArray txtBuffer;
          QFile file("file.txt");
          if (file.open(QIODevice::ReadOnly))
          {
          	txtBuffer = file.readAll();
          	qInfo() << "[Info] Txt has been read from the disk";
          }
          file.close();
          

          but what if that data isn't that stupid like "txtdata" but something more like progress in a game, how to save this? I could do it like this but then the player can just alter the progress in the game with a text-editor so it has more abilities or something like that. I would like to prevent this by writing to a protected location or a password-protected file, especially on Android. What is the right approach?

          T Offline
          T Offline
          the_
          wrote on 30 May 2016, 05:48 last edited by the_
          #4

          @Maxim-DC

          but what if that data isn't that stupid like "txtdata" but something more like progress in a game, how to save this?

          Another approach for this could be QSettings with a custom format. You could set a reader and a writer function that writes encrypted data into a file.

          
          QSettings::Format newformat = QSettings::registerFormat("cnf",read,write);
          settings = new QSettings("cnf/u.cnf",newformat);
          
          bool read(QIODevice &dev,QSettings::SettingsMap &map) {
             //your code goes here 
             return true;
          }
          
          bool write(QIODevice &dev,const QSettings::SettingsMap &map) {
             //your code goes here
            return true;
          }
          

          For not so strong encryption you could use Simple Crypt in your read and write functions

          -- No support in PM --

          1 Reply Last reply
          1

          1/4

          29 May 2016, 19:51

          • Login

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