:parse_args if "%~1"=="" goto :generate if /i "%~1"=="-o" set OUTPUTFILE=%~2& shift & shift & goto parse_args if /i "%~1"=="-s" set KEYSIZE=%~2& shift & shift & goto parse_args if /i "%~1"=="-f" set FORMAT=%~2& shift & shift & goto parse_args if /i "%~1"=="-h" goto :usage shift goto parse_args
echo [SUCCESS] Keyfile saved as %OUTPUT_FILE% echo [MD5] %OUTPUT_FILE% - Use for verification. keyfilegenerator.cmd
:generate echo [!] Generating %KEYSIZE%-byte keyfile as %FORMAT% ... if %FORMAT%==raw ( certutil -rand %KEYSIZE% > %OUTPUTFILE% 2>nul ) else if %FORMAT%==base64 ( powershell -Command "$r = [System.Security.Cryptography.RNGCryptoServiceProvider]::new(); $b = [byte[]]::new(%KEYSIZE%); $r.GetBytes($b); [Convert]::ToBase64String($b) | Out-File -Encoding ascii %OUTPUTFILE%" ) else if %FORMAT%==hex ( powershell -Command "$r = [System.Security.Cryptography.RNGCryptoServiceProvider]::new(); $b = [byte[]]::new(%KEYSIZE%); $r.GetBytes($b); ($b^|%%' 0:X2' -f $_) -join '' | Out-File -Encoding ascii %OUTPUTFILE%" ) else ( echo [ERROR] Unknown format %FORMAT%. Use base64, hex, or raw. exit /b 1 ) :parse_args if "%~1"=="" goto :generate if /i "%~1"=="-o"
@echo off setlocal EnableExtensions EnableDelayedExpansion set SCRIPT_NAME=%~n0 set VERSION=2.1 :: Argument parsing set OUTPUTFILE=keyfile_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.key set KEYSIZE=2048 set FORMAT=base64 Use base64, hex, or raw
: A keyfile generator is only as strong as its random source. Avoid %RANDOM% like the plague; embrace certutil or PowerShell’s cryptography APIs. Always distribute keyfiles over secure channels (never plaintext email or unencrypted network shares), and periodically rotate keys.
set /a RANDOM_KEY=%RANDOM%%RANDOM%%RANDOM% echo %RANDOM_KEY% > key.txt Here, the randomness is only 15 bits (0-32767) repeated – trivially brute-forceable. Always use system-level cryptographic APIs. If you’re deploying this script in an enterprise, here’s a robust template:
:usage echo %SCRIPT_NAME% v%VERSION% - Secure Keyfile Generator echo Usage: %SCRIPT_NAME% [-o outputfile] [-s size_bytes] [-f ^(base64^|hex^|raw^)] echo Example: %SCRIPT_NAME% -o license.dat -s 4096 -f raw exit /b 0