通过提供模数和指数进行Powershell RSA加密

时间:2019-02-22 05:49:07

标签: powershell encryption cryptography rsa azure-runbook

我试图通过提供模数和指数在powershell脚本中进行RSA加密,并且已将其部署在Azure自动运行手册中。

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
}

当我运行相同的代码时,出现以下错误

  

捕获到异常:   System.Management.Automation.MethodInvocationException:异常   调用参数为“ 1”的“ ImportParameters”:“错误数据。

     

“ ---> System.Security.Cryptography.CryptographicException:数据错误。

     在

  System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32   hr)

     

在System.Security.Cryptography.Utils._ImportKey(SafeProvHandle   hCSP,Int32 keyNumber,CspProviderFlags标志,Object cspObject,   SafeKeyHandle&hKey)

     在

  System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters   参数)

     

在CallSite.Target(Closure,CallSite,Object,Object)

     

---内部异常堆栈跟踪的结尾---

     在

  System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext   funcContext,异常例外)

     在

  System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame   框架)

     在

  System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame   框架)

     在

  System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame   框架)

0 个答案:

没有答案
相关问题