I am using Qt 5.2.1 and I made a program that takes some values and saves them on a text file. What I want is that everybody can see this file and read the content but nobody can edit it. To edit it, you would have to run the program and enter a password. How do I do this?

thanx in Advance :D

Dani AI

Generated

Short summary and two practical approaches.

The requirement "everybody can read the file but only your program (after a password) can edit it" has two different meanings and two different solutions: enforce write-only access at the OS level (true prevention), or make edits tamper-evident so only someone with the right secret/password can produce a valid file (detection). Encryption (as suggested) prevents reading, so it only fits if you also want to hide the contents — not your case if the file must be readable. See Qt's hashing utilities for quick checks (they do hashing only; not signing/HMAC). QCryptographicHash docs. (doc.qt.io)

Recommendation 1 — readable + tamper-evident: digital signatures

  • Create an asymmetric key pair (Ed25519 recommended).
  • Keep the private key encrypted on disk (PEM + passphrase) and let the program prompt for that passphrase to unlock it.
  • When the program saves, it signs the file and writes a detached signature (e.g. file.sig). Anyone can read the file, and the program (or any verifier with the public key) can verify that the file was produced by the signer. This makes casual/manual edits detectable and prevents unauthorized silent modification. Use a well-tested library for signing; for example libsodium's detached-sign APIs and Qt's key helpers. libsodium crypto_sign docsQSslKey (Qt) for PEM/passphrase handling. (doc.libsodium.org)

Minimal signing sketch (libsodium + Qt):

// read file into QByteArray data
// secret key (sk) must be crypto_sign_SECRETKEYBYTES long
unsigned char sig[crypto_sign_BYTES];
crypto_sign_detached(sig, nullptr,
    reinterpret_cast<const unsigned char*>(data.constData()),
    (unsigned long long)data.size(),
    reinterpret_cast<const unsigned char*>(sk.constData()));
// write sig to file.sig

Recommendation 2 — password-based edit control (HMAC + KDF)

  • Derive a symmetric MAC key from the edit password with a KDF (Argon2 via libsodium pwhash).
  • Compute an authentication tag (HMAC/crypto_auth) over the file and store the tag + salt/params beside the file.
  • To edit, the program asks for the password, derives the key, updates file, recomputes tag. Without the password you cannot produce a valid tag. See libsodium pwhash + auth APIs. libsodium pwhash docslibsodium auth/HMAC docs. (doc.libsodium.org)

Minimal HMAC sketch:

// derive key with crypto_pwhash(..., salt, ops, mem, alg)
// compute tag with crypto_auth(tag, msg, msglen, key)
// store salt + tag in file.meta

Extra tips

  • Combine methods: enforce filesystem ACLs where possible (true prevention) and add signatures/MACs for tamper-evidence (defense in depth).
  • Do not invent crypto. Use vetted libraries: libsodium, OpenSSL or Qt-friendly QCA for higher-level integration. . ()

This gives a clear tradeoff: OS permissions actually stop edits (until an admin changes ACLs), while signatures/HMACs ensure that only someone with the private key or password can produce a valid file that your program will accept.

Write a file in encrypted form, write a program which prompt about password at run, read encrypted file, decrypt it and fill your edit control.

The thing is I don't know how to do that... could you help me in the right direction?

Well, you could start for example here: http://www.cplusplus.com/forum/articles/38516/

This article explain one of the simpliest encryption method. As soon as you get it implemented, go ahead and write reading from/to file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.