2

As per this CodeProject, I have encrypted and decrypted file. At the end of the code, I am setting file attribute as Encrypted.

fsIn.Close();
cs.Close();
fsCrypt.Close();
// Mark the file as Encrypted
File.SetAttributes(outputFile, File.GetAttributes(outputFile) | FileAttributes.Encrypted);

and when I am done with decrypt.

// Mark the file Decrypted
File.SetAttributes(outputFile, File.GetAttributes(outputFile) & ~FileAttributes.Encrypted);

And here is how I am checking:

public static bool IsFileEncrypted(string filePath)
{
   FileAttributes attributes = File.GetAttributes(filePath);
   return (attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted;
}

The problem is, variable attributes shows only Archive attribute.

How I can check whether File is encrypted or not? Is there any other solution?

Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81
  • 1
    https://stackoverflow.com/a/12875282/1663001 Note that attribute is whether the file system has encrypted the file, not you. – DavidG Aug 25 '17 at 11:50
  • Have you checked that the file gets encrypted? May be its stored on a filesystem that does not support encryption? – Michael Aug 25 '17 at 11:51
  • I need to check it through file system? – Faizan Mubasher Aug 25 '17 at 11:53
  • Yes and no. Depending on what you want. If you encrypt the file with AES or rijndael you can choose a password and there is also no need to set the encrypted flag. Or you let the OS do the encryption, then you use - if i remember correctly - `File.Encrypt`. By that you can't specify a password and the encrypted file can only be used/decrypted by the user-account that has encrypted the file. – Michael Aug 25 '17 at 11:59
  • Ok, using `File.Encrypt` was causing some exception notifying some certificate is invalid or expired. – Faizan Mubasher Aug 25 '17 at 12:07

0 Answers0