Hack Forums
[Shell] Drag-n-drop BASE64 encoder/decoder - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Drag-n-drop BASE64 encoder/decoder (/showthread.php?tid=6063809)



Drag-n-drop BASE64 encoder/decoder - SIM-4581439 - Apr 5, 2020

Without going into much detail I was required to encode a decent amount of files in Base64. The script below encodes/decodes either one file or many (from a directory). The script determines if a file has been encoded by looking at the header. If present, the script decodes the file, and if not, encodes it. This code isn't fool-proof however. It uses string manipulation to trim off the last three or last four characters from the object name. If what was trimmed starts with a period then the script assumes it's a file. If not, it assumes the object is a directory. This only functions for two or three character file extensions. Feel free to post any suggestions or possible upgrades to improve the functionality. Cheers!

Code
@echo off >NUL
Set "file=%~1"

REM [CHECK FOR FILE EXTENSION]
If "%file:~-4,1%"=="." goto :FILE
If "%file:~-3,1%"=="." goto :FILE
REM [IF NOT FILE, DECRYPT/ENCRYPT DIRECTORY]
goto :DIR
exit

:FILE
If "%file%"=="" GOTO:EOF
REM [CHECKS FOR BASE64 HEADER]
>nul find "-----BEGIN CERTIFICATE-----" "%file%" && (
  certutil -f -v -decode "%file%" "%file%"
) || (
  certutil -f -v -encode "%file%" "%file%"
)
exit

:DIR
REM [CATALOGS FILE(S) FROM DIRECTORY]
DIR /B /S "%file%" >"%temp%\list.ini"
Set "content=%temp%\list.ini"

REM [CHECKS FOR BASE64 HEADER IN FILE(S)]
Setlocal EnableDelayedExpansion
FOR /F "delims=" %%K IN (%content%) DO (
set "obj=%%K"

>nul find "-----BEGIN CERTIFICATE-----" "!obj!" && (
  certutil -f -v -decode "!obj!" "!obj!"
) || (
  certutil -f -v -encode "!obj!" "!obj!"
)

)
Del /F /Q "%content%"
exit