I am working on a non-Powershell tool (NodeJS) that will use Powershell for displaying a password prompt. For the purposes of this question, that means the following:
- I invoke the Powershell script with a public key.
- The public key will be used to encrypt the password given by the user.
- The Powershell script outputs the encrypted password.
- I can't use a version of Powershell that does not ship with Windows, so I'm stuck at Powershell 5.
For the life of me, I can't figure out a way to get the public key into a class that does encryption.
Some of the things I tried:
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsa.ImportFromPem($publicKeyPem)
# Method invocation failed because [System.Security.Cryptography.RSACng] does not contain a method named 'ImportFromPem'.
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsa.Import()
# Method invocation failed because [System.Security.Cryptography.RSACryptoServiceProvider] does not contain a method named 'Import'.
$rsa = [System.Security.Cryptography.AsymmetricAlgorithm]::Create()
$rsa.ImportRSAPublicKey()
# Method invocation failed because [System.Security.Cryptography.RSACryptoServiceProvider] does not contain a method named 'ImportRSAPublicKey'.
I definitely tried more options, all running into the same issue where the function does not exist. Because of this, I can't get it to import the key. When generating a new key pair in Powershell, I can successfully encrypt and decrypt. But that does not help me here.
This question is similar to Encrypting and Encoding a password string with a public key. But that solution is no help for me since its solution requires Powershell > 5.
I'm I trying to do something weird here? Surely using an existing public key for encryption shouldn't be this difficult.