0

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)

Alias Varghese
  • 2,104
  • 3
  • 24
  • 52
  • What size is your data? On second thought, you are already crashing when importing the key parameters (feel free to mention that in your post). What values do you have for N and E? – Maarten Bodewes Feb 22 '19 at 14:51
  • I'm voting as off topic, without the info this question cannot be answered in my opinion. – Maarten Bodewes Feb 23 '19 at 13:32

0 Answers0