2017-10-28 90 views
6

現在我要獲得android調試密鑰的簽名。Openssl的結果是不匹配的cmd和windows的電源外殼

在windows命令(cmd.exe的)

keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl.exe sha1 -binary | openssl.exe base64 
Enter keystore password: android 

Warning: 
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore debug.keystore -destkeystore debug.keystore -deststoretype pkcs12". 
uQzK/Tk81BxWs8sBwQyvTLOWCKQ= 

在windows電源外殼

keytool -exportcert -alias androiddebugkey -keystore debug.keystore | .\openssl.exe 
sha1 -binary | .\openssl.exe base64 
Enter keystore password: android 

Warning: 
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore debug.keystore -destkeystore debug.keystore -deststoretype pkcs12". 
Pz8/Pwo/MDNuPyE/Pys/Pz8/Sm8K 

兩個結果不匹配。

cmd.exe的:uQzK/Tk81BxWs8sBwQyvTLOWCKQ =

電源外殼:Pz8 /的Pwo/MDNuPyE/PYS/Pz8/Sm8K

爲什麼? 發生了什麼?

回答

4

這是PowerShell中對象管道的結果,您不應該在PowerShell中管道原始二進制數據,因爲它會損壞。

在PowerShell中管理原始二進制數據永遠不會安全。 PowerShell中的管道用於可以安全地自動轉換爲字符串數組的對象和文本。請詳細閱讀this瞭解詳情。

用powershell計算的結果是錯誤的,因爲您使用了管道。解決這個問題的方法之一是使用cmd.exe的從PowerShell中:寫入輸入/輸出

cmd /C "keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl.exe sha1 -binary | openssl.exe base64" 

而不是使用管道可以讀/自/至文件。不幸的是,openssl.exe sha1沒有-in參數來指定輸入文件。因此,我們需要使用PowerShell的命令行Start-Process,它允許讀取和處理參數-RedirectStandardInput-RedirectStandardOutput寫入文件:

keytool -exportcert -alias mykey -storepass wortwort -file f1.bin 
Start-Process -FilePath openssl.exe -ArgumentList "sha1 -binary" -RedirectStandardInput f1.bin -RedirectStandardOutput f2.bin 
Start-Process -FilePath openssl.exe -ArgumentList base64 -RedirectStandardInput f2.bin -RedirectStandardOutput o_with_ps.txt 

keytool寫入到文件f1.bin。然後openssl.exe sha1f1.bin讀取並寫入f2.bin。最後,從openssl.exe base64讀取f2.bin和PowerShell的回購寫入o_with-ps.txt

+2

1此問題也被在[這個問題]長度討論(https://github.com/PowerShell/PowerShell/issues/1908)。 –