.NET/PowerShell核心中的RSACryptServiceProvider、SignData和填充
我正在尝试使用Powershell中.NET RSACryptoServiceProvider类的SignData方法 在Windows 10/Powershell 5.1/.NET 4.7上,以下代码工作可靠:.NET/PowerShell核心中的RSACryptServiceProvider、SignData和填充,.net,powershell,cryptography,.net,Powershell,Cryptography,我正在尝试使用Powershell中.NET RSACryptoServiceProvider类的SignData方法 在Windows 10/Powershell 5.1/.NET 4.7上,以下代码工作可靠: $toSign = [System.Text.Encoding]::UTF8.GetBytes("ABCDE") $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $ce
$toSign = [System.Text.Encoding]::UTF8.GetBytes("ABCDE")
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.import("c:\ps\GAdmin\apiaccess.pfx","notasecret","Exportable,PersistKeySet")
$cert | fl *
$params = New-Object System.Security.Cryptography.CspParameters
$params.KeyContainerName = $cert.PrivateKey.CspKeyContainerInfo.KeyContainerName
$params.ProviderName = "Microsoft Enhanced RSA and AES Cryptographic Provider"
$params
$cert.PrivateKey.CspKeyContainerInfo.KeyNumber
$params.KeyNumber = 1
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider($params)
$tosign
$rsa.SignData($toSign,"SHA256")
但是,在具有.NET内核/Powershell内核的Linux(或Windows)上,我遇到了一些问题。我可以实例化RSACryptServiceProvider,但签出数据错误:
$cert = Get-PfxCertificate ./apiaccess.pfx -Password (ConvertTo-SecureString "notasecret" -AsPlainText -Force)
$rsa = $cert.PrivateKey
$rsa.SignData($toSign,"SHA256")
找不到“SignData”和参数计数“2”的重载。
第1行字符:1
+$rsa.SignData($toSign,“SHA256”)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified:(:)[],MethodException
+FullyQualifiedErrorId:MethodCountNotFindBest
检查方法定义-似乎需要填充参数:
($rsa | get-member SignData).Definition
字节[]SignData(字节[]数据,System.Security.Cryptography.HashAlgorithmName HashAlgorithmName,System.Security.Cryptography.RSASignaturePadding padding),字节[]SignData(字节[]数据,int偏移量,int计数,System.Security.Cryptography.HashAlgorithmName HashAlgorithmName,System.Security.Cryptography.RSASignaturePadding padding),字节[]SignData(System.IO.Stream数据,System.Security.Cryptography.HashAlgorithmName hashAlgorithm,System.Security.Cryptography.RSASignature添加填充)
所以我的问题是:如何从Powershell Core指定填充和调用SignData?可能是
$cert.PrivateKey
返回了RSACryptServiceProvider以外的内容。您确实应该避免调用该属性,并应将其替换为$cert.GetRSAPrivateKey()
(或者不管您希望密钥是什么算法……如果您弄错了,它会返回null
,因此不需要担心异常)。这不是说它会有帮助,只是它保证在Linux上永远不会返回RSACryptServiceProvider(并且“几乎保证”在Windows上不会返回它)。虽然它可以解决您在Windows上重新解释CSP参数的需要
而不是打电话`
$rsa.SignData($toSign, "SHA256")
你应该打电话
$rsa.SignData(
$toSign,
System.Security.Cryptography.HashAlgorithmNames.SHA256,
System.Security.Cryptography.RSASignaturePadding.Pkcs1)
对于后两个参数,我实际上不知道powershell ese,希望我对该语言语法的理解足以解除对您的阻止。“SHA256”和“Pkcs1”以下是属性,如果这样可以使代码更美观,您可以将它们保存到本地。我原以为Linux()上的RSACryptServiceProvider存在问题。对于您关于Windows Core的问题,请查看这篇文章。这两个答案都来自Microsoft的Jeremy Barton。堆栈上的问题应该能提供您所需要的。@Adam-您是对的,这个问题并不是特定于此提供程序的。行为已经改变,并且也影响到RSACng和OpenSSL提供程序;我应该更精确一些。Powershe这是“$rsa.SignData($toSign,[Security.Cryptography.HashAlgorithmName]::SHA256,[Security.Cryptography.RSASignature Padding]::Pkcs1)”-它可以工作了!谢谢你,也感谢Vadims Podans(@Crypt32)。