Advertisement
J2897

GPG Interface - BETA

Feb 24th, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.15 KB | None | 0 0
  1. :: Released under the GNU General Public License version 3 by J2897.
  2.  
  3. @echo OFF
  4. pushd "%~dp0"
  5. setlocal EnableDelayedExpansion
  6. color 1b
  7. REM Adjust window size.
  8. mode CON:cols=120 lines=60
  9.  
  10. REM #################### README #################### > BEGIN
  11. :: Filename:    gpg.bat
  12. :: Version:     0.6
  13. :: Latest:      http://pastebin.com/AhEeyGnX
  14. :: Contact:     https://pastebin.com/message_compose?to=J2897
  15. ::
  16. :: This is meant for educational purposes. You can quickly manage a maximum of
  17. :: eight GPG key-pairs. I made this mainly for experimenting with GNU compliant
  18. :: Duplicity. But you could use it for anything that urgently requires GPG
  19. :: keys. If you need to manage lots of GPG keys, you should learn GPG on the
  20. :: commandline, or install the full version of Gpg4win.
  21. REM #################### README #################### > END
  22.  
  23. if defined PROGRAMFILES(X86) (
  24.     set "GPGDIR=%PROGRAMFILES(X86)%\GNU\GnuPG"
  25. ) else (
  26.     set "GPGDIR=%PROGRAMFILES%\GNU\GnuPG"
  27. )
  28. set "GPGEXE=gpg2.exe"
  29. set "GPG=%GPGDIR%\%GPGEXE%"
  30. set "RETURN=Press any key to return . . ."
  31. set "EXIT=Press any key to exit . . ."
  32.  
  33. if not exist "%GPG%" (
  34.     echo Please install at least the vanilla flavour of Gpg4win.
  35.     echo https://www.gpg4win.org/
  36.     popd
  37.     echo.
  38.     echo %EXIT%
  39.     endlocal
  40.     pause >nul
  41.     color
  42.     exit 0
  43. )
  44.  
  45. title Main Menu
  46. :start
  47. cls
  48. <nul set /p =.^<^<^<                        Released under the GNU General Public License version 3+ by J2897.                        ^>^>^>
  49. echo.
  50. echo                                                   GPG Batch Interface
  51. echo.
  52. echo                                                Copyright (C) 2016 J2897.
  53. echo                                  You are free to change and redistribute this software.
  54. echo                                            ^<http://gnu.org/licenses/gpl.html^>
  55. echo.
  56. echo  1. Help.
  57. echo  2. List public/private keys.
  58. echo  3. Create new key(s).
  59. echo  4. Import public key.
  60. echo  5. Import private key.
  61. echo  6. Export public key.
  62. echo  7. Export private key.
  63. echo  8. Delete keys.
  64. echo  9. Exit.
  65. echo.
  66. choice /C:123456789 /N /M "Which number? "
  67. if errorlevel 9 goto :end
  68. if errorlevel 8 goto :delete
  69. if errorlevel 7 goto :export-private-key
  70. if errorlevel 6 goto :export-public-key
  71. if errorlevel 5 goto :import-private-key
  72. if errorlevel 4 goto :import-public-key
  73. if errorlevel 3 goto :gen-key
  74. if errorlevel 2 goto :list-keys
  75. if errorlevel 1 goto :help
  76. goto :start
  77.  
  78. :help
  79. cls
  80. "%GPG%" --help | more
  81. echo %RETURN%
  82. pause >nul
  83. goto :start
  84.  
  85. :list-keys
  86. cls
  87. echo Public Keys:
  88. echo.
  89. "%GPG%" --fingerprint
  90. pause
  91. cls
  92. echo Private Keys:
  93. echo.
  94. "%GPG%" --list-secret-keys --fingerprint
  95. echo %RETURN%
  96. pause >nul
  97. goto :start
  98.  
  99. :gen-key
  100. cls
  101. echo If you don't want a password, just press the OK button five times when it appears.
  102. echo.
  103. "%GPG%" --gen-key
  104. echo %RETURN%
  105. pause >nul
  106. goto :start
  107.  
  108. :import-public-key
  109. cls
  110. echo Import public key:
  111. echo.
  112. if not exist "%CD%\Import" (md "%CD%\Import")
  113. echo Place a copy of the GPG key inside this folder and rename it to "public.key":
  114. echo %CD%\Import
  115. echo.
  116. start "" explorer.exe "%CD%\Import"
  117. pause
  118. pushd "%CD%\Import"
  119. echo.
  120. "%GPG%" --import public.key
  121. popd
  122. echo.
  123. echo %RETURN%
  124. pause >nul
  125. goto :start
  126.  
  127. :import-private-key
  128. cls
  129. echo Import private key:
  130. echo.
  131. if not exist "%CD%\Import" (md "%CD%\Import")
  132. echo Place a copy of the GPG key inside this folder and rename it to "private.key":
  133. echo %CD%\Import
  134. echo.
  135. start "" explorer.exe "%CD%\Import"
  136. pause
  137. pushd "%CD%\Import"
  138. echo.
  139. "%GPG%" --allow-secret-key-import --import private.key
  140. popd
  141. echo.
  142. echo %RETURN%
  143. pause >nul
  144. goto :start
  145.  
  146. :export-public-key
  147. cls
  148. echo Export public key:
  149. echo.
  150. "%GPG%" --list-keys |findstr /C:"pub   " /C:"uid   "
  151. echo.
  152. if not exist "%CD%\Export" (md "%CD%\Export")
  153. pushd "%CD%\Export"
  154. call :get-public-keys
  155. call :show-keys
  156. choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
  157. if errorlevel 9 goto :start
  158. if errorlevel 8 "%GPG%" --armor --output public.key --export %ARRAYLINE[8]% &goto :public-key-exported
  159. if errorlevel 7 "%GPG%" --armor --output public.key --export %ARRAYLINE[7]% &goto :public-key-exported
  160. if errorlevel 6 "%GPG%" --armor --output public.key --export %ARRAYLINE[6]% &goto :public-key-exported
  161. if errorlevel 5 "%GPG%" --armor --output public.key --export %ARRAYLINE[5]% &goto :public-key-exported
  162. if errorlevel 4 "%GPG%" --armor --output public.key --export %ARRAYLINE[4]% &goto :public-key-exported
  163. if errorlevel 3 "%GPG%" --armor --output public.key --export %ARRAYLINE[3]% &goto :public-key-exported
  164. if errorlevel 2 "%GPG%" --armor --output public.key --export %ARRAYLINE[2]% &goto :public-key-exported
  165. if errorlevel 1 "%GPG%" --armor --output public.key --export %ARRAYLINE[1]%
  166. :public-key-exported
  167. REM Export binary.
  168. ::"%GPG%" --no-armor --output public.key --export alice@cyb.org
  169. popd
  170. start "" explorer.exe "%CD%\Export"
  171. echo.
  172. echo %RETURN%
  173. pause >nul
  174. goto :start
  175.  
  176. :export-private-key
  177. cls
  178. echo Export private key:
  179. echo.
  180. "%GPG%" --list-secret-keys |findstr /C:"sec   " /C:"uid   "
  181. echo.
  182. if not exist "%CD%\Export" (md "%CD%\Export")
  183.  
  184. pushd "%CD%\Export"
  185. call :get-private-keys
  186. call :show-keys
  187. choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
  188. if errorlevel 9 goto :start
  189. if errorlevel 8 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[8]% &goto :private-key-exported
  190. if errorlevel 7 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[7]% &goto :private-key-exported
  191. if errorlevel 6 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[6]% &goto :private-key-exported
  192. if errorlevel 5 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[5]% &goto :private-key-exported
  193. if errorlevel 4 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[4]% &goto :private-key-exported
  194. if errorlevel 3 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[3]% &goto :private-key-exported
  195. if errorlevel 2 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[2]% &goto :private-key-exported
  196. if errorlevel 1 "%GPG%" --armor --output private.key --export-secret-key %ARRAYLINE[1]%
  197. :private-key-exported
  198. REM Export binary.
  199. ::"%GPG%" --no-armor --output private.key --export alice@cyb.org
  200. popd
  201. start "" explorer.exe "%CD%\Export"
  202. echo.
  203. echo %RETURN%
  204. pause >nul
  205. goto :start
  206.  
  207. :delete
  208. cls
  209. echo The private key will be deleted, and then the corresponding public key:
  210. echo.
  211. call :get-private-keys
  212. call :show-keys
  213. choice /C:123456789 /T 600 /D 9 /N /M "Which number? "
  214. if errorlevel 9 goto :start
  215. if errorlevel 8 "%GPG%" --delete-secret-key %ARRAYLINE[8]% &"%GPG%" --delete-key %ARRAYLINE[8]% &goto :key-deleted
  216. if errorlevel 7 "%GPG%" --delete-secret-key %ARRAYLINE[7]% &"%GPG%" --delete-key %ARRAYLINE[7]% &goto :key-deleted
  217. if errorlevel 6 "%GPG%" --delete-secret-key %ARRAYLINE[6]% &"%GPG%" --delete-key %ARRAYLINE[6]% &goto :key-deleted
  218. if errorlevel 5 "%GPG%" --delete-secret-key %ARRAYLINE[5]% &"%GPG%" --delete-key %ARRAYLINE[5]% &goto :key-deleted
  219. if errorlevel 4 "%GPG%" --delete-secret-key %ARRAYLINE[4]% &"%GPG%" --delete-key %ARRAYLINE[4]% &goto :key-deleted
  220. if errorlevel 3 "%GPG%" --delete-secret-key %ARRAYLINE[3]% &"%GPG%" --delete-key %ARRAYLINE[3]% &goto :key-deleted
  221. if errorlevel 2 "%GPG%" --delete-secret-key %ARRAYLINE[2]% &"%GPG%" --delete-key %ARRAYLINE[2]% &goto :key-deleted
  222. if errorlevel 1 "%GPG%" --delete-secret-key %ARRAYLINE[1]% &"%GPG%" --delete-key %ARRAYLINE[1]%
  223. :key-deleted
  224. echo.
  225. echo %RETURN%
  226. pause >nul
  227. goto :start
  228.  
  229. :end
  230. popd
  231. endlocal
  232. color
  233. exit 0
  234.  
  235. :get-public-keys
  236. REM Create an array using a for-loop.
  237. set INDEX=0
  238. pushd "%GPGDIR%"
  239. for /f "tokens=1,2,3 delims=/ " %%A in ('%GPGEXE% --list-keys ^| find "pub   "') do (
  240.     set /a INDEX=INDEX+1
  241.     set ARRAYLINE[!INDEX!]=%%C
  242. )
  243. popd
  244. exit /b 0
  245.  
  246. :get-private-keys
  247. REM Create an array using a for-loop.
  248. set INDEX=0
  249. pushd "%GPGDIR%"
  250. for /f "tokens=1,2,3 delims=/ " %%A in ('%GPGEXE% --list-secret-keys ^| find "sec   "') do (
  251.     set /a INDEX=INDEX+1
  252.     set ARRAYLINE[!INDEX!]=%%C
  253. )
  254. popd
  255. exit /b 0
  256.  
  257. :show-keys
  258. REM Iterate through that array using another for-loop.
  259. if %INDEX% GTR 8 (set /a INDEX=8)
  260. for /l %%N in (1,1,%INDEX%) do (
  261.     echo  %%N. !ARRAYLINE[%%N]!.
  262. )
  263. echo  9. Exit.
  264. echo.
  265. exit /b 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement