【问题标题】:RSACryptoServiceProvider, SignData and padding in .NET/PowerShell Core.NET/PowerShell Core 中的 RSACryptoServiceProvider、SignData 和填充
【发布时间】:2018-05-04 05:28:54
【问题描述】:

我正在尝试使用 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
$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 Core/Powershell Core 的 Linux(或 Windows)上,我遇到了问题。我可以实例化一个 RSACryptoServiceProvider,但 SignData 出错了:

$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 : MethodCountCouldNotFindBest

检查方法定义 - 似乎需要填充参数:

($rsa | get-member SignData).Definition

byte[] SignData(byte[] data, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding), byte[] SignData(byte[] data, int offset, int count, System. Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding), byte[] SignData(System.IO.Stream data, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding)

所以我的问题是:如何指定填充并从 Powershell Core 调用 SignData?

【问题讨论】:

标签: .net powershell cryptography


【解决方案1】:

$cert.PrivateKey 可能返回的不是 RSACryptoServiceProvider。您确实应该避免调用该属性,并应将其替换为 $cert.GetRSAPrivateKey() (或您希望密钥使用的任何算法...如果您弄错了,它将返回 null ,因此不必担心异常)。并不是说它会有所帮助,只是它保证永远不会在 Linux 上返回 RSACryptoServiceProvider(并且“几乎保证”不会在 Windows 上返回它)。虽然它解决您在 Windows 上重新解释 CSP 参数的需要。

而不是调用`

$rsa.SignData($toSign, "SHA256")

你应该打电话

$rsa.SignData(
    $toSign,
    System.Security.Cryptography.HashAlgorithmNames.SHA256,
    System.Security.Cryptography.RSASignaturePadding.Pkcs1)

我实际上并不知道后两个参数的 powershell-ese,希望我对该语言语法的理解足以解除您的阻碍。这里的“SHA256”和“Pkcs1”都是属性,所以如果它让你的代码更漂亮,你可以将它们保存到本地。

【讨论】:

  • Powershell-ese 是 "$rsa.SignData($toSign, [Security.Cryptography.HashAlgorithmName]::SHA256, [Security.Cryptography.RSASignature Padding]::Pkcs1)" - 和有用!谢谢你,也感谢 Vadims Podans (@Crypt32)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-02-18
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2023-01-13
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多