Rewrite a file content in an existing file.
-
It looks correct. You should do some more testing (and add some error handling) before trusting it on files you care about. Make sure you can decrypt and get the original contents back exactly with your newly created encrypted file.
@mchinand said in Rewrite a file content in an existing file.:
It looks correct. You should do some more testing (and add some error handling) before trusting it on files you care about. Make sure you can decrypt and get the original contents back exactly with your newly created encrypted file.
I see encrypted data in the file. But decrypt doesn't work.
OMG! Sorry, my bad. It works like a charm.
-
@mchinand said in Rewrite a file content in an existing file.:
It looks correct. You should do some more testing (and add some error handling) before trusting it on files you care about. Make sure you can decrypt and get the original contents back exactly with your newly created encrypted file.
I see encrypted data in the file. But decrypt doesn't work.
OMG! Sorry, my bad. It works like a charm.
@jenya7 said in Rewrite a file content in an existing file.:
It works like a charm.
great, so if your issue is solved please don't forget to mark your post as such!
-
That'll do?
void CRYPTO_EncryptFile(QString file) { //firs - open a file QFile fil(file); if (!fil.open(QFile::ReadOnly)) return; simp_crypt.setKey(0x0c2ad4a4acb9f023); //read the file QTextStream txt_str(&fil); QString content = txt_str.readAll(); fil.close(); //encrypt it QString encrypted = simp_crypt.encryptToString(content); //write the encrypted data back //txt_str.seek(0); //txt_str.flush(); //txt_str << encrypted; if (!fil.open(QFile::WriteOnly)) return; QByteArray ba = encrypted.toLocal8Bit(); char *str = ba.data(); fil.write(str); fil.close(); }
@jenya7 said in Rewrite a file content in an existing file.:
QByteArray ba = encrypted.toLocal8Bit();
char *str = ba.data();fil.write(str);
That can be simplified:
fil.write(encrypted.toLocal8Bit());
There's no need to search for a pointer since QIODevice already provides an overload to write QByteArray.
-
@jenya7 said in Rewrite a file content in an existing file.:
QByteArray ba = encrypted.toLocal8Bit();
char *str = ba.data();fil.write(str);
That can be simplified:
fil.write(encrypted.toLocal8Bit());
There's no need to search for a pointer since QIODevice already provides an overload to write QByteArray.
-
@jenya7 said in Rewrite a file content in an existing file.:
What if I want to change it - where can I store it?
This is a very complicated question because there is a difference between "Where can I store it?" and "Where should I store it?".
The first question is how you obtain the key. Is it something that is the same for everybody? Should it be installed together with your software? Or does every user create their own key? If every user has their own key you can use QSettings to store it. However, it might be too easy for other users on the same computer to get access to the key which they shouldn't be able to do. As an example SSH private keys are stored in a users home folder and can only be used if file permissions are set only for that user. You could then use QSettings and specify a file in the users folder for storage. I am not sure, though, if you can change file permissions with Qt. One step further would be to not save the key as plain text, but protect it with a password. Have a look at password hashing with a random seed (though here we want to hash the key with the methods usually used for passwords).
You see, the correct approach depends on which level of security you require. BTW, if you change the key in your code you will not be able to decrypt any old files. This is something you should consider...
-
One more question. The key is hardcoded
simp_crypt.setKey(0x0c2ad4a4acb9f023);
What if I want to change it - where can I store it?
@jenya7
You have several problems!I assume you are using symmetric encryption. That means the same key as used to encrypt is also used to decrypt.
-
Why would you want to change this key?
-
When would you want to change it? If you change it after you have encrypted anything you will need the previous one(s) in order to decrypt. That means you will have to store some history of which keys area associated with which encrypted objects. Not a good idea.
-
Most significantly: If you store the key used externally, anybody who can access that can break all your encryption. You will then spend much time trying to ensure the stored location or content is secure.
-
-
@jenya7
You have several problems!I assume you are using symmetric encryption. That means the same key as used to encrypt is also used to decrypt.
-
Why would you want to change this key?
-
When would you want to change it? If you change it after you have encrypted anything you will need the previous one(s) in order to decrypt. That means you will have to store some history of which keys area associated with which encrypted objects. Not a good idea.
-
Most significantly: If you store the key used externally, anybody who can access that can break all your encryption. You will then spend much time trying to ensure the stored location or content is secure.
@JonB said in Rewrite a file content in an existing file.:
@jenya7
You have several problems!I assume you are using symmetric encryption. That means the same key as used to encrypt is also used to decrypt.
-
Why would you want to change this key?
-
When would you want to change it? If you change it after you have encrypted anything you will need the previous one(s) in order to decrypt. That means you will have to store some history of which keys area associated with which encrypted objects. Not a good idea.
-
Most significantly: If you store the key used externally, anybody who can access that can break all your encryption. You will then spend much time trying to ensure the stored location or content is secure.
Suppose I provide the app to a user. He wants to set his own key. Another case - the key was busted - he wants to change it. The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
-
-
@JonB said in Rewrite a file content in an existing file.:
@jenya7
You have several problems!I assume you are using symmetric encryption. That means the same key as used to encrypt is also used to decrypt.
-
Why would you want to change this key?
-
When would you want to change it? If you change it after you have encrypted anything you will need the previous one(s) in order to decrypt. That means you will have to store some history of which keys area associated with which encrypted objects. Not a good idea.
-
Most significantly: If you store the key used externally, anybody who can access that can break all your encryption. You will then spend much time trying to ensure the stored location or content is secure.
Suppose I provide the app to a user. He wants to set his own key. Another case - the key was busted - he wants to change it. The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
@jenya7 said in Rewrite a file content in an existing file.:
Suppose I provide the app to a user. He wants to set his own key.
Then prompt the user to enter a key. Are you then saying you want your program to save that particular key, so that without user intervention it can later decrypt the data itself? Or, are you saying the end user would be prompted to re-enter the key for decryption, in which case you have no need to save it anywhere?
Another case - the key was busted - he wants to change it.
I do not know what "busted" means. If you want to change the key after data has been encrypted, you would first need the original key to decrypt what you have encrypted, only then could you use a new key to re-encrypt.
The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
This is a quite different situation. The forum requires a password for authorisation, but it has not in any way encrypted the current data with the original password.
-
-
@jenya7 said in Rewrite a file content in an existing file.:
Suppose I provide the app to a user. He wants to set his own key.
Then prompt the user to enter a key. Are you then saying you want your program to save that particular key, so that without user intervention it can later decrypt the data itself? Or, are you saying the end user would be prompted to re-enter the key for decryption, in which case you have no need to save it anywhere?
Another case - the key was busted - he wants to change it.
I do not know what "busted" means. If you want to change the key after data has been encrypted, you would first need the original key to decrypt what you have encrypted, only then could you use a new key to re-encrypt.
The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
This is a quite different situation. The forum requires a password for authorisation, but it has not in any way encrypted the current data with the original password.
@JonB said in Rewrite a file content in an existing file.:
@jenya7 said in Rewrite a file content in an existing file.:
Suppose I provide the app to a user. He wants to set his own key.
Then prompt the user to enter a key. Are you then saying you want your program to save that particular key, so that without user intervention it can later decrypt the data itself? Or, are you saying the end user would be prompted to re-enter the key for decryption, in which case you have no need to save it anywhere?
Another case - the key was busted - he wants to change it.
I do not know what "busted" means. If you want to change the key after data has been encrypted, you would first need the original key to decrypt what you have encrypted, only then could you use a new key to re-encrypt.
The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
This is a quite different situation. The forum requires a password for authorisation, but it has not in any way encrypted the current data with the original password.
I thought about the following scenario:
I provide the app to a user. He has settings.xml file with all system parameters, say I put the key in the file too.
Now from the GUI he can command - "encrypt file_pass" - and the file encrypted. If he wants to change some data like the encryption key - he commands from GUI - "decrypt file_pass" - and file decrypted, he changes the data - and commands - "encrypt file_pass" to encrypt the data back.
In order to issue the command from GUI he has to enter a password.....where should I store the password ? The password can not be hardcoded also - the user wants to change it sometimes.
It always the end of the rope - and the master key is hanging there.Ohhh...Now I see - he can feed the key with the command "encrypt file_pass key". The problem is solved.
Not quite! The app at start up has to decrypt the settings.xml file and parse system parameters from it. So the key should be present and available to the app.
I have to note - it's an autonomous app. A user (technician) come occasionally to check the system or update parameters. -
@JonB said in Rewrite a file content in an existing file.:
@jenya7 said in Rewrite a file content in an existing file.:
Suppose I provide the app to a user. He wants to set his own key.
Then prompt the user to enter a key. Are you then saying you want your program to save that particular key, so that without user intervention it can later decrypt the data itself? Or, are you saying the end user would be prompted to re-enter the key for decryption, in which case you have no need to save it anywhere?
Another case - the key was busted - he wants to change it.
I do not know what "busted" means. If you want to change the key after data has been encrypted, you would first need the original key to decrypt what you have encrypted, only then could you use a new key to re-encrypt.
The same situation like with any password - I log to Qt forum with a password but I have an ability to change it.
This is a quite different situation. The forum requires a password for authorisation, but it has not in any way encrypted the current data with the original password.
I thought about the following scenario:
I provide the app to a user. He has settings.xml file with all system parameters, say I put the key in the file too.
Now from the GUI he can command - "encrypt file_pass" - and the file encrypted. If he wants to change some data like the encryption key - he commands from GUI - "decrypt file_pass" - and file decrypted, he changes the data - and commands - "encrypt file_pass" to encrypt the data back.
In order to issue the command from GUI he has to enter a password.....where should I store the password ? The password can not be hardcoded also - the user wants to change it sometimes.
It always the end of the rope - and the master key is hanging there.Ohhh...Now I see - he can feed the key with the command "encrypt file_pass key". The problem is solved.
Not quite! The app at start up has to decrypt the settings.xml file and parse system parameters from it. So the key should be present and available to the app.
I have to note - it's an autonomous app. A user (technician) come occasionally to check the system or update parameters.@jenya7 said in Rewrite a file content in an existing file.:
say I put the key in the file too.
Don't do that. If you just want to obfuscate the password you can qCompress it.
@jenya7 said in Rewrite a file content in an existing file.:
where should I store the password ?
You should not. Store the hash of the password (SHA-512 for example), which is one way function btw., then when user enters the password you compute same hash algo from it and compare the hashes. This is the way.
@jenya7 said in Rewrite a file content in an existing file.:
The app at start up has to decrypt the settings.xml file and parse system parameters from it. So the key should be present and available to the app.
This is often the problem and there are many ways to overcome this, from obfuscation to secure wallets/stores provided by the system but none of it is straightforward (and I might be wrong here but Qt does not provide uniform interface to those for any of the platforms).
Usually one just has to evaluate the effort required to extract the data/value of the data against the effort required to perform such "break in"/extraction. Sometimes just doing qCompress (or other bit manipulation) on the key part of the data is sufficient, sometimes you have to dig in further. -
@jenya7 said in Rewrite a file content in an existing file.:
say I put the key in the file too.
Don't do that. If you just want to obfuscate the password you can qCompress it.
@jenya7 said in Rewrite a file content in an existing file.:
where should I store the password ?
You should not. Store the hash of the password (SHA-512 for example), which is one way function btw., then when user enters the password you compute same hash algo from it and compare the hashes. This is the way.
@jenya7 said in Rewrite a file content in an existing file.:
The app at start up has to decrypt the settings.xml file and parse system parameters from it. So the key should be present and available to the app.
This is often the problem and there are many ways to overcome this, from obfuscation to secure wallets/stores provided by the system but none of it is straightforward (and I might be wrong here but Qt does not provide uniform interface to those for any of the platforms).
Usually one just has to evaluate the effort required to extract the data/value of the data against the effort required to perform such "break in"/extraction. Sometimes just doing qCompress (or other bit manipulation) on the key part of the data is sufficient, sometimes you have to dig in further. -
@artwaw
OK. But what if the app at start up has to know the key to decode the file (in case of autonomous run)?@jenya7 How should I know? Every circumstances differ.
Depending on the security level provided you can either obfuscate the key in QSettings (and give it innocent enough name as property keys are easily readable on every platform) or learn how to use system api secure storage calls to store the key there (Wallet on macOS/Kerberos Linux and I believe Windows has its own api for that).It is a bit of egg and the chicken problem but it is for you to determine where the weak link is.
On related note I just discovered that Qt introduced QCryptographicHash class, which can do hashing easy for you - I was always using OpenSSL for that and here is the good news, now it is easy.
But back to your question: do you absolutely have to encode the file in such a strong way? Maybe just putting it through qCompress is enough?
-
@jenya7 How should I know? Every circumstances differ.
Depending on the security level provided you can either obfuscate the key in QSettings (and give it innocent enough name as property keys are easily readable on every platform) or learn how to use system api secure storage calls to store the key there (Wallet on macOS/Kerberos Linux and I believe Windows has its own api for that).It is a bit of egg and the chicken problem but it is for you to determine where the weak link is.
On related note I just discovered that Qt introduced QCryptographicHash class, which can do hashing easy for you - I was always using OpenSSL for that and here is the good news, now it is easy.
But back to your question: do you absolutely have to encode the file in such a strong way? Maybe just putting it through qCompress is enough?
But back to your question: do you absolutely have to encode the file in such a strong way? Maybe just putting it through qCompress is enough?
May be. But I (and the app) should now a password to decompress it back, shouldn't I? For example I have the SSID and PASS in the file to connect to the WIFI . I don't want to provide this sensitive info to anyone, but only authorized personal.
I think I'll do like this - the app runs on Linux - I generate the key based on some unique system data, like MAC address, so every app will have a unique key on a specific machine.
-
But back to your question: do you absolutely have to encode the file in such a strong way? Maybe just putting it through qCompress is enough?
May be. But I (and the app) should now a password to decompress it back, shouldn't I? For example I have the SSID and PASS in the file to connect to the WIFI . I don't want to provide this sensitive info to anyone, but only authorized personal.
I think I'll do like this - the app runs on Linux - I generate the key based on some unique system data, like MAC address, so every app will have a unique key on a specific machine.
@jenya7 said in Rewrite a file content in an existing file.:
But I (and the app) should now a password to decompress it back, shouldn't I?
No. You did not look into qCompress documentation. This is solution that's meant to obfuscate the data in simple way (where simple == not easily readable with text editor and not base64 - the latter is still present as "security" method which drives me nuts).
If you want to password/key protect the access in autonomous mode then I am afraid you need to investigate linux ways to do it. Those will be OS-dependent. Again - and I can't stress it enough - storing the key or password through the Qt provided ways will not be secure in any practical sense of the word.
-
@jenya7 said in Rewrite a file content in an existing file.:
But I (and the app) should now a password to decompress it back, shouldn't I?
No. You did not look into qCompress documentation. This is solution that's meant to obfuscate the data in simple way (where simple == not easily readable with text editor and not base64 - the latter is still present as "security" method which drives me nuts).
If you want to password/key protect the access in autonomous mode then I am afraid you need to investigate linux ways to do it. Those will be OS-dependent. Again - and I can't stress it enough - storing the key or password through the Qt provided ways will not be secure in any practical sense of the word.
-
If you are using a password manager or drive encryption the secret (i.e. the actual key) is protected by a password, i.e. the password is used for encryption of other passwords or keys. This also means that you need to type in the password to decrypt the data. Without user interaction this approach will not be able to access any secured data.
All other software I know of stores passwords or keys directly; either as plain text or maybe obfuscated (like @artwaw suggested). There is no other protection. This means that everyone who can access the data has access to passwords or keys. Like I said already, SSH keys are store plain in the users folder. What is the protection here? The simplest way to protect this data is access privileges. By default the user's home directory is only accessible by the user (and also root or any super user can easily gain access). This is safe as long as no one rips out your hard drive and puts it into their computer where they control who has access to which files. The only way to prevent that is file system encryption. File system encryption means that you have to enter the password every time you mount the filesystem (usually at startup). Still, root and any super user can get access to this data.
If you think of your key more like a regular password, you should read up on password hashing. Use a random salt to hash the password and then store the salt and the hash. When the user enters the password again, combine it with the stored salt and hash it again. If the hash matches the user entered the correct password. However, this approach always required interaction from a person. If you want to decrypt autonomously you have to store the key plainly (or obfuscated) somewhere. Your best bet is to use system api secure storage like @artwaw suggested.
Also: Using the MAC for encryption is also just obfuscation as this is a well known "secret" anybody can figure out.
Finally: Security through obscurity is no real security at all.