Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Gpg encrypts and decrypts batch files

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

When pgp does decryption and encryption, the command line always needs to manually enter the password and specify ID, which is more tedious, so I wrote an automatic script.

The more interesting parts are:

Setlocal enabledelayedexpansion variable delay and! Variables! The use of. In the same way, you can read the contents of different lines in a text file to assign variables.

@ echo off

:: todo

Setlocal enabledelayedexpansion

Set / a jambo0

For / f "delims="% I in (ps.txt) do (

Set / a junk 1

Rem set / a junk 1

Rem echo! j!

Rem echo I

If! jackpot 1 set ps=%%i

Rem if! jackpot 2 set txt=%%i

)

Rem echo ps%

Rem echo txt%

Set inputfile=%1%

Echo

Echo Decrypt the pgp file from WideVine portal -% inputfile%

Echo

Echo

Set outputfile=%inputfile:~0,-28%xml

Rem echo Output file -% outputfile%

Gpg-passphrase% ps%-decrypt% inputfile% >% outputfile%

Echo

Echo Decrypt the pgp file successfully.

Echo

Echo

Echo Encrypt the outputfile% with customer key

Echo

Echo

Gpg-r widevine_keys-e% outputfile%

Rm outputfile%

There is a problem, however, that when the file is finally encrypted, gpg always appears the following prompt and asks to select YBO

It is NOT certain that the key belongs to the person named

In the user ID. If you really know what you are doing

You may answer the next question with yes.

Use this key anyway? (ycompan N) y

After checking, it is found that there is no trust in key. The operation is as follows:

Gpg-edit-key key-uid

Gpg then lists the key information:

Secret key is available.

Pub 2048R/B89A8C48 created: 2018-03-07 expires: never usage: SC

Trust: ultimate validity: ultimate

Sub 2048R/F13C4008 created: 2018-03-07 expires: never usage: e

[ultimate] (1) Jacky Wang

Invalid command (try "help")

Gpg >

Then enter trust, and enter will display:

Please decide how far you trust this user to correctly verify other users' keys

(by looking at passports, checking fingerprints from different sources, etc.)

1 = I don't know or won't say

2 = I do NOT trust

3 = I trust marginally

4 = I trust fully

5 = I trust ultimately

M = back to the main menu

Your decision?

Enter 5, then enter, and then encryption will not always prompt questions.

Google's WV portal recently changed the process by uploading a device id file, and then the generated key will be generated in turn according to the uploaded device id (the mac address of the device). But now you don't need it, just enter how many key you want to generate, and then device id increments from 0 by default. The factory-produced script has to be modified, and in order to reduce conflicts, you have to write a preprocessed script to replace the mac address with the incremental device id.

The script is much more complex, using multiple for / f loops to get different segments of the same line, and then using redirected output.

Summary of questions:

Sometimes internal variables in the for / f loop can assign values to external variables, sometimes not, for example, num is fine, but when you want to assign the contents of different segments to the variable str1/2/3, and then there is always a problem with str1/2/3 through string manipulation at the end, str cannot get the assignment. I don't know why.

The contents of the document are as follows:

2412

C5f4edc5ff57aff896abf7adf42c3481000000020000206

Script

For / f skip^ = 2 ^ tokens ^ = 1 ^, 3 ^ delims ^ = ^ > ^

< %%m in (%xmlfile%) do ( rem echo %%m >

> num.txt

Set num=%%m

Set num1=%%n

Goto gg

)

: gg

Rem echo Find% num% keys from XML file.

Echo num=%num1%

Echo num1=%num1%

Try several case and print them as follows

For / f skip^ = 2 ^ tokens ^ = 1 ^-3 ^ delims ^ = ^ > ^

