1

I have googled extensively on this topic, but have not found any method to replicate the following functionality in powershell 5:

Powershell 7 code:

$rsaPrivateKey = [IO.File]::ReadAllText($RSAPrivateKeyFilePath)
$rsa = [System.Security.Cryptography.RSA]::create()
$rsa.ImportFromPem($rsaPrivateKey.ToCharArray())
$signature = $rsa.SignData($dataToSign, [Security.Cryptography.HashAlgorithmName]::SHA256, [Security.Cryptography.RSASignaturePadding]::Pkcs1)

The above contains .NET 5+ features not present in powershell 5. Does anyone know how to replicate the above powershell 7 functionality in powershell 5 (I am forced to use powershell 5)?

Dobob
  • 718
  • 1
  • 12
  • 26

1 Answers1

1

I have moved past this problem with a work-around by invoking the code in my question in PowerShell 7 from PowerShell 5. This worked for me since I needed to eventually install PowerShell 7 anyway, but the VM I use does not initially have PowerShell 7. I was hoping for a simple PowerShell 5 solution, but apparently that does not exist.

For example, the below code will print the version table of PowerShell 7 from PowerShell 5:

# Install PowerShell 7 before the following code.
$scriptBlock = {$PSVersionTable | Out-String}
$pwsh = "<path to pwsh.exe (PowerShell 7 exe file)>"
& $pwsh $scriptBlock
Dobob
  • 718
  • 1
  • 12
  • 26
  • The alternative would be to use e.g. openssl to convert the PEM-encoded key to a pkcs12 (*.pfx) file, which is easy to digest. https://stackoverflow.com/a/5394967/18771 – Tomalak Apr 02 '22 at 08:11