What is the secure way to encrypt a file with a key? Since it will be a small file, is using AES good enough for this assuming I keep the key in memory? So what I can do is just use AES for encryption/decryption - or am I am missing something?
What improves security is to not persist the key but rather keep it in memory only long enough to use it; then discard it. Use a key management service (KMS), backed by a hardware security module (HSM), to retrieve a key only when you need it. Once you're done with using the key for the immediate operation, you need to discard it from memory. Retrieve it later when necessary by accessing it again through the KMS.
Also, you might want to use a unique key per encrypt operation, sometimes called a data key. This key would itself be encrypted by a higher-level key and the ciphertext of it stored, until decryption is necessary. Using a unique key per encryption operation reduces your exposure in the event that the key is compromised.
You can try this on windows
To encrypt a folder or file
- Right-click the folder or file you want to encrypt, and then click Properties.
- Click the General tab, and then click Advanced.
- Select the Encrypt contents to secure data check box, click OK, and then click OK again.
To decrypt a folder or file
1.Right-click the folder or file you want to decrypt, and then click Properties.
2.Click the General tab, and then click Advanced.
3.Clear the Encrypt contents to secure data check box, click OK, and then click OK again
Other answers have already recommended AES encrytption, but I though it was worth sharing this link from NIST on recommended key lengths (and algorithms in fact). In summary, it uses an assessment of the rate of increase in computing power to work our a key length based on how long you want the file to be safe for.
So for example, AES-128 is considered OK if you need to keep your file safe up to 2030.
It might not be relevent for your use case if your file is pretty much ephemeral, but other searchers landing on this page might find it useful...