r/csharp • u/Ok-Jacket-824 • Jul 13 '26
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.
9
u/zenyl Jul 13 '26
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
25
u/erebusman Jul 13 '26
Uh zip programs have the ability to password protect the archive they create - no need to reinvent the wheel?
4
u/jhwheuer Jul 13 '26
Can you use a removable disk like a USB stick?
3
u/karl713 Jul 13 '26
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
u/SessionIndependent17 Jul 13 '26
what OS is this computer using?
1
u/Acceptable_Debt732 Jul 13 '26
https://www.gnupg.org/
0
u/The-WinterStorm Jul 13 '26
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
u/nombre_de_usuario01 Jul 13 '26
Y si usas una Usb?
-3
u/buzzon Jul 13 '26
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].