1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# .NET Core RSACryptoServiceProvider でXML文字列形式の鍵を読み込む

Posted at

.NET Core では RSACryptoServiceProvider.FromXmlString メソッドで例外が発生します

.NET Core では RSACryptoServiceProvider クラスの FromXmlString / ToXmlString メソッドがサポートされていません。PlatformNotSupportedException がスローされます。

秘密鍵の読込
// using System.Security.Cryptography;

var privateKey = "<RSAKeyValue><Modulus>yT12/iqZLN....Q==</D></RSAKeyValue>";
var rsa = new RSACryptoServiceProvider();

// ここで PlatformNotSupportedException がスローされます。
// 'Operation is not supported on this platform.'
rsa.FromXmlString(privateKey);

対処法

FromXmlString / ToXmlString の代替メソッドを公開している方がいらっしゃいました。
【GithubGist】Jargon64/RSACryptoServiceProviderExtensions.cs

var privateKey = "<RSAKeyValue><Modulus>yT12/iqZLN....Q==</D></RSAKeyValue>";
var rsa = new RSACryptoServiceProvider();

// 鍵を読み込むことができます。.NET Core 2.1 で確認。
// シグネチャが同一である場合は拡張メソッドではなく本体に実装されているメソッドが優先されるため、メソッド名は変更しました。
rsa.FromXmlStringEx(privateKey);
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?