Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: Released under the GNU General Public License version 3 by J2897.
- @echo OFF
- pushd "%~dp0"
- setlocal EnableDelayedExpansion
- color 1b
- REM Adjust window size.
- mode CON:cols=120 lines=60
- REM #################### README #################### > BEGIN
- :: Filename: gpg.bat
- :: Version: 0.6
- :: Latest: http://pastebin.com/AhEeyGnX
- :: Contact: https://pastebin.com/message_compose?to=J2897
- ::
- :: This is meant for educational purposes. You can quickly manage a maximum of
- :: eight GPG key-pairs. I made this mainly for experimenting with GNU compliant
- :: Duplicity. But you could use it for anything that urgently requires GPG
- :: keys. If you need to manage lots of GPG keys, you should learn GPG on the
- :: commandline, or install the full version of Gpg4win.
- REM #################### README #################### > END
- if defined PROGRAMFILES(X86) (
- set "GPGDIR=%PROGRAMFILES(X86)%\GNU\GnuPG"
- ) else (
- set "GPGDIR=%PROGRAMFILES%\GNU\GnuPG"
- )
- set "GPGEXE=gpg2.exe"
- set "GPG=%GPGDIR%\%GPGEXE%"
- set "RETURN=Press any key to return . . ."
- set "EXIT=Press any key to exit . . ."
- if not exist "%GPG%" (
- echo Please install at least the vanilla flavour of Gpg4win.
- echo https://www.gpg4win.org/
- popd
- echo.
- echo %EXIT%
- endlocal
- pause >nul
- color
- exit 0
- )
- title Main Menu
- :start
- cls
- <nul set /p =.^<^<^< Released under the GNU General Public License version 3+ by J2897. ^>^>^>
- echo.
- echo GPG Batch Interface
- echo.
- echo Copyright (C) 2016 J2897.
- echo You are free to change and redistribute this software.
- echo ^<http://gnu.org/licenses/gpl.html^>
- echo.
- echo 1. Help.
- echo 2. List public/private keys.
- echo 3. Create new key(s).
- echo 4. Import public key.
- echo 5. Import private key.
- echo 6. Export public key.
- echo 7. Export private key.
- echo 8. Delete keys.
- echo 9. Exit.
- echo.
- choice /C:123456789 /N /M "Which number? "
- if errorlevel 9 goto :end
- if errorlevel 8 goto :delete
- if errorlevel 7 goto :export-private-key
- if errorlevel 6 goto :export-public-key
- if errorlevel 5 goto :import-private-key
- if errorlevel 4 goto :import-public-key
- if errorlevel 3 goto :gen-key
- if errorlevel 2 goto :list-keys
- if errorlevel 1 goto :help
- goto :start
- :help
- cls
- "%GPG%" --help | more
- echo %RETURN%
- pause >nul
- goto :start
- :list-keys
- cls
- echo Public Keys:
- echo.
- "%GPG%" --fingerprint
- pause
- cls
- echo Private Keys:
- echo.
- "%GPG%" --list-secret-keys --fingerprint
- echo %RETURN%
- pause >nul
- goto :start
- :gen-key
- cls
- echo If you don't want a password, just press the OK button five times when it appears.
- echo.
- "%GPG%" --gen-key
- echo %RETURN%
- pause >nul
- goto :start
- :import-public-key
- cls
- echo Import public key:
- echo.
- if not exist "%CD%\Import" (md "%CD%\Import")
- echo Place a copy of the GPG key inside this folder and rename it to "public.key":
- echo %CD%\Import
- echo.
- start "" explorer.exe "%CD%\Import"
- pause
- pushd "%CD%\Import"
- echo.
- "%GPG%" --import public.key
- popd
- echo.
- echo %RETURN%
- pause >nul
- goto :start
- :import-private-key
- cls
- echo Import private key:
- echo.
- if not exist "%CD%\Import" (md "%CD%\Import")
- echo Place a copy of the GPG key inside this folder and rename it to "private.key":
- echo %CD%\Import
- echo.
- start "" explorer.exe "%CD%\Import"
- pause
- pushd "%CD%\Import"
- echo.
- "%GPG%" --allow-secret-key-import --import private.key
- popd
- echo.
- echo %RETURN%
- pause >nul
- goto :start
- :export-public-key
- cls
- echo Export public key:
- echo.
- "%GPG%" --list-keys |findstr /C:"pub " /C:"uid "
- echo.
- if not exist "%CD%\Export" (md "%CD%\Export")
- pushd "%CD%\Export"
- call :get-public-keys
- call :show-keys
- choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
- if errorlevel 9 goto :start
- if errorlevel 8 "%GPG%" --armor --output public.key --export %ARRAYLINE[8]% &goto :public-key-exported
- if errorlevel 7 "%GPG%" --armor --output public.key --export %ARRAYLINE[7]% &goto :public-key-exported
- if errorlevel 6 "%GPG%" --armor --output public.key --export %ARRAYLINE[6]% &goto :public-key-exported
- if errorlevel 5 "%GPG%" --armor --output public.key --export %ARRAYLINE[5]% &goto :public-key-exported
- if errorlevel 4 "%GPG%" --armor --output public.key --export %ARRAYLINE[4]% &goto :public-key-exported
- if errorlevel 3 "%GPG%" --armor --output public.key --export %ARRAYLINE[3]% &goto :public-key-exported
- if errorlevel 2 "%GPG%" --armor --output public.key --export %ARRAYLINE[2]% &goto :public-key-exported
- if errorlevel 1 "%GPG%" --armor --output public.key --export %ARRAYLINE[1]%
- :public-key-exported
- REM Export binary.
- ::"%GPG%" --no-armor --output public.key --export alice@cyb.org
- popd
- start "" explorer.exe "%CD%\Export"
- echo.
- echo %RETURN%
- pause >nul
- goto :start
- :export-private-key
- cls
- echo Export private key:
- echo.
- "%GPG%" --list-secret-keys |findstr /C:"sec " /C:"uid "
- echo.
- if not exist "%CD%\Export" (md "%CD%\Export")
- pushd "%CD%\Export"
- call :get-private-keys
- call :show-keys
- choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
- if errorlevel 9 goto :start
- if errorlevel 8 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[8]% &goto :private-key-exported
- if errorlevel 7 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[7]% &goto :private-key-exported
- if errorlevel 6 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[6]% &goto :private-key-exported
- if errorlevel 5 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[5]% &goto :private-key-exported
- if errorlevel 4 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[4]% &goto :private-key-exported
- if errorlevel 3 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[3]% &goto :private-key-exported
- if errorlevel 2 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[2]% &goto :private-key-exported
- if errorlevel 1 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[1]%
- :private-key-exported
- REM Export binary.
- ::"%GPG%" --no-armor --output private.key --export alice@cyb.org
- popd
- start "" explorer.exe "%CD%\Export"
- echo.
- echo %RETURN%
- pause >nul
- goto :start
- :delete
- cls
- echo The private key will be deleted, and then the corresponding public key:
- echo.
- call :get-private-keys
- call :show-keys
- choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
- if errorlevel 9 goto :start
- if errorlevel 8 "%GPG%" --delete-secret-key %ARRAYLINE[8]% &"%GPG%" --delete-key %ARRAYLINE[8]% &goto :key-deleted
- if errorlevel 7 "%GPG%" --delete-secret-key %ARRAYLINE[7]% &"%GPG%" --delete-key %ARRAYLINE[7]% &goto :key-deleted
- if errorlevel 6 "%GPG%" --delete-secret-key %ARRAYLINE[6]% &"%GPG%" --delete-key %ARRAYLINE[6]% &goto :key-deleted
- if errorlevel 5 "%GPG%" --delete-secret-key %ARRAYLINE[5]% &"%GPG%" --delete-key %ARRAYLINE[5]% &goto :key-deleted
- if errorlevel 4 "%GPG%" --delete-secret-key %ARRAYLINE[4]% &"%GPG%" --delete-key %ARRAYLINE[4]% &goto :key-deleted
- if errorlevel 3 "%GPG%" --delete-secret-key %ARRAYLINE[3]% &"%GPG%" --delete-key %ARRAYLINE[3]% &goto :key-deleted
- if errorlevel 2 "%GPG%" --delete-secret-key %ARRAYLINE[2]% &"%GPG%" --delete-key %ARRAYLINE[2]% &goto :key-deleted
- if errorlevel 1 "%GPG%" --delete-secret-key %ARRAYLINE[1]% &"%GPG%" --delete-key %ARRAYLINE[1]%
- :key-deleted
- echo.
- echo %RETURN%
- pause >nul
- goto :start
- :end
- popd
- endlocal
- color
- exit 0
- :get-public-keys
- REM Create an array using a for-loop.
- set INDEX=0
- pushd "%GPGDIR%"
- for /f "tokens=1,2,3 delims=/ " %%A in ('%GPGEXE% --list-keys ^| find "pub "') do (
- set /a INDEX=INDEX+1
- set ARRAYLINE[!INDEX!]=%%C
- )
- popd
- exit /b 0
- :get-private-keys
- REM Create an array using a for-loop.
- set INDEX=0
- pushd "%GPGDIR%"
- for /f "tokens=1,2,3 delims=/ " %%A in ('%GPGEXE% --list-secret-keys ^| find "sec "') do (
- set /a INDEX=INDEX+1
- set ARRAYLINE[!INDEX!]=%%C
- )
- popd
- exit /b 0
- :show-keys
- REM Iterate through that array using another for-loop.
- if %INDEX% GTR 8 (set /a INDEX=8)
- for /l %%N in (1,1,%INDEX%) do (
- echo %%N. !ARRAYLINE[%%N]!.
- )
- echo 9. Exit.
- echo.
- exit /b 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement