Example #1
0
        public void File_Encrypt()
        {
            Console.WriteLine("File.Encrypt()");

            // Create file and append text.
            var tempFile = Path.GetTempFileName();

            // Append text as UTF-8, default.
            File.AppendAllText(tempFile, UnitTestConstants.TextHelloWorld);

            var utf8             = NativeMethods.DefaultFileEncoding.BodyName.ToUpperInvariant();
            var readText8        = File.ReadAllText(tempFile);
            var actual           = File.GetAttributes(tempFile);
            var encryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tCreated {0} file: [{1}]", utf8, tempFile);
            Console.WriteLine("\tContent: [{0}]", readText8);
            Console.WriteLine("\n\tFile.GetAttributes(): [{0}]", actual);
            Console.WriteLine("\tEncryption status   : [{0}]", encryptionStatus);

            var encryptOk = false;

            try
            {
                File.Encrypt(tempFile);
                encryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            encryptionStatus = File.GetEncryptionStatus(tempFile);
            Console.WriteLine("\n\tFile.Encrypt() (Should be True): [{0}]", encryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tEncryption status              : [{0}]", encryptionStatus);

            var decryptOk = false;

            try
            {
                File.Decrypt(tempFile);
                decryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            var decryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tFile.Decrypt() (Should be True): [{0}]:", decryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tDecryption status              : [{0}]", decryptionStatus);

            Assert.IsTrue(encryptOk, "File should be encrypted.");
            Assert.IsTrue(encryptionStatus == FileEncryptionStatus.Encrypted, "File should be encrypted.");
            Assert.IsTrue(decryptOk, "File should be decrypted.");
            Assert.IsTrue(decryptionStatus == FileEncryptionStatus.Encryptable, "File should be decrypted.");

            File.Delete(tempFile, true);
            Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed.");
        }
Example #2
0
 public void Encrypt()
 {
     File.Encrypt(FullPath);
 }