< %%m in (%xmlfile%) do ( ... num=NumberOfKeyboxes num1=2412 for /f skip^=2^ tokens^=1^,3^ delims^=^>

^

< %%m in (%xmlfile%) do ( ... num=NumberOfKeyboxes num1=/NumberOfKeyboxes for /f skip^=2^ tokens^=2^ delims^=^>

^

< %%m in (%xmlfile%) do ( num=2412 num1=%n 到此为止一切正常。 接着想把c5f4edc5ff57aff896abf7adf42c3481000000020000206 这部分内容按照双引分成三段,第一和第二保持不变,替换第二段为mac地址。 for /f skip^=3^ tokens^=1^,3^ delims^=^"^" %%m in (%xmlfile%) do ( rem echo %%m set str1=%%m set str3=%%n set str5=%%o goto cc ) :cc echo str1=%str1% echo str3=%str3% echo str5=%str5% .... 报错 Find 2412 keys from XML file. The system cannot find the file specified. < was unexpected at this time. 试试其他办法 for /f skip^=3^ tokens^=1^-3^ delims^=^"^" %%m in (%xmlfile%) do ( ... str3正确,str1不成功 The system cannot find the file specified. str3=device_id_0 str5=%o for /f skip^=3^ tokens^=1^ delims^=^"^" %%m in (%xmlfile%) do ( ...str1拿不到 The system cannot find the file specified. str3=%n for /f skip^=3^ tokens^=2^ delims^=^"^" %%m in (%xmlfile%) do ( ...str1正确 str1=device_id_0 str3=%n for /f skip^=3^ tokens^=3^ delims^=^"^" %%m in (%xmlfile%) do ( ... str1报错 < was unexpected at this time. 只有tokens为2的情况下,似乎获取没有问题,其余case都不行,原因不得而知。 接着我试图获取一整行,然后用字符串处理函数来实现功能,但是发现即便是获取整行仍然出现问题, for /f "skip=3 delims=" %%m in (%xmlfile%) do ( rem echo %%m 此处打印能正常打印出内容 set str1=%%m goto cc ) :cc echo str1=%str1% ... 整行内容得不到,但是在for循环内部能正常打印%%m的内容。 < was unexpected at this time. 最终能工作的脚本如下: @echo off :todo rem get gpg key password from ps.txt setlocal enabledelayedexpansion set /a j=0 for /f "delims=" %%i in (ps.txt) do ( set /a j+=1 rem set /a j+=1 rem echo !j! rem echo %%i if !j!==1 set ps=%%i rem if !j!==2 set txt=%%i ) rem echo %ps% rem echo %txt% set para=%1% if %para%==-r ( set inputfile=%2% ) else ( set inputfile=%1% ) echo ----------------------------------------------------------- echo Decrypt the pgp file from WideVine portal - %inputfile% echo ----------------------------------------------------------- echo; rem xxxx.txt.1540199541676.output.pgp set xmlfile=%inputfile:~0,-28%xml set macfile=%inputfile:~0,-28%txt set tmpfile=%inputfile:~0,-28%tmp echo TXTfile - %txtfile% XMLfile - %xmlfile% gpg --passphrase %ps% --decrypt %inputfile% >

% xmlfile%

If% para%==-r (

Goto hh

) else (

Goto ii

)

Goto eof

: hh

Echo

Echo Preprocess -% xmlfile% to replace device ID with mac address from% macfile%

Echo

Setlocal enabledelayedexpansion

Set / a jambo0

Set / a kryp3

Set / a lump 0

For / f "delims="% I in (% xmlfile%) do (

Set / a junk 1

If! jackpot 4 goto aa

Echo I > > tmpfile%

)

: aa

For / f skip^ = 2 ^ tokens ^ = 2 ^ delims ^ = ^ > ^

< %%m in (%xmlfile%) do ( rem echo %%m >

> num.txt

Set num=%%m

Goto gg

)

: gg

Echo Find% num% keys from XML file.

: bb

Rem goto eof

Rem echo first time 1%

For / f skip^ =% k% ^ tokens ^ = 1 ^ delims ^ = ^ "%% m in (% xmlfile%) do (

Rem echo m

If% m = = ^ (

Rem > >% tmpfile% set / p = "" >% tmpfile%

Goto ff

)

Rem echo m > > tmpfile%

Set / packs% m >% tmpfile%

Rem set str=%%m

Rem > >% tmpfile% set / paired%% m >% tmpfile%

Rem > >% tmpfile% set / paired%% a% tmpfile%

Rem set str2=%%a

Goto dd

)

) else (

For / f "skip=%l% delims=" an in (% macfile%) do (

Rem echo a

Rem echo'a'> > tmpfile%

Rem > >% tmpfile% set / paired%% a% tmpfile%

Rem set str2=%%a

Goto dd

)

)

: dd

For / f skip^ =% k% ^ tokens ^ = 3 ^ delims ^ = ^ "% n in (% xmlfile%) do (

Rem echo n

Rem echo n > > tmpfile%

Set / packs% n >% tmpfile%

Rem set str3=%%n

Goto ee

)

Rem echo str2%

Rem echo a

Rem echo m'a'n > > tmpfile%

: ee

Set / a krypton1

Set / a lump number 1

Echo. > > tmpfile%

Goto bb

: ff

Rem echo i%%a%%j > > tmpfile%

Rem rm xmlfile%

Rem ren tmpfile% xmlfile%

Rem echo "> > tmpfile%

If! lame flowers% num% (

Echo Total! l! Keys generated!

Rm xmlfile%

Ren tmpfile% xmlfile%

) else (

Echo Error: Key number not match, please check!

Goto eof

)

: ii

Echo

Echo Decrypt the pgp file successfully.

Echo

Echo

Echo Encrypt the xmlfile% with customer key

Echo

Echo

Gpg-r widevine_keys-e% xmlfile%

Rm xmlfile%

: eof

The script on Mac OS needs to modify the key.sh slightly as follows:

#! / bin/bash

# file= ". / PS.txt"

File= "/ Users/jackywang/Documents/GPG/Harman/PS.txt"

If [[- f "$file"]]

Then

# read it

While IFS= read line

Do

Ps= "$line"

Done

< "$file" else echo "password file not exist!!!" exit fi inputfile=$1 echo $inputfile #inputlen=$inputfile.length echo ----------------------------------------------------------- echo Decrypt the pgp file from WideVine portal - $inputfile echo ----------------------------------------------------------- echo; extstr=${inputfile:0-28:28} xmlfile=${inputfile/%$extstr/xml} macfile=${inputfile/%$extstr/txt} tmpfile=${inputfile/%$extstr/tmp} echo TXTfile - $macfile XMLfile - $xmlfile gpg --passphrase $ps --decrypt $inputfile >

$xmlfile

Echo

Echo Decrypt the pgp file successfully.

Echo

Echo

Echo Encrypt the xmlfile% with customer key

Echo

Echo

Gpg-r widevine_keys-e $xmlfile

Rm $xmlfile

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report