Skip to content

Instantly share code, notes, and snippets.

@gsherman
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsherman/3ab016af442acd0d8d73 to your computer and use it in GitHub Desktop.
Save gsherman/3ab016af442acd0d8d73 to your computer and use it in GitHub Desktop.
bat file for encrypting and decrypting config files. In the bat file, there's a path to aspnet_regiis.exe that looks like: %windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe . That may need to be tweaked for your environment.
@echo off
REM depends on find_replace.vbs, assumed to be in same directory as this file (crypt.bat)
echo.
echo *** Encrypt/Decrypt a section of a Configuration file ***
echo.
if "%1" == "" goto usage
if "%2" == "" goto usage
if "%3" == "" goto usage
set operation=e
set options=-prov DataProtectionConfigurationProvider
if "%1" == "decrypt" (
set operation=d
set options=
)
if "%1" == "d" (
set operation=d
set options=
)
cscript //nologo "%~dp0find_replace.vbs" %2 "<configSections>" "<configSections><!--"
cscript //nologo "%~dp0find_replace.vbs" %2 "</configSections>" "--></configSections>"
rename %2 web.config
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -p%operation%f %3 . %options%
rename web.config %2
cscript //nologo "%~dp0find_replace.vbs" %2 "<configSections><!--" "<configSections>"
cscript //nologo "%~dp0find_replace.vbs" %2 "--></configSections>" "</configSections>"
goto done
:usage
echo.
echo Missing arguments!
echo Usage: crypt.bat operation(encrypt or decrypt) file-to-be-crypted section-within-the-file-to-be-crypted
echo.
:done
If WScript.Arguments.Count <> 3 then
WScript.Echo "usage: find_replace.vbs filename word_to_find replace_with "
WScript.Quit
end If
FindAndReplace WScript.Arguments.Item(0), WScript.Arguments.Item(1), WScript.Arguments.Item(2)
WScript.Echo "Operation Complete"
function FindAndReplace(strFile, strFind, strReplace)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile(strFile,1)
strTempDir = objFSO.GetSpecialFolder(2)
Set objTempFile = objFSO.OpenTextFile(strTempDir & "\temp.txt",2,true)
do until objInputFile.AtEndOfStream
objTempFile.WriteLine(Replace(objInputFile.ReadLine, strFind, strReplace))
loop
objInputFile.Close
Set objInputFile = Nothing
objTempFile.Close
Set objTempFile = Nothing
objFSO.DeleteFile strFile, true
objFSO.MoveFile strTempDir & "\temp.txt", strFile
Set objFSO = Nothing
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment