0

I have following script.

echo off

cd C:\Users\User_Name\Desktop\Encryption\Hemant test 
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.gpg   
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" -e Test.txt 

But after that in command windows its asking me user Input as per following "Enter the User ID.End with an empty line:"

As displayed at the image:
image

I have to pass the 'Test' value and click on enter.

But I am not able to pass the 'Test' value in above Batch script.

coder08
  • 13
  • 4
coder88
  • 31
  • 9
  • I don't understand what you are asking for. Only two things straightaway from your post - should be `@echo off` and when changing patch with spaces you have to add double quotes `cd "C:\Users\tmp0060hk\Desktop\Encryption\Hemant test"` – tukan May 17 '18 at 07:37
  • After '"C:\Program Files (x86)\GnuPG\bin\gpg.exe" -e Test.txt' this line in batch file. Its asked me User input where I have to update 'test' value. But via batch file I am not able to update. – coder88 May 17 '18 at 08:09
  • Why don't use use the `-u USER_ID` (alias for `--local-user`) at the command line? – tukan May 17 '18 at 08:16
  • Thanks for suggestion. Any reply for above problem? – coder88 May 17 '18 at 08:31
  • Not an answer, but change `cd C:\Users\User_Name\Desktop\Encryption\Hemant test` to `cd /D "C:\Users\User_Name\Desktop\Encryption\Hemant test"`, so `cd` changes the directory even if you are on a different drive... – aschipfl May 17 '18 at 15:54

1 Answers1

0

Edit I have installed the newest GnuPG for windows (gpg4win 3.1.1)

Which contains the following library versions:

c:\app\security\GnuPG\bin\gpg.exe --version
gpg (GnuPG) 2.2.7
libgcrypt 1.8.2

As for your script it should look the following way

@echo off
cd "C:\Users\User_Name\Desktop\Encryption\Hemant test"
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.gpg  
set /p user_id=Please enter your user id:
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" -r "%user_id%" --encrypt Test.txt

Edit2 - showing whole workflow (notice that I have the key already there so there is a message, in Czech, that states that the key is without any changes)

My test.bat

@echo off
c:\app\security\GnuPG\bin\gpg.exe --import C:\app\security\GnuPG\mygpgkey_pub.gpg
set /p user_id=Please enter your user id:
c:\app\security\GnuPG\bin\gpg.exe -r "%user_id%" -e VERSION

Now the actual workflow:

C:\app\security\GnuPG>dir *.gpg
 Volume in drive C is OSDisk
 Volume Serial Number is 6C83-746F

 Directory of C:\app\security\GnuPG

17.05.2018  13:55             6 731 mygpgkey_priv.gpg
17.05.2018  13:55             3 181 mygpgkey_pub.gpg
               2 File(s)          9 912 bytes
               0 Dir(s)   5 941 710 848 bytes free

C:\app\security\GnuPG>test.bat
gpg: klíč B6E8D7336780XXXX: "John Smith <john.smith@usa.net>" beze změn
gpg: Celkový počet zpracovaných klíčů: 1
gpg:                        beze změn: 1
Please enter your user id:John Smith

C:\app\security\GnuPG>dir *.gpg
 Volume in drive C is OSDisk
 Volume Serial Number is 6C83-746F

 Directory of C:\app\security\GnuPG

17.05.2018  13:55             6 731 mygpgkey_priv.gpg
17.05.2018  13:55             3 181 mygpgkey_pub.gpg
17.05.2018  13:57               609 VERSION.gpg
               3 File(s)         10 521 bytes
               0 Dir(s)   5 941 673 984 bytes free

