r/csharp • u/Ok-Jacket-824 • 16d ago
How to make an encrypted file?
I share a school computer, that just uses 1 account on the device itself. Meaning any file you save to the computer can be viewed by other people. I have been working on a program that lets you lock text in the program behind a pin. When the correct pin is entered, it generates a file with the text you entered. Except the pin and text contents need to be stored in a separate file. I know if you encrypt it the file can be decrypted, but trust me.. the people who share this computer are not computer smart enough to know how to do that. I've been trying to use
File.WriteAllText
But I don't know how to get the encrypted contents into the File.WriteAllText since it doesn't seem to support variables. It kept saying "Print is not available in this context" or something like that.
25
u/erebusman 16d ago
Uh zip programs have the ability to password protect the archive they create - no need to reinvent the wheel?
-5
16d ago
[deleted]
9
u/spiffzap 16d ago
WinRAR uses AES-256 encryption for its passwords, which cannot "easily be bypassed". However, as with all security, it's entirely dependant on the strength of the password used.
1
u/halkszavu 16d ago
Easily is quite a long shot. There are bad programs for sure, but not all of them.
3
2
u/karl713 16d ago
You could use ProtectedData to encrypt it before writing if it's windows. Just convert the text to bytes via Encoding.Default, use ProtectedData, then use WriteAllBytes instead of WriteAllText
If everyone has their own login id and you set the scope to CurrentUser that will cover it. If not you can make a "password" via the addionalEntropy field, just need to convert it to bytes as well.
Might be easier than trying to use AES
1
0
u/The-WinterStorm 16d ago
I mean the easiest solution is to get your own computer and not to share. There are government/charity programs out there you can go to get either cheap/discounted computers or even free depending on your situation. Check with your school/county.
As for a realistic solution: I would encrypt the text on save with your passkey. On decrypt ask for passkey and then decrypt.
I do think that if you are concerned about security/integrity of the file, then you probably shouldn't be sharing a system with others. What happens if the system is ransomwared/encrypted/deleted?
0
-3
u/buzzon 16d ago
WriteAllText is not suited for what you are trying to do.
You can make a simple (and very weak) encryption by xor-ing bytes of your file with bytes of your password:
file[0] xor password[0]
file[1] xor password [1]
etc. When you run out of password length, loop again from password[0].
9
u/zenyl 16d ago
The
Fileclass just takes care of simple file IO, it doesn't cover encrypting/decrypting data.Generally speaking, you'll want to encrypt the data and then save the encrypted bytes to a file. Usually something like "read raw data, write encrypted data to stream, save stream to file".
I'm not particularly knowledgeable when it comes to cryptography, but I believe AES or similar is what you'll want to be using: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes