[英]Using x509 certificates to decrypt text with PowerShell that was encrypted with OpenSSL
我正在尋找一個PowerShell腳本來解密在Linux環境中使用OpenSSL使用證書公用密鑰加密的機密。
帶有要用於解密的私鑰的證書位於Windows Local Machine證書存儲中。
到目前為止,我嘗試使用[Security.Cryptography.RSACryptoServiceProvider].PrivateKey.Decrypt
解密加密文件中的字節,但是填充出現錯誤
$mycert.PrivateKey.Decrypt($bytes, $true)
Exception calling "Decrypt" with "2" argument(s): "Error occurred while decoding OAEP padding."
At line:1 char:1
+ $mycert.PrivateKey.Decrypt($bytes, $true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CryptographicException
我可以得到一些有關如何解決此密碼異常的建議嗎?
在Linux中,使用以下示例openssl命令生成了包含密文的文件:
echo " Secret Text" | openssl rsautl -encrypt -inkey publickey.pem -pubin > secret.enc
可以使用OpenSSL對其進行很好的解密,但是嘗試使用PowerShell時,我們總是會遇到相同的錯誤。
openssl rsautl
,不帶-oaep
標志,將執行PKCSv1_1.5填充,這與您正在調用的Decrypt
方法中的false
值相對應。
切線:如果您可以在Powershell范圍內訪問.NET 4.6 API,則可以從將cert.PrivateKey
強制cert.PrivateKey
為RSACryptoServiceProvider
到調用cert.GetRSAPrivateKey()
並使用較新的RSA.Decrypt(data, RSAEncryptionPadding.Pkcs1)
來獲得更好的服務。 。 有些證書/密鑰對舊方法無效,而新方法無效。
當在OpenSSL的加密命令中未指定填充時,$ mycert.PrivateKey.Decrypt($ bytes,$ false)起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.