The problem of encrypting and decrypting files in C #

Played with encryption and decryption files in VC # Express 2010.

All the tutorials and documentation I've seen require two FileSteams to encrypt a file. One for reading an unencrypted version, and another for encryption. When I actually wrote the code, it always threw an error telling me that it could not open the file because it was opened by another process in the output stream.

I assume this is because the file is opened by the input file stream. So I have to specify a different file name? Thus, even after a successful operation, I find out that the original unencrypted file in the directory and a separate encrypted version? Doesn't that conquer the point? Or am I doing something wrong? My code is like this ...

public string filename = "test.xml"; using(FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read)) using(FileStream output = new FileStram(filename, FileMode.Open, FileAccess.Write)) using(....all the crypto stream and transform stuf...) { ...do the encryption.... } 
+4
source share
4 answers

You are right, but that does not defeat the point. Cryptographic APIs (streaming) are designed for encryption from Src to Dst. Consider encrypting output when sending / receiving over a network, etc. This simplifies them, as they should.

You complicate the problem using the same file for Src and Dst. This is not entirely impossible, but, like copying a file, it needs extra care.

Note that in general, encryption will increase the file size. Therefore, do not encrypt the file in place. Decryption can be, but I would not risk it.

What you need is a Temp file and a rename action after completion.

+3
source

In your example, you cannot create a separate stream for input and output in a single file, but you can create a handle that will read and write. The FileAccess enumeration has a flags attribute, so you just say var handle = new FileStream(filename, FileAccess.Read | FileAccess.Write); The obvious disadvantage of this is that you will lose data if your encryption does not succeed.

I recommend having a separate file for output, although at least this way you wonโ€™t lose data if your program terminates unexpectedly. If encryption is successful, delete the original and rename the encrypted file with the original file name.

+1
source

Use File.ReadAllBytes . Then these byte messages should work for your encryption.

0
source

There is another parameter in which you can specify whether to allow another process to read or write to the file

openFile is the file name, line type.

 using (FileStream fileIn = new FileStream(openFile, FileMode.Open, FileAccess.Read, FileShare.Write)) using (FileStream fileOut = new FileStream(openFile, FileMode.Open, FileAccess.Write, FileShare.Open)) 

This way you can read and write to the same file.

 while (myfileStream.Position < fileLength) { fileIn .Read(buffer, 0, 51200); buffer = encrypt(buffer); fileOut .Write(buffer, 0, 51200); } 

Although itโ€™s easy and you donโ€™t need to write to a temporary file or move / rename it, etc., it can be really dangerous if for some reason the encryption suddenly breaks, you will lose data! A

In addition, the encryption function is what I implemented. AesCryptoServiceProvider together with CryptoStream can be used :)

0
source

Source: https://habr.com/ru/post/1315315/


All Articles