I do not understand why this generates 2 files instead of one:
have the same names, but one (that is ok) has the right extension (extension
) and is xxxxBytes, while the other has no extension (file type is) and is 0Bytes.
Stream my1Stream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((my1Stream = saveFileDialog1.OpenFile()) != null)
{
fileout = saveFileDialog1.FileName + extension;
passwordBytes = GetPasswordBytes();
my1Stream.Close();
AES.EncryptFile(filein, fileout, passwordBytes);
MessageBox.Show("File Criptato!");
}
}
the extension is derived from filein
(in a OpenFileDialog) and declared in the form: private string extension
:
filein = openFileDialog1.FileName;
extension = Path.GetExtension(filein);
From the MSDN page on SaveFileDialog.OpenFile method
For security purposes, this method creates a new file with the selected name and opens it with read/write permissions. This can cause unintentional loss of data if you select an existing file to save to
So this line
if ((my1Stream = saveFileDialog1.OpenFile()) != null)
creates a file with the name selected and with zero bytes. Then your code continues creating the file in the AES.Encryptfile call with tne name of fileOut
You could simply write
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileout = saveFileDialog1.FileName;
passwordBytes = GetPasswordBytes();
AES.EncryptFile(filein, fileout, passwordBytes);
MessageBox.Show("File Criptato!");
}