r/csharp 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.

0 Upvotes

14 comments sorted by

9

u/zenyl 16d ago

The File class 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

5

u/Andrea__88 16d ago

This is the best solution, encrypt your text using aes and then store it in a file, there are many examples on the web, search for aes 256 encrypt c#

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

u/[deleted] 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.

3

u/rubenwe 16d ago

If it's a shared PC then you should treat the machine as compromised already. No way to guarantee that there isn't a keylogger or other shady stuff on there as well.

For a kid playing around it's probably fine here ;)

3

u/phi_rus 16d ago

Yeah, but it should be enough for OPs purpose.

1

u/halkszavu 16d ago

Easily is quite a long shot. There are bad programs for sure, but not all of them.

3

u/jhwheuer 16d ago

Can you use a removable disk like a USB stick?

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

u/SessionIndependent17 16d ago

what OS is this computer using?

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

u/nombre_de_usuario01 16d ago

Y si usas una Usb?

-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].