|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /* 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
-
- /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to decrypt.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void Decrypt(string path, PathFormat pathFormat)
- {
- EncryptDecryptDirectoryCore(path, false, false, pathFormat);
- }
-
- /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to decrypt.</param>
- /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void Decrypt(string path, bool recursive, PathFormat pathFormat)
- {
- EncryptDecryptDirectoryCore(path, false, recursive, pathFormat);
- }
-
- /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to decrypt.</param>
- [SecurityCritical]
- public static void Decrypt(string path)
- {
- EncryptDecryptDirectoryCore(path, false, false, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to decrypt.</param>
- /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt the directory.</param>
- [SecurityCritical]
- public static void Decrypt(string path, bool recursive)
- {
- EncryptDecryptDirectoryCore(path, false, recursive, PathFormat.RelativePath);
- }
-
- #endregion // Decrypt
-
- #region Encrypt
-
- /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to encrypt.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void Encrypt(string path, PathFormat pathFormat)
- {
- EncryptDecryptDirectoryCore(path, true, false, pathFormat);
- }
-
- /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to encrypt.</param>
- /// <param name="recursive"><see langword="true"/> to encrypt the directory recursively. <see langword="false"/> only encrypt the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void Encrypt(string path, bool recursive, PathFormat pathFormat)
- {
- EncryptDecryptDirectoryCore(path, true, recursive, pathFormat);
- }
-
-
- /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to encrypt.</param>
- [SecurityCritical]
- public static void Encrypt(string path)
- {
- EncryptDecryptDirectoryCore(path, true, false, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to encrypt.</param>
- /// <param name="recursive"><see langword="true"/> to encrypt the directory recursively. <see langword="false"/> only encrypt the directory.</param>
- [SecurityCritical]
- public static void Encrypt(string path, bool recursive)
- {
- EncryptDecryptDirectoryCore(path, true, recursive, PathFormat.RelativePath);
- }
-
- #endregion // Encrypt
-
- #region DisableEncryption
-
- /// <summary>[AlphaFS] Disables encryption of the specified directory and the files in it.
- /// <para>This method only creates/modifies the file "Desktop.ini" in the root of <paramref name="path"/> and disables encryption by writing: "Disable=1"</para>
- /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
- /// </summary>
- /// <param name="path">The name of the directory for which to disable encryption.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void DisableEncryption(string path, PathFormat pathFormat)
- {
- EnableDisableEncryptionCore(path, false, pathFormat);
- }
-
- /// <summary>[AlphaFS] Disables encryption of the specified directory and the files in it.
- /// <para>This method only creates/modifies the file "Desktop.ini" in the root of <paramref name="path"/> and disables encryption by writing: "Disable=1"</para>
- /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
- /// </summary>
- /// <param name="path">The name of the directory for which to disable encryption.</param>
- [SecurityCritical]
- public static void DisableEncryption(string path)
- {
- EnableDisableEncryptionCore(path, false, PathFormat.RelativePath);
- }
-
- #endregion // DisableEncryption
-
- #region EnableEncryption
-
- /// <summary>[AlphaFS] Enables encryption of the specified directory and the files in it.
- /// <para>This method only creates/modifies the file "Desktop.ini" in the root of <paramref name="path"/> and enables encryption by writing: "Disable=0"</para>
- /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
- /// </summary>
- /// <param name="path">The name of the directory for which to enable encryption.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SecurityCritical]
- public static void EnableEncryption(string path, PathFormat pathFormat)
- {
- EnableDisableEncryptionCore(path, true, pathFormat);
- }
-
- /// <summary>[AlphaFS] Enables encryption of the specified directory and the files in it.
- /// <para>This method only creates/modifies the file "Desktop.ini" in the root of <paramref name="path"/> and enables encryption by writing: "Disable=0"</para>
- /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
- /// </summary>
- /// <param name="path">The name of the directory for which to enable encryption.</param>
- [SecurityCritical]
- public static void EnableEncryption(string path)
- {
- EnableDisableEncryptionCore(path, true, PathFormat.RelativePath);
- }
-
- #endregion // EnableEncryption
-
- #region Internal Methods
-
- /// <summary>Enables/disables encryption of the specified directory and the files in it.
- /// <para>This method only creates/modifies the file "Desktop.ini" in the root of <paramref name="path"/> and enables/disables encryption by writing: "Disable=0" or "Disable=1".</para>
- /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
- /// </summary>
- /// <param name="path">The name of the directory for which to enable encryption.</param>
- /// <param name="enable"><see langword="true"/> enabled encryption, <see langword="false"/> disables encryption.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
- /// <summary>Decrypts/encrypts a directory recursively so that only the account used to encrypt the directory can decrypt it.</summary>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">A path that describes a directory to encrypt.</param>
- /// <param name="encrypt"><see langword="true"/> encrypt, <see langword="false"/> decrypt.</param>
- /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt files and directories in the root of <paramref name="path"/>.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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<string>(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
- }
- }
|