Powershell: System.Security.Cryptography ToXmlString was fail

Created on 27 Dec 2017  路  6Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Add-Type -AssemblyName System.Security
$CSPParam = New-Object System.Security.Cryptography.CspParameters
$CSPParam.KeyContainerName = "Test"
$RSA = New-Object System.Security.Cryptography.RSACryptoServiceProvider($CSPParam)
$PublicKey = $RSA.ToXmlString($False)

Expected behavior

Created Public Key

Actual behavior

Exception calling "ToXmlString" with "1" argument(s): "Operation is not supported on this platform."
At line:1 char:1
+ $PublicKey = $RSA.ToXmlString($False)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : PlatformNotSupportedException

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-rc.2
PSEdition                      Core
GitCommitId                    v6.0.0-rc.2
OS                             Microsoft Windows 10.0.17063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

All 6 comments

I believe it is CoreFX limitation.

How can I get public key?

You can see .Net Framework implementation http://referencesource.microsoft.com/#mscorlib/system/security/cryptography/rsa.cs,fd15a0bacd426550

.Net Core is ToXmlString not support ?

@MuraAtVwnet It would appear it is not supported on .NET Core : https://github.com/dotnet/corefx/blob/c0df023a72954f72ab42387cf511f16f1a0f1db6/src/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RSA.Xml.cs#L14 All that method does is throw a PlatformNotSupportedException exception.

T T
thank you

Was this page helpful?
0 / 5 - 0 ratings