Program that saves user information to an encrypted file - c++

I'm writing a program that asks the user for a username and password. I also want to be able to save this information if they so choose, but I don't want to have my program create a simple plain text file that anyone can open. How can I have my c++ program write to an encrypted file and be able to access it when the program is launched later?

Try libgcrypt. For this sort of task, the key (generated from the user's password), will have to be entered every time. You don't want to save the key right next to the encrypted data.
You could also try some scripting with pgp or gpg, if you want to tackle it at a higher level.

Related

File Type For SaveFiles c++

Using fstream, what file type would you recommend that I can read and write to for save files? I'm attempting to make a program where the user enters a username and a password and it'll output it to a file. I'd like a filetype that isn't very easy for the user to edit. (so not a .txt or .ini)
Thanks in advance
fstream doesn't really have a sense of a "file format" outside of being in text mode vs pure binary mode (there are some nuances with newlines for example). You would typically use some sort of intermediate library to generate or directly write XML files, JSON files, INI files, etc. SQLite is a popular library/format to store database tables in a file. Mozilla Firefox uses it for their bookmarks and history for example.
What you're talking about is security through obscurity. By using using some sort of encoding, perhaps ultimately binary so it wouldn't even be printable at all without a hex editor, it would obscure things a bit. If you used encryption it wouldn't be readable until decrypted, but you could always reverse engineer the program to determine the encryption/decryption keys and algorithms.
The most secure option is to not store the password at all, but a hash of the password. This means that even with reverse engineering the program, you wouldn't be able to directly extract the stored password. Instead, whenever the user attempted to enter a password, it would compare use the same hashing algorithm and compare the results.
This doesn't always work though, because sometimes you are storing the password to submit to a 3rd party, in which case you do have to ultimately recover the stored password into plaintext for transmitting to the 3rd party.
I hope this gives you some ideas!

How to make a user changeable constant?

I want to make a simple console application that will execute some usefull functions that I regularly use for my school (to check if a number is prime, to write factors of a number, solve a quadratic equation etc.).
But as I was making it I decided to add some details about the app to make the user interface prettier: to write the name of the app, version ...
I came to an idea to have an admin password which I can type in and then I can modify those extra details or change the password.
But if I have a password that I store like a const variable or a regular variable const string password = "blablabla;" every time I run the app it will have the same password "blablabla" so is there a way to somehow change the password and other extra data so the program saves the new values permanently?
I thought to save all those extra information in some file and then read them from it every time, but then if the file is changed then the data will be to and I don't want that. I want to change the data only with an admin password. I also thought I can encrypt those information but still thay can be easely changed.
I also thought that the program can open the source code and then modify those initialized values, but then I need to compile that file and I also don't want that.
I only want an .exe app that will run and can change those information permanently with an admin password.
Does anyone now if this can be done and how?
I'm writing this app in C++.
Thank you in advance
is there a way to somehow change the password and other extra data so the program saves the new values permanently?
In theory, you may be able to modify the constants in the data segment of the executable. How to do that is dependent on the executable format. But I would consider that a kludgy workaround and it would be difficult to prevent someone from modifying the executable without the password. What you really want is variables. And you have a good idea for how to initialize the variables:
I thought to save all those extra information in some file
A good idea. Simple solution, probably appropriate for the scope of your app.
if the file is changed then the data will be to and I don't want that. I want to change the data only with an admin password.
Then protect the file somehow. You could, for example not give anyone write permission to the file. Or if you're paranoid (this is just a small console app just for you, right?) encrypt it...
I also thought I can encrypt those information but still thay can be easely changed.
It won't be easy without the key. Sure they can modify the cryptotext, resulting in garbage data, but if you are concerned about that, realize that if they have write access to your files, they can just delete the program itself.
If you want to go this far though, consider storing your data in a full-fledged database which will have authentication and encryption already implemented.
You need to add Windows resources to your application and then update them run-time.
https://msdn.microsoft.com/en-us/library/ms648004%28v=vs.85%29.aspx

How would to assign exclusive right of a file to a process in c++?

I am using visual studio to develop a windows form application in c++ to detect certain anomalies and log it.
The log file that I am intending to create will be of a common format such as .txt. I do not want users of the computer to modify this file that is I want only my program to modify it( I want users to read this file not modify it).
Is there any way to achieve this??
if you want to hide the content from other users then encrypt the file or use a binary format that only your program will be able to understand.
As long as the file resides on a publicly accessible area in the file system, then other users will be able to access it.
You can put the file in a specific user's Documents area, which might go some way to protecting the file from other users of that machine, but not from administrators.
You could even set the file attributes to "hidden" or something along those lines, to further make it hard for people to find it. But a complete access block is difficult (if not impossible).
After all this, you can also use NirMH's method to ensure that even if someone finds and attempts to read the file, then the encryption should make it difficult to crack it open. A binary format can still be read with a hex editor, if someone is really keen to read your file.

keep the password while reading encrypted file in C/C++ program

I need to open and read an encrypted file in a C++ program. Do I need to keep the password in the program? Are the user able to get the password when the program is disassembled?
What's the best way to keep the user away from the password?
If you just put the password hard coded then, yes. With some dissassembly it is easy to get the password (you don't even need to dissassemble fully, only get the strings).
You can try to obfuscate the password somehow, such as computing a hash of something that creates the password (this is very simplistic).
In the end if you give someone an encrypted file and a program that opens that file you can't really hide that information from them.
Another simplistic solution (provides very basic security only - may be succeptible to hacker attack )
Create your password file in a directory on which users have no permission.
Provide users only execute permissions to the executable but use setuid so that they can run the executable as you.
In the exe read the password from the file kept in a directory that only you have permission to. (created in step 1).

How to save a variable after runtime in Python 2.7

Alright, so I am having issues with getting input from the user that will be used in the program until it is ran again with certain options in the cmd.
So say a user runs the program from cmd with the argument GUI, this will open a Tk window that asks for a their email, the user presses submit, and the text from the entry box is saved to a variable, now it will be able to use it for that runtime, but at the next run, say with no parameters, it will not have anything assigned to that variable since it was cleared from the memory.
I would find it ideal if I could just have it save the variable somehow after the runtime, since the user will use the program like so until they got a new email, then they would just run it with the option GUI again to assign a new one. Right now I am using a .txt to do that, but I find that a bit unsecure even after encrypting the email/pass with base64 since it can easily be unencrypted. How would I do this in a more safe, and more portable way, since a user can easily forget not to delete the file, and to move the .txt file to the right directory.
First of all, base64 is not "encryption". It's just an encoding format, and saving anything in base64 will not protect it from being read.
I think the best solution for your case is to use some kind of system-level "keychain". Otherwise, just ask the password to the user every time it is needed, but of course that may become annoying.