I was trying to do RSA encryption in my powershell script by supplying modulus and exponent and I have deployed it in azure automation runbook.
function Get-EncryptedString($certPass)
{
Write-Output "Getting Encrypted String."
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$parameters = New-Object System.Security.Cryptography.RSAParameters
$parameters.Modulus = $script:secretKey.Key.N
$parameters.Exponent = $script:secretKey.Key.E
$rsa.ImportParameters($parameters)
$byteData = [System.Text.Encoding]::Unicode.GetBytes($certPass)
$encryptedData = $rsa.Encrypt($byteData, $true)
$result = [System.Convert]::ToBase64String($encryptedData)
Write-Output "Encrypted String retrieved: $result"
return $result
}
When I am running the same , I am getting the following error
caught exception: System.Management.Automation.MethodInvocationException: Exception calling "ImportParameters" with "1" argument(s): "Bad Data.
" ---> System.Security.Cryptography.CryptographicException: Bad Data.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
at CallSite.Target(Closure , CallSite , Object , Object )
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)