I currently have this code I use to encrypt and decrypt files on my disk, but it checks for specific file extensions only, but I want it to encrypt every single file extension. Here is the code I currently have:
:1layer
echo Encrypting disk, this may take a while.
set ForLoopFile=%VaultPath%
FOR /f %%G IN ('dir /s /b /a-d %ForLoopFile%') DO (call :Encrypt %%G)
if %layer%==2 goto :2layer
goto :actionask
:Encrypt
set targetFile=%1
set outputFile=%targetFile:txt=txt.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:dll=dll.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ini=ini.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:exe=exe.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:zip=zip.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:rar=rar.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:png=png.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:jpg=jpg.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ico=ico.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:gif=gif.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:mp4=mp4.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:m4v=m4v.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:.py=.py.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:asc=asc.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:vbs=vbs.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:bat=bat.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ovpn=ovpn.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
del /f %targetFile%
exit /b
See, I have to change the target extension file for the extensionsI want to check for, but does anyone know how I can make it encrypt every single file, no matter the extension?
If anyone can help me, I'd greatly appreciate it.
I have no idea on how to do this, all I tried was checking for file extensions manually, but this is limitted to the ones I specify, I want every file extension to get encrypted.
CodePudding user response:
FOR /f "delims=" %%G IN ('dir /s /b /a-d %ForLoopFile%') DO if /i "%%~xG" neq ".aes" (call :Encrypt "%%G")
...
:Encrypt
aes -e %encryptionKey% "%~1" "%~1.aes" >NUL
del /f "%~1"
goto :eof
should accomplish this Note: untried
Always verify against a test directory before applying to real data.
The modification to the for
line:
"delims=" to assign the entirety of the filenames to %%G
, else only the first token will be assigned (see for/?
from the prompt for docco.
if /i
- only if (case-insensitive)
"%%~xG" neq ".aes" - the current file extension is Not EQual to .aes
"%%G" - entire filename is one parameter
This should skip .aes
files so that you dont get .aes.aes.aes.aes
as the procedure is repeated.
The :encrypt
subroutine:
apply aes
to the first parameter delivered to the routine (%1
) stripped of the enclosing quotes (%~1
); simply add .aes
to the string for destination file.
then delete the file
This should allow filenames including spaces to be processed.
Note
add echo
before the aes
and del
and remove the >nul
to produce a tester version which will simply show the proposed actions on the console.
Note: untried
Always verify against a test directory before applying to real data.
--- Decrypt version (Theoretical)
FOR /f "delims=" %%G IN ('dir /s /b /a-d %ForLoopFile%.aes') DO (call :Decrypt "%%G")
(Allows filenames containing spaces, and selects only files type *.aes
)
:Decrypt
aes -d %encryptionKey% "%~1" "%~dpn1" >NUL
del /f "%~1"
goto :eof
(Still theoretical)
decrypts to the Drive Path Name of the 1st parameter.
Note:
Since "%~1"
is deleted automatically by this code, it would be advisable to gate the delete
so that any error bypasses the delete. Logically, this should be
if not errorlevel 1 del /f "%~1"
But I am unfamiliar with how aes
works.
Another way would be
if exist "%~1.aes" del /f "%~1"
And possibly it would be advisable to use
:Decrypt
if exist "%~dpn1" echo Error - "%~dpn1" already exists&goto :eof
aes -d %encryptionKey% "%~1" "%~dpn1" >NUL
del /f "%~1"
goto :eof
CodePudding user response:
https://stackoverflow.com/users/2128947/magoo
FOR /f %%G IN ('dir /s /b /a-d %ForLoopFile%') DO (call :Decrypt %%G)
:Decrypt
set targetFile=%1
set outputFile=%targetFile:txt.aes=txt%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:dll.aes=dll%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ini.aes=ini%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:exe.aes=exe%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:zip.aes=zip%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:rar.aes=rar%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:png.aes=png%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:jpg.aes=jpg%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ico.aes=ico%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:gif.aes=gif%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:mp4.aes=mp4%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:m4v.aes=m4v%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:.py.aes=.py%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:asc.aes=asc%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:vbs.aes=vbs%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:bat.aes=bat%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ovpn.aes=ovpn%
aes -d %encryptionKey% %targetFile% %outputFile% >NUL
del /f %targetFile%
exit /b