C:\app\security\GnuPG>cat VERSION.gpg
☻♀♥VsP┼ĎžI}☺ ˙w▬ďÍ[Łu   a↓#ŇÜŔ~>Ö▬ľżŕ↔►►=*Q×♥+Ç$NôCČuy→8Ą'      @⌂∟ĘŽ∟'ôż|→↑|Ţ↕ÄĄú4|-b >%ÄXľ♂Ť  ŮLŤÍ♫∟"˛2ýś+¸ęęHDF\`+-BţYu˙:|ŽÝÍ♣%☺RďGX┼łŠX%?Łä÷äĘo'Ę|3.Ń<ě°§ó☻ÇKŰ+CăÉĆŚßźE˙Ń?bÔj§ž`<?Ř9c♦§Şś>Éb˛       (ďćžDŹ☻jí6♦§-ßÚŇ~♀"kfďxĺk)˘¨äŰ×▼N÷1 3"}Ä-sžóáe↓x.↓=mĘ?Đ'Á♥{>7c♫SA^I˛uÁi!$Źő/¶ˇ  ÇŃyr☺¬+"F-+ílBn¨xŮ«%]"ól5‼ ☺Ë'►JT↓►ď6ÍŤjÍ|ßŃ"@m+/ ¤¶ĆvŐĂwł♦$Ăżçy% O­ÇŁŔóť¤"öbzB0X;♠˙/ăÁbĽH♦_@♣Ôă☻Ĺ◄Ł♣\▲
Ă♀é&/►Ë⌂RĚ 'Yô'­K­ô~Á˝H☻'´pBŚp▬q↓<ÎŃn2Ů *í«[ëťh×űŽ♠oˇ'ĎtČw♠ Sč Pń{vPąŻwĘŰ♣š♂♠↑Öĺ?Űc=¨%ÉöÜš▬űüR0¶ŕ[qäXOżOb%ÁóçĹ;áÄ☺ĹńLúŘ?i┼e3Qť┼{T%Ż<ďĚŇP☺~Çbéx{O☺Ä◄uM× -ۤ[┼^♦ëŐ˙Rđť<2]{\^Ö%E<ÄX4a_R*?^Ç►ë‼♠Ň2ŐČě?z.M 1ä▲ąěŞuźăŁ↔÷q+Ŕ#á'

C:\app\security\GnuPG>cat VERSION
gnupg
2.2.7

C:\app\security\GnuPG>

Edit 3 - To check if the key is imported

c:\app\security\GnuPG\bin\gpg.exe --list-secret-keys --keyid-format LONG

You should get something like this:

C:/Users/userID/AppData/Roaming/gnupg/pubring.gpg
---------------------------------------------------
sec   rsa4096/B6E8D7336780XXXX 2018-05-17 [SC]
      ABEC1158A91C80AF5B716DCCB6E8D7336780XXXX
uid             [  absolutní ] John Smith <john.smith@usa.net>
ssb   rsa4096/56735087CF9EXXXX 2018-05-17 [E]

Edit 4 - non-interactive batch script:

@echo off
set "user_id=John Smith"

cd "C:\Users\User_Name\Desktop\Encryption\Hemant test"
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.gpg  
"C:\Program Files (x86)\GnuPG\bin\gpg.exe" -r "%user_id%" --encrypt Test.txt
tukan
  • 17,050
  • 1
  • 20
  • 48
  • It did not work. When I am getting 'Enter the User ID. end with an empty line' then I am not able input the value using batch file. – coder88 May 17 '18 at 10:01
  • echo off cd C:\Users\User_Name\Desktop\Encryption\Hemant test "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.gpg "C:\Program Files (x86)\GnuPG\bin\gpg.exe" -e Test.txt after this on command prompt its asking for 'Enter the UserID. End with an empty line:' Here I am not able to update the value – coder88 May 17 '18 at 10:24
  • @Hermant well you have to enter the user_id before the actual input. It won't work otherwise. See my answer. – tukan May 17 '18 at 11:17
  • Appreciate your reply. But I have tried above code but it is not generating the encrypted document. Window is just stuck at the receipient. I did some research and found following link https://www.gnupg.org/gph/en/manual/x110.html. By using following command command as given on official site I am trying 'alice% gpg --output doc.gpg --encrypt --recipient blake@cyb.org doc'.I want to encrypt the document. Can you please tell me whether document is being encrypted at your end. – coder88 May 17 '18 at 11:29
  • @Herman Yes it does work. I'll show you the workflow. Are you sure you are entering the correct name? – tukan May 17 '18 at 12:04
  • Thanks for your effort. However, I have tried to make the changes as per your bat file. Please check below. @echo off C:\Program Files (x86)\GnuPG\bin\gpg.exe --import C:\Encryption\PB_Key.gpg set /p user_id=Please enter your user id: C:\Program Files (x86)\GnuPG\bin\gpg.exe -r "%user_id%" -encrypt Test.txt But getting error ''C:\Program' is not recognized as an internal or external command,' My user name is 'Test'. I want to incorporate same in bat file. So if I will run the bat file then directly I will get the encrypted document. – coder88 May 17 '18 at 12:29
  • I have changed e simple directory. Please check as per following Public Key : C:\Encryption\PB_Key.gpg File need to encrypt : C:\Encryption\Test.txt Bat file : C:\Encryption\Test.bat gpg.exe : C:\Program Files (x86)\GnuPG\bin\gpg.exe Can you please assist me, where its going wrong. – coder88 May 17 '18 at 12:29
  • @Hemant you need to enclose the path (you have spaces there) with double quotes! e.g. "C:\Program Files (x86)\GnuPG\bin\gpg.exe" – tukan May 17 '18 at 12:33
  • I have updated the " quotes and "C" directory issue resolved. Now its asking me enter the User ID. Once I will update the User then screen is disappearing and there is no encrypted document in output folder. – coder88 May 17 '18 at 12:37
  • @Hemant sounds likes it is working but the ouput is somewhere you don't expect. Do you check `"C:\Users\User_Name\Desktop\Encryption\Hemant test"? – tukan May 17 '18 at 12:56
  • I did check. Nothing seems wrong. Please check my directory. @echo off cd "C:\Encryption" "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.asc set /p user_id=Enter the User ID: "C:\Program Files (x86)\GnuPG\bin\gpg.exe" -r" "%user_id%" -e Test.txt above is my batch file code Public Key : C:\Encryption\PB_Key.gpg File need to encrypt : C:\Encryption\Test.txt Bat file : C:\Encryption\Test.bat gpg.exe : C:\Program Files (x86)\GnuPG\bin\gpg.exe UserName : Test – coder88 May 17 '18 at 14:41
  • @hemant that is hard to read. You are probably importing incorrect key ` --import PB_Key.asc`. Is your key successfully imported after the import? Check it with `gpg.exe --list-secret-keys --keyid-format LONG` – tukan May 17 '18 at 14:50
  • @echo off cd "C:\Encryption" "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.asc set /p user_id=Enter the User ID: If I will use only above line I am getting following message gpg: key 6F532BB24D36B6CA: "User_Test " not changed gpg: Total number processed: 1 gpg: unchanged: 1 Enter the User ID: I assume here It is imported but when I tried to generate the encrypted document it is not working. If you don't mind can I have your email ID, I can send you the code as here it is hard to read. Else reply me here. – coder88 May 17 '18 at 15:01
  • @Hemant you got complete workflow. Nothing more I could do to help you. Enter the *User_test* as ID when asked. – tukan May 17 '18 at 17:53
  • I appreciate your help.I had run the given script to check whether public key imported or not and it turns out it imported perfectly. Here I was looking the User_ID should be updated in the bat file and when I will run that batch directly I will get the encrypted document.I no need to type User_ID in the run time. If possible can you send me the script based on above requirement. – coder88 May 18 '18 at 04:25
  • @hemant you can not fill the information into CLI from batch file. The correct way is the way I did it. Btw. did you try to enter the first & last name instead of email? As you saw I entered Jonh Smith from the key. – tukan May 18 '18 at 07:10
  • Yes I tried and its working, if I will update the User Name manually. But here I am looking any way where we can update the User Name, Public key and encrypted doc file path I will update in Batch file and when I will run, then directly I will get the encrypted doc. One last thing, if we can not update the user name in batch file then, do we have any other workaround where we can update all the details one single file and run same to generate encrypted document. – coder88 May 18 '18 at 08:54
  • @Hemant I meant try when using batch file. As you could see you can update a user name using batch file. My guess is that you are having some extra space (batch files are really really picky) or some other detail (like double quotes). Can't you imitate my workflow? You can put `-r "John Smith"` instead of `"%user_id%"`. – tukan May 18 '18 at 09:08
  • I have tried the given batch and it worked. Please find below batch file script. @echo off "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --import PB_Key.asc "C:\Program Files (x86)\GnuPG\bin\gpg.exe" -r "Test" -e "C:\Encryption\Test.txt" – coder88 May 19 '18 at 10:55
  • Appreciate your effort and elaboration. :) – coder88 May 19 '18 at 10:57
  • @Hemant Ah, you wanted non-interactive way! I misunderstood it. I'm glad you worked it out. I'll edit the post last time to have a non-interactive way. I'm glad I could help a little. – tukan May 21 '18 at 06:54
  • @Hemant Hehe, If I help you a lot you can even "upvote" ;). Take care and I hope you will have good times at SO. – tukan May 21 '18 at 07:18
  • Buddy, I want to give upvote but I don't have much points. Hence not able to do it. Anyways your guidance is much helpful. One last thing, If I don't have user id for public key, can you tell me how to generate encrypted doc. I have tried committing the -r and user name details but its not working. – coder88 May 21 '18 at 07:48
  • Found a way gpg --always-trust – coder88 May 25 '18 at 04:31
  • @Hemant Hmm some bug at SO, did not see your penultimate post. Glad you got it sorted out. – tukan May 25 '18 at 06:22