/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
partial class Directory
{
#region Decrypt
/// [AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.
///
///
///
///
///
///
/// A path that describes a directory to decrypt.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void Decrypt(string path, PathFormat pathFormat)
{
EncryptDecryptDirectoryCore(path, false, false, pathFormat);
}
/// [AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.
///
///
///
///
///
///
/// A path that describes a directory to decrypt.
/// to decrypt the directory recursively. only decrypt the directory.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void Decrypt(string path, bool recursive, PathFormat pathFormat)
{
EncryptDecryptDirectoryCore(path, false, recursive, pathFormat);
}
/// [AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.
///
///
///
///
///
///
/// A path that describes a directory to decrypt.
[SecurityCritical]
public static void Decrypt(string path)
{
EncryptDecryptDirectoryCore(path, false, false, PathFormat.RelativePath);
}
/// [AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.
///
///
///
///
///
///
/// A path that describes a directory to decrypt.
/// to decrypt the directory recursively. only decrypt the directory.
[SecurityCritical]
public static void Decrypt(string path, bool recursive)
{
EncryptDecryptDirectoryCore(path, false, recursive, PathFormat.RelativePath);
}
#endregion // Decrypt
#region Encrypt
/// [AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.
///
///
///
///
///
///
/// A path that describes a directory to encrypt.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void Encrypt(string path, PathFormat pathFormat)
{
EncryptDecryptDirectoryCore(path, true, false, pathFormat);
}
/// [AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.
///
///
///
///
///
///
/// A path that describes a directory to encrypt.
/// to encrypt the directory recursively. only encrypt the directory.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void Encrypt(string path, bool recursive, PathFormat pathFormat)
{
EncryptDecryptDirectoryCore(path, true, recursive, pathFormat);
}
/// [AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.
///
///
///
///
///
///
/// A path that describes a directory to encrypt.
[SecurityCritical]
public static void Encrypt(string path)
{
EncryptDecryptDirectoryCore(path, true, false, PathFormat.RelativePath);
}
/// [AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.
///
///
///
///
///
///
/// A path that describes a directory to encrypt.
/// to encrypt the directory recursively. only encrypt the directory.
[SecurityCritical]
public static void Encrypt(string path, bool recursive)
{
EncryptDecryptDirectoryCore(path, true, recursive, PathFormat.RelativePath);
}
#endregion // Encrypt
#region DisableEncryption
/// [AlphaFS] Disables encryption of the specified directory and the files in it.
/// This method only creates/modifies the file "Desktop.ini" in the root of and disables encryption by writing: "Disable=1"
/// This method does not affect encryption of files and subdirectories below the indicated directory.
///
/// The name of the directory for which to disable encryption.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void DisableEncryption(string path, PathFormat pathFormat)
{
EnableDisableEncryptionCore(path, false, pathFormat);
}
/// [AlphaFS] Disables encryption of the specified directory and the files in it.
/// This method only creates/modifies the file "Desktop.ini" in the root of and disables encryption by writing: "Disable=1"
/// This method does not affect encryption of files and subdirectories below the indicated directory.
///
/// The name of the directory for which to disable encryption.
[SecurityCritical]
public static void DisableEncryption(string path)
{
EnableDisableEncryptionCore(path, false, PathFormat.RelativePath);
}
#endregion // DisableEncryption
#region EnableEncryption
/// [AlphaFS] Enables encryption of the specified directory and the files in it.
/// This method only creates/modifies the file "Desktop.ini" in the root of and enables encryption by writing: "Disable=0"
/// This method does not affect encryption of files and subdirectories below the indicated directory.
///
/// The name of the directory for which to enable encryption.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void EnableEncryption(string path, PathFormat pathFormat)
{
EnableDisableEncryptionCore(path, true, pathFormat);
}
/// [AlphaFS] Enables encryption of the specified directory and the files in it.
/// This method only creates/modifies the file "Desktop.ini" in the root of and enables encryption by writing: "Disable=0"
/// This method does not affect encryption of files and subdirectories below the indicated directory.
///
/// The name of the directory for which to enable encryption.
[SecurityCritical]
public static void EnableEncryption(string path)
{
EnableDisableEncryptionCore(path, true, PathFormat.RelativePath);
}
#endregion // EnableEncryption
#region Internal Methods
/// Enables/disables encryption of the specified directory and the files in it.
/// This method only creates/modifies the file "Desktop.ini" in the root of and enables/disables encryption by writing: "Disable=0" or "Disable=1".
/// This method does not affect encryption of files and subdirectories below the indicated directory.
///
/// The name of the directory for which to enable encryption.
/// enabled encryption, disables encryption.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
internal static void EnableDisableEncryptionCore(string path, bool enable, PathFormat pathFormat)
{
if (Utils.IsNullOrWhiteSpace(path))
throw new ArgumentNullException("path");
string pathLp = Path.GetExtendedLengthPathCore(null, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
// EncryptionDisable()
// In the ANSI version of this function, the name is limited to 248 characters.
// To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
// 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
if (!NativeMethods.EncryptionDisable(pathLp, !enable))
NativeError.ThrowException(pathLp);
}
/// Decrypts/encrypts a directory recursively so that only the account used to encrypt the directory can decrypt it.
///
///
///
///
///
///
/// A path that describes a directory to encrypt.
/// encrypt, decrypt.
/// to decrypt the directory recursively. only decrypt files and directories in the root of .
/// Indicates the format of the path parameter(s).
[SecurityCritical]
internal static void EncryptDecryptDirectoryCore(string path, bool encrypt, bool recursive, PathFormat pathFormat)
{
string pathLp = Path.GetExtendedLengthPathCore(null, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
var options = DirectoryEnumerationOptions.FilesAndFolders | DirectoryEnumerationOptions.AsLongPath;
// Process folders and files when recursive.
if (recursive)
{
options |= DirectoryEnumerationOptions.Recursive;
foreach (string fsei in EnumerateFileSystemEntryInfosCore(null, pathLp, Path.WildcardStarMatchAll, options, PathFormat.LongFullPath))
File.EncryptDecryptFileCore(true, fsei, encrypt, PathFormat.LongFullPath);
}
// Process the root folder, the given path.
File.EncryptDecryptFileCore(true, pathLp, encrypt, PathFormat.LongFullPath);
}
#endregion // Internal Methods
}
}