WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Encryption

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Encrypt and Decrypt Using EFS


Question:

Can anyone point me in the right direction on how I to use Winbatch to encrypt and decrypt files using EFS (Windows Encrypted File System)? Thank you.

Answer:

Sorry no builtin EFS support. However if there is a commandline utility that accomplishes this you can automate that or this API looks interesting, you might be able to use DLLCALL(): http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/windataprotection-dpapi.asp

For anyone wanting to use it with a run command and cmd here it is:

RunWait('cmd.exe', '/c cipher /e /a /s:c:\temp')
to decrypt use a /d in place of the /e.

*OR*

;; May not be what you are looking for but here it is anyway.
;; Could be enhanced.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EncryptFile - Encripts file using default context
;;
;; In - full file name.
;;
;; Return - Non-zero on success, zero on failure.
;;
;; Notes - Must have correct access rights to use.
;;       - Does not error if file aready encrypted.
;;       - Win2k or later.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction EncryptFile( sFileName )
   sAdvapi = StrCat(DirWindows(1),"Advapi32.dll")

   LastError()
   nErrorMode = ErrorMode(@OFF)
   bResult = DllCall(sAdvapi,long:"EncryptFileA", lpstr:sFileName)
   ErrorMode(nErrorMode)

   If !bResult
      ; Lazy error handling.
      Message("EncryptFile Error", DllLastError())
   EndIf

   Return bResult
#EndFunction

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DecryptFile - Decrypts file using default context
;;
;; In - full file name.
;;
;; Return - Non-zero on success, zero on failure.
;;
;; Notes - Must have correct access rights to use.
;;       - Does not error if file is not encrypted.
;;       - Win2k or later.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction DecryptFile( sFileName )
   sAdvapi =StrCat(DirWindows(1),"Advapi32.dll")

   LastError()
   nErrorMode = ErrorMode(@OFF)
   bResult = DllCall(sAdvapi,long:"DecryptFileA", lpstr:sFileName, long:0)
   ErrorMode(nErrorMode)

   If !bResult
      ; Lazy error handling.
      Message("DecryptFile Error", DllLastError())
   EndIf

   Return bResult
#EndFunction

sFile = "c:\temp\test.txt"
bResult = EncryptFile(sFile)
Message("Encrypt test", bResult)

If  bResult
   bResult = DecryptFile(sFile)
   Message("Decrypt test", bResult)
EndIf

Article ID:   W17251
File Created: 2007:07:03:14:29:00
Last Updated: 2007:07:03:14:29:00