Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

273 Zeilen
14 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.IO;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. partial class Directory
  27. {
  28. #region Decrypt
  29. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  30. /// <exception cref="ArgumentException"/>
  31. /// <exception cref="ArgumentNullException"/>
  32. /// <exception cref="DirectoryNotFoundException"/>
  33. /// <exception cref="IOException"/>
  34. /// <exception cref="NotSupportedException"/>
  35. /// <exception cref="UnauthorizedAccessException"/>
  36. /// <param name="path">A path that describes a directory to decrypt.</param>
  37. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  38. [SecurityCritical]
  39. public static void Decrypt(string path, PathFormat pathFormat)
  40. {
  41. EncryptDecryptDirectoryCore(path, false, false, pathFormat);
  42. }
  43. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  44. /// <exception cref="ArgumentException"/>
  45. /// <exception cref="ArgumentNullException"/>
  46. /// <exception cref="DirectoryNotFoundException"/>
  47. /// <exception cref="IOException"/>
  48. /// <exception cref="NotSupportedException"/>
  49. /// <exception cref="UnauthorizedAccessException"/>
  50. /// <param name="path">A path that describes a directory to decrypt.</param>
  51. /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt the directory.</param>
  52. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  53. [SecurityCritical]
  54. public static void Decrypt(string path, bool recursive, PathFormat pathFormat)
  55. {
  56. EncryptDecryptDirectoryCore(path, false, recursive, pathFormat);
  57. }
  58. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  59. /// <exception cref="ArgumentException"/>
  60. /// <exception cref="ArgumentNullException"/>
  61. /// <exception cref="DirectoryNotFoundException"/>
  62. /// <exception cref="IOException"/>
  63. /// <exception cref="NotSupportedException"/>
  64. /// <exception cref="UnauthorizedAccessException"/>
  65. /// <param name="path">A path that describes a directory to decrypt.</param>
  66. [SecurityCritical]
  67. public static void Decrypt(string path)
  68. {
  69. EncryptDecryptDirectoryCore(path, false, false, PathFormat.RelativePath);
  70. }
  71. /// <summary>[AlphaFS] Decrypts a directory that was encrypted by the current account using the Encrypt method.</summary>
  72. /// <exception cref="ArgumentException"/>
  73. /// <exception cref="ArgumentNullException"/>
  74. /// <exception cref="DirectoryNotFoundException"/>
  75. /// <exception cref="IOException"/>
  76. /// <exception cref="NotSupportedException"/>
  77. /// <exception cref="UnauthorizedAccessException"/>
  78. /// <param name="path">A path that describes a directory to decrypt.</param>
  79. /// <param name="recursive"><see langword="true"/> to decrypt the directory recursively. <see langword="false"/> only decrypt the directory.</param>
  80. [SecurityCritical]
  81. public static void Decrypt(string path, bool recursive)
  82. {
  83. EncryptDecryptDirectoryCore(path, false, recursive, PathFormat.RelativePath);
  84. }
  85. #endregion // Decrypt
  86. #region Encrypt
  87. /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
  88. /// <exception cref="ArgumentException"/>
  89. /// <exception cref="ArgumentNullException"/>
  90. /// <exception cref="DirectoryNotFoundException"/>
  91. /// <exception cref="IOException"/>
  92. /// <exception cref="NotSupportedException"/>
  93. /// <exception cref="UnauthorizedAccessException"/>
  94. /// <param name="path">A path that describes a directory to encrypt.</param>
  95. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  96. [SecurityCritical]
  97. public static void Encrypt(string path, PathFormat pathFormat)
  98. {
  99. EncryptDecryptDirectoryCore(path, true, false, pathFormat);
  100. }
  101. /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
  102. /// <exception cref="ArgumentException"/>
  103. /// <exception cref="ArgumentNullException"/>
  104. /// <exception cref="DirectoryNotFoundException"/>
  105. /// <exception cref="IOException"/>
  106. /// <exception cref="NotSupportedException"/>
  107. /// <exception cref="UnauthorizedAccessException"/>
  108. /// <param name="path">A path that describes a directory to encrypt.</param>
  109. /// <param name="recursive"><see langword="true"/> to encrypt the directory recursively. <see langword="false"/> only encrypt the directory.</param>
  110. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  111. [SecurityCritical]
  112. public static void Encrypt(string path, bool recursive, PathFormat pathFormat)
  113. {
  114. EncryptDecryptDirectoryCore(path, true, recursive, pathFormat);
  115. }
  116. /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
  117. /// <exception cref="ArgumentException"/>
  118. /// <exception cref="ArgumentNullException"/>
  119. /// <exception cref="DirectoryNotFoundException"/>
  120. /// <exception cref="IOException"/>
  121. /// <exception cref="NotSupportedException"/>
  122. /// <exception cref="UnauthorizedAccessException"/>
  123. /// <param name="path">A path that describes a directory to encrypt.</param>
  124. [SecurityCritical]
  125. public static void Encrypt(string path)
  126. {
  127. EncryptDecryptDirectoryCore(path, true, false, PathFormat.RelativePath);
  128. }
  129. /// <summary>[AlphaFS] Encrypts a directory so that only the account used to encrypt the directory can decrypt it.</summary>
  130. /// <exception cref="ArgumentException"/>
  131. /// <exception cref="ArgumentNullException"/>
  132. /// <exception cref="DirectoryNotFoundException"/>
  133. /// <exception cref="IOException"/>
  134. /// <exception cref="NotSupportedException"/>
  135. /// <exception cref="UnauthorizedAccessException"/>
  136. /// <param name="path">A path that describes a directory to encrypt.</param>
  137. /// <param name="recursive"><see langword="true"/> to encrypt the directory recursively. <see langword="false"/> only encrypt the directory.</param>
  138. [SecurityCritical]
  139. public static void Encrypt(string path, bool recursive)
  140. {
  141. EncryptDecryptDirectoryCore(path, true, recursive, PathFormat.RelativePath);
  142. }
  143. #endregion // Encrypt
  144. #region DisableEncryption
  145. /// <summary>[AlphaFS] Disables encryption of the specified directory and the files in it.
  146. /// <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>
  147. /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
  148. /// </summary>
  149. /// <param name="path">The name of the directory for which to disable encryption.</param>
  150. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  151. [SecurityCritical]
  152. public static void DisableEncryption(string path, PathFormat pathFormat)
  153. {
  154. EnableDisableEncryptionCore(path, false, pathFormat);
  155. }
  156. /// <summary>[AlphaFS] Disables encryption of the specified directory and the files in it.
  157. /// <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>
  158. /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
  159. /// </summary>
  160. /// <param name="path">The name of the directory for which to disable encryption.</param>
  161. [SecurityCritical]
  162. public static void DisableEncryption(string path)
  163. {
  164. EnableDisableEncryptionCore(path, false, PathFormat.RelativePath);
  165. }
  166. #endregion // DisableEncryption
  167. #region EnableEncryption
  168. /// <summary>[AlphaFS] Enables encryption of the specified directory and the files in it.
  169. /// <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>
  170. /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
  171. /// </summary>
  172. /// <param name="path">The name of the directory for which to enable encryption.</param>
  173. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  174. [SecurityCritical]
  175. public static void EnableEncryption(string path, PathFormat pathFormat)
  176. {
  177. EnableDisableEncryptionCore(path, true, pathFormat);
  178. }
  179. /// <summary>[AlphaFS] Enables encryption of the specified directory and the files in it.
  180. /// <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>
  181. /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
  182. /// </summary>
  183. /// <param name="path">The name of the directory for which to enable encryption.</param>
  184. [SecurityCritical]
  185. public static void EnableEncryption(string path)
  186. {
  187. EnableDisableEncryptionCore(path, true, PathFormat.RelativePath);
  188. }
  189. #endregion // EnableEncryption
  190. #region Internal Methods
  191. /// <summary>Enables/disables encryption of the specified directory and the files in it.
  192. /// <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>
  193. /// <para>This method does not affect encryption of files and subdirectories below the indicated directory.</para>
  194. /// </summary>
  195. /// <param name="path">The name of the directory for which to enable encryption.</param>
  196. /// <param name="enable"><see langword="true"/> enabled encryption, <see langword="false"/> disables encryption.</param>
  197. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  198. [SecurityCritical]
  199. internal static void EnableDisableEncryptionCore(string path, bool enable, PathFormat pathFormat)
  200. {
  201. if (Utils.IsNullOrWhiteSpace(path))
  202. throw new ArgumentNullException("path");
  203. string pathLp = Path.GetExtendedLengthPathCore(null, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  204. // EncryptionDisable()
  205. // In the ANSI version of this function, the name is limited to 248 characters.
  206. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  207. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  208. if (!NativeMethods.EncryptionDisable(pathLp, !enable))
  209. NativeError.ThrowException(pathLp);
  210. }
  211. /// <summary>Decrypts/encrypts a directory recursively so that only the account used to encrypt the directory can decrypt it.</summary>
  212. /// <exception cref="ArgumentException"/>
  213. /// <exception cref="ArgumentNullException"/>
  214. /// <exception cref="DirectoryNotFoundException"/>
  215. /// <exception cref="IOException"/>
  216. /// <exception cref="NotSupportedException"/>
  217. /// <exception cref="UnauthorizedAccessException"/>
  218. /// <param name="path">A path that describes a directory to encrypt.</param>
  219. /// <param name="encrypt"><see langword="true"/> encrypt, <see langword="false"/> decrypt.</param>
  220. /// <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>
  221. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  222. [SecurityCritical]
  223. internal static void EncryptDecryptDirectoryCore(string path, bool encrypt, bool recursive, PathFormat pathFormat)
  224. {
  225. string pathLp = Path.GetExtendedLengthPathCore(null, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  226. var options = DirectoryEnumerationOptions.FilesAndFolders | DirectoryEnumerationOptions.AsLongPath;
  227. // Process folders and files when recursive.
  228. if (recursive)
  229. {
  230. options |= DirectoryEnumerationOptions.Recursive;
  231. foreach (string fsei in EnumerateFileSystemEntryInfosCore<string>(null, pathLp, Path.WildcardStarMatchAll, options, PathFormat.LongFullPath))
  232. File.EncryptDecryptFileCore(true, fsei, encrypt, PathFormat.LongFullPath);
  233. }
  234. // Process the root folder, the given path.
  235. File.EncryptDecryptFileCore(true, pathLp, encrypt, PathFormat.LongFullPath);
  236. }
  237. #endregion // Internal Methods
  238. }
  239